-
Notifications
You must be signed in to change notification settings - Fork 382
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
Cmdlets that run in current scope (like ForEach-Object) not accounted for by PSUseDeclaredVarsMoreThanAssignments #1163
Comments
Hello, @RobFaie and I been looking into this issue. |
I do not have enough C# knowledge to comment on the specific code, but I am a little confused that the focus shifted to the $All = $False
$Property | ForEach-Object {
If ($_ -eq "*") {$All = $True}
ElseIf ($_ -eq a calcultated property...
}
If ($All) {
Add any known property to output object that is not filled by a calculated property
}
... Besides this example, I also imaging that a state variable like a |
Currently the rule is limited to analysis within a scriptblock. Extending this functionality would require more thinking how to handle the scoping behaviour that PowerShell exhibits. |
Regardless if this is easy to detect or not, having warnings all over the place because the variable is "never used" is less than useless. I would almost suggest removing this rule until it is better at detection. Either that or assume everything is in the same scope for now, which would only cause things not in scope to get a false "pass". I think this would be a much better situation than what we currently have. |
This is a known issue due to the rule being limited to the scope of a scriptblock. If you look for issues with the Area - PSUseDeclaredVarsMoreThanAssignments label, you will find similar reports. Specifically, #1031 already covers Begin/Process/End blocks, therefore I suggest to close as duplicate. |
Another cmdlet I've stumbled upon that's impacted by this is Measure-Command. The |
Why does this fire on $global:variables if it only accounts for a single scriptblock? |
Issue is 5 years old and still no progress :-( |
Although issue #1031 and #1129 probably have the same cause, I have added a new bug report as it is (afaik) not directly related to the
Begin
,Process
andEnd
function blocks as suggested in there which brings the issue in a different perspective.Apparently for some cmdlets, PowerShell is invoked in the current scope but that is apparently not respected by the PSScriptAnalyzer.
Steps to reproduce
note that the result of the above is
$True
, meaning that the value of$Test
is actually assigned and changed to$True
within theForEach-Object
cmdlet.Expected behavior
No warning.
Actual behavior
Environment data
Will this eventually be resolved?
How can I nicely avoid the incorrect warning in an example like above?
The text was updated successfully, but these errors were encountered: