Skip to content

Issue with Message suppression #875

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
gmitch64 opened this issue Feb 6, 2018 · 4 comments
Closed

Issue with Message suppression #875

gmitch64 opened this issue Feb 6, 2018 · 4 comments

Comments

@gmitch64
Copy link

gmitch64 commented Feb 6, 2018

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"
@bergmeister
Copy link
Collaborator

@gmitch64 : Assuming you have a PowerShell script that you call directly has e.g. .\HelloWorld.ps1, then you can suppress as follows:

[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseDeclaredVarsMoreThanAssignments", "")]
Param()

Set-StrictMode -Version Latest


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

Please let me know if this answered your question.

@gmitch64
Copy link
Author

gmitch64 commented Feb 8, 2018

Thanks, that seems to make it work as I expected it.

What I am missing is my understanding of my I need to specify a Param() for it, and why it needs to be the first lines in the script. The more you learn, the more there is that you need to learn :)

Thanks very much for the solution, and for giving me more work to do :)

@bergmeister
Copy link
Collaborator

@gmitch64 You're welcome, I am glad it worked out for you. PSSA works by parsing PowerShell scripts as the PowerShell AST (abstract syntax tree), which are publicly available classes from the internals of PowerShell itself. The code that parses the script is here and you see here that it parses the script as scriptblock and function asts, therefore the Param() helps the parser to identify the whole script file as a function. But I am sure that this parsing code could be improved so that the Param() is not needed any more. At the moment, ideas are also floating around in issue 849 to enhance suppression to be able to enable/disable suppresions just for a few lines (similar to ReSharper warnings).
If your question is answered satisfactorily, then please close the issue.

@gmitch64
Copy link
Author

@bergmeister Thanks for the explanation. I'll upvote issue 849, as it would be useful I think. I'll mark this issue as closed.

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants