-
Notifications
You must be signed in to change notification settings - Fork 395
Warning about unused variable in VSCode #827
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
+1 |
I tried running Script Analyzer on its own against the original repro and I'm seeing the warning. Here's the repro: In the file $a = 0
$b = @(1, 2)
$b | ForEach-Object {
$a += 1
}
Write-Host $a in a terminal: Install-Module -Name PSScriptAnalyzer
Invoke-ScriptAnalyzer -Path path/to/repro.ps1 -Settings PSGallery -Recurse The output:
|
I think this warning is appropriate. In your example, $a = 1
& {
$a = 2
}
$a I don't believe there is an accurate way for PSScriptAnalyzer to guess that your intention is to use the |
There are various issues with false positives of this rule that would probably need to be re-written from the ground up using a different technique to overcome its limitations. I plan to collate all those issues in a meta-issue at some point. |
Here is my example. $RemoveCount is reported as unused. $RemoveError is not main loopk in code. Calls functions Remove-member() and Write-Log()$BaseDir = "C:\Temp\attest"
} |
+1 The directive to suppress messages also does not work:
Anyone know how to suppress this in the visual code settings? |
@GuyWicks Your example case already got fixed in PR #836 and will be in the upcoming release of 1.17 that is hopefully going to be released in the next days. |
Thanks for this. For future reference, this now works as I expected:
Agree with your recommendation to use this suppression with caution - in my particular use-case I have a common file that stores global configuration variables so these are not used in this file, but in other files. You may legitimately criticise this design model :) |
If the warning appears in variables you're referencing inside a To borrow/modify @mattmcnabb's example:
I think that method is |
I ran into this warning with ForEach-Object's Script Blocks:
Shows warning: $i and $nums unused. |
A wrong warning about unused variable, but that variable used later in code:
The text was updated successfully, but these errors were encountered: