-
Notifications
You must be signed in to change notification settings - Fork 393
PSUseDeclaredVarsMoreThanAssignments false positive when used via Get-Variable #831
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
Comments
Thanks for reporting the case. However, it is a known issue that |
@bergmeister I checked the other issues on this repo for problems with this rule and found a few, but didn't see any for this particular false positive. Given the wide array of challenges with this rule, I often find myself telling junior scripters to omit this rule from their testing/review processes. I am certainly sympathetic that it can be hard to dedicate resources to issues like this one, but as you alluded to, sometimes it's just good to have the problem documented. Perhaps there will be an enterprising member of the community who will feel compelled to re-write the |
@ThmsRynr You can still just suppress the warning on a case by case basis. I also found that using |
Awesome! Looking forward to seeing them merged :) |
@ThmsRynr, @bergmeister the PRs have been merged, could you take a look? |
@JamesWTruher I tried it but the PRs did not fix this issue and the reason seems to be because the variable is accessed only indirectly. This issue is tricky and some people might even say it is by design because the variable itself is not used directly. $variable = 'a value'
Write-Output "The value is $variable" |
I filed this bug (PowerShell/vscode-powershell#1226) on vscode-powershell. It got closed with reference to this bug. |
There are various known issues with false positives of this rule. The most similar one to the one reported by you is this one here. Small improvements have already been made to the rule in the meantime but this is a long ongoing progress of tweaking the rule that was initially written in a simplistic way to provide some value. The rule will effectively only become better once it uses SSA (single statement analysis) which is at the moment not completely implemented and a big undertaking. |
There are various known issues with false positives of this rule. The most similar one to the one reported by you is this one here. Small improvements have already been made to the rule in the meantime but this is a long ongoing progress of tweaking the rule that was initially written in a simplistic way to provide some value. The rule will effectively only become better once it uses SSA (single statement analysis) which is at the moment not completely implemented and a big undertaking. |
The PSUseDeclaredVarsMoreThanAssignments rule will erroneously throw a warning if a variable is declared but only used via the
Get-Variable
cmdlet.Steps to reproduce
Create a .ps1 file with the following contents.
Examine the file with PSScriptAnalyzer.
A warning will be created.
Screenshot
This is in addition to the issues this rules has when splatting or using in
Invoke-Command
,foreach-object
loops, and others which have already got issues created for them. I couldn't find another open issue for this particular false positive.The text was updated successfully, but these errors were encountered: