Skip to content

How to suppress a warning in a method? #1586

Closed

Description

Following example code:

class test {
    [String]$name

    [Array]test_method ($test1, $test2) {
        ...
    }
}

How can i supress a warning in the method test_method?

Activity

  1. changed the title [-]How to suppress a warning for a method?[/-] [+]How to suppress a warning in a method?[/+] on Sep 12, 2020
  2. JamesWTruher commented on Sep 15, 2020

    @JamesWTruher
    Contributor

    it can be done with the appropriate attribute as the following shows:

    PS> $ss = 'class foo {                         
    >> [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingCmdletAliases", "")]
    >> [string]ToString() {   
    >> return (gci).ToString()
    >> } 
    >> }'
    PS> Invoke-ScriptAnalyzer -ScriptDefinition $ss
    PS>                                               

    without the attribute

    PS> $ss = 'class foo {                         
    >> [string]ToString() {                                                                
    >> return (gci).ToString()
    >> }                      
    >> }'
    PS> Invoke-ScriptAnalyzer -ScriptDefinition $ss
                           
    RuleName                            Severity     ScriptName Line  Message
    --------                            --------     ---------- ----  -------
    PSAvoidUsingCmdletAliases           Warning                 3     'gci' is an alias of 'Get-ChildItem'. Alias can introduce
                                                                      possible problems and make scripts hard to maintain. Please
                                                                      consider changing alias to its full content.
  3. floh96 commented on Sep 15, 2020

    @floh96
    ContributorAuthor

    It's not working in the following example:

    Class data_bag {
      [String]$name
    
      [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidLongLines", "")]
      [Array]decrypt ($decryption_key_location, $decryption_location) {
        $decrypted_data_bag = bundle exec knife data bag show passwords $this.name --secret-file $decryption_key_location -F json -z
        return $decrypted_data_bag
      }
    }
    

    I have set MaximumLineLength = 80 in psscriptanalyzersettings.psd1.
    Still displays the warning:

    RuleName                            Severity     ScriptName Line  Message
    --------                            --------     ---------- ----  -------
    PSAvoidLongLines                    Warning      beratertes 68    Line exceeds the configured maximum length of 80 characters
                                                     t_vm_initi
                                                     al_setup.p
                                                     s1
    1 rule violation found.    Severity distribution:  Error = 0, Warning = 1, Information = 0
    
  4. floh96 commented on Sep 15, 2020

    @floh96
    ContributorAuthor

    James Truher (@JamesWTruher) when i call psscriptanalyzer with Invoke-ScriptAnalyzer -Path .\scripts\test.ps1 -ReportSummary it is showing 0 warnings but when i call psscriptanalyzer with Invoke-ScriptAnalyzer -Path . -Recurse -ReportSummary it's showing the warning.
    Is this a known bug?

    Edit: it's always showing the warning i was not requiring the PSScriptAnalyzerSettings.psd1 file when i called ScriptAnalyzer against the file.

  5. rjmholt commented on Sep 15, 2020

    @rjmholt
    Contributor

    Code for context:

    /// <summary>
    /// Returns a list of rule suppression from the class
    /// </summary>
    /// <param name="typeAst"></param>
    /// <returns></returns>
    internal List<RuleSuppression> GetSuppressionsClass(TypeDefinitionAst typeAst)
    {
    List<RuleSuppression> result = new List<RuleSuppression>();
    if (typeAst != null && typeAst.Attributes != null && typeAst.Attributes.Count != 0)
    {
    result.AddRange(RuleSuppression.GetSuppressions(typeAst.Attributes, typeAst.Extent.StartOffset, typeAst.Extent.EndOffset, typeAst));
    }
    if (typeAst.Members == null)
    {
    return result;
    }
    foreach (var member in typeAst.Members)
    {
    FunctionMemberAst funcMemb = member as FunctionMemberAst;
    if (funcMemb == null)
    {
    continue;
    }
    result.AddRange(RuleSuppression.GetSuppressions(funcMemb.Attributes, funcMemb.Extent.StartOffset, funcMemb.Extent.EndOffset, funcMemb));
    }
    return result;
    }

  6. floh96 commented on Sep 22, 2020

    @floh96
    ContributorAuthor

    James Truher (@JamesWTruher) Rob Holt (@rjmholt) Sydney Smith (@SydneyhSmith) is someone looking into this? (sorry for asking again since this issue has the resolution anwsered label I was afraid that it will get overlooked.)
    Or should I open a new issue?

  7. ghost closed this as completedon Sep 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions