You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
$foo="never used"# Should generate PSUseDeclaredVarsMoreThanAssignments$global:var1=$null# Should generate PSAvoidGlobalVars
Now let's try to add these suppressions:
[Diagnostics.CodeAnalysis.SuppressMessage('NonexistentRule','')]
$foo="never used"# Should still generate PSUseDeclaredVarsMoreThanAssignments
[Diagnostics.CodeAnalysis.SuppressMessage('PSAvoidGlobalVars','')]
$global:var1=$null# Should NOT generate PSAvoidGlobalVars
Expected behavior
I should still see the PSUseDeclaredVarsMoreThanAssignments warning, but not the PSAvoidGlobalVars one.
Actual behavior
The opposite happens, the first message is suppressed, but the second is not:
RuleName Severity ScriptName Line Message
-------- -------- ---------- ---- -------
PSAvoidGlobalVars Warning Test.ps1 5 Found global variable 'global:var1'.
Environment data
>$PSVersionTable
Name Value
---------
PSVersion 7.4.5
PSEdition Core
GitCommitId 7.4.5
OS Microsoft Windows 10.0.22631
Platform Win32NT
PSCompatibleVersions {1.0,2.0,3.0,4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0> (Get-Module-ListAvailable PSScriptAnalyzer).Version |ForEach-Object { $_.ToString() }
1.23.0
The text was updated successfully, but these errors were encountered:
Variables that are assigned with attributes are not handled by the PSUseDeclaredVarsMoreThanAssignments rule. For example, this: [ValidateNotNull()]$var1 = 1 is also not handled by that rule because of the validation attribute. Multiple type conversions/constraints also don't work: [string] [int] $var1 = 1.
The root cause and fix is likely the same that I recently found in the ast analyzer for variable completion: PowerShell/PowerShell#25016
Here I was only looking at ConvertExpressions and assuming the child would be the variable when I should have looked for AttributedExpressions and traverse through the children until I found the variable expression.
Steps to reproduce
Consider this code:
Now let's try to add these suppressions:
Expected behavior
I should still see the
PSUseDeclaredVarsMoreThanAssignments
warning, but not thePSAvoidGlobalVars
one.Actual behavior
The opposite happens, the first message is suppressed, but the second is not:
Environment data
The text was updated successfully, but these errors were encountered: