Skip to content

Issue with Message suppression #875

@gmitch64

Description

@gmitch64

There seems to be an issue with my understanding of message suppression, or there's a bug.

I have a source file that simply is a list of variables used in our environment that I want to include in some scripts. Since they're all assigned, but never used, ScriptAnalyzer throws up the appropriate message. Given that there's almost a hundred lines in there, it makes the output very messy (and VSCode messy too).

I've tried to suppress the message with
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseDeclaredVarsMoreThanAssignments", "")]
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseDeclaredVarsMoreThanAssignments", "", Scope="Module")]
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseDeclaredVarsMoreThanAssignments", "", Scope="Module", Target="*")]

and various types of scope (module, script, function etc.) and it only ever suppresses the warning for the next variable defined. I've attached a simple file to show the issue - the only message suppressed is the one for the DefaultDHCPServer.

If I add multiple Suppress lines, one before each variable declaration, it works, but the code gets very messy, very quickly.

What am I doing wrong or missing? Or is there an issue with the code?

Thanks.

Graham.

Set-StrictMode -Version Latest

[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseDeclaredVarsMoreThanAssignments", "", Scope="Module", Target="*")]

$DefaultDHCPServer = "server"
$DefaultDNSServer = "server"
$DefaultDomainName = "domain"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions