-
Notifications
You must be signed in to change notification settings - Fork 393
PSUseDeclaredVarsMoreThanAssignment not correctly handling array adds #699
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
There are some new warnings coming up when checking StoreBroker with newer versions of [PSScriptAnalyzer](https://github.com/PowerShell/PSScriptAnalyzer). * `PSUseDeclaredVarsMoreThanAssignments` - We had some instances where a variable was being assigned to but never used. For the API instances, we now capture to $null in the instances where we don't want the helper method's results being returned to the user. * `PSUseShouldProcessForStateChangingFunctions` - Two PackageTool methods use the verb "Remove" but have no need for providing ShouldProcess support. Those instances are now suppressed. All remaining warnings are false positives of `PSUseDeclaredVarsMoreThanAssignments`. I have opened two different issues against PSScriptAnalyzer to track these false positives: * [PSUseDeclaredVarsMoreThanAssignment not correctly handling global vars assigned to within functions](PowerShell/PSScriptAnalyzer#698) * [PSUseDeclaredVarsMoreThanAssignment not correctly handling array adds](PowerShell/PSScriptAnalyzer#699) Updated CONTRIBUTING.md to remind users to keep the analyzer module up-to-date. Additionally, the links in PDP.md were invalid. Those have now been fixed.
There are some new warnings coming up when checking StoreBroker with newer versions of [PSScriptAnalyzer](https://github.com/PowerShell/PSScriptAnalyzer). * `PSUseDeclaredVarsMoreThanAssignments` - We had some instances where a variable was being assigned to but never used. For the API instances, we now capture to $null in the instances where we don't want the helper method's results being returned to the user. * `PSUseShouldProcessForStateChangingFunctions` - Two PackageTool methods use the verb "Remove" but have no need for providing ShouldProcess support. Those instances are now suppressed. All remaining warnings are false positives of `PSUseDeclaredVarsMoreThanAssignments`. I have opened two different issues against PSScriptAnalyzer to track these false positives: * [PSUseDeclaredVarsMoreThanAssignment not correctly handling global vars assigned to within functions](PowerShell/PSScriptAnalyzer#698) * [PSUseDeclaredVarsMoreThanAssignment not correctly handling array adds](PowerShell/PSScriptAnalyzer#699) Updated CONTRIBUTING.md to remind users to keep the analyzer module up-to-date. Additionally, the links in PDP.md were invalid. Those have now been fixed.
bump Is this fixed already? I still see these false positives. PS>gmo PSScriptAnalyzer
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Script 1.15.0 PSScriptAnalyzer {Get-ScriptAnalyzerRule, Invoke-Formatter, Invoke-ScriptAn...
PS>Invoke-ScriptAnalyzer -ScriptDefinition { $jobs=@()
>> $ipList | ForEach-Object {
>> $running = @(Get-Job | Where-Object { $_.State -eq 'Running' })
>> if ($running.Count -ge 10)
>> {
>> $running | Wait-Job -Any | Out-Null
>> }
>> Write-Verbose "Discovering ip $_"
>> $jobs += Start-Job -ScriptBlock $cmd -ArgumentList $_, $credential, $deepDiscover
>> }
>> Wait-Job -Job $jobs | Out-Null
>> Receive-Job -Job $jobs
>> }.ToString()
RuleName Severity ScriptName Line Message
-------- -------- ---------- ---- -------
PSUseDeclaredVarsMoreThanAssignment Warning 9 The variable 'jobs' is assigned but never used.
s
|
It's not the array, it's the ForEach loop It also happens if you define a variable in the begin block of the ForEach, like this: $Doubles = 1..100 | ForEach { $collect = @() } { $Collect += $_ * 2 } { $Collect } Or when you use a hashtable like: $Requirement = Get-Item .
[hashtable]$hash = $Requirement.PSObject.Properties.Where{$_.Value} | ForEach-Object { $ht = @{} } { $ht.Add($_.Name, $_.value) } { $ht }
$hash |
@Jaykul Actually, it is a problem of both the |
Given the following code
I get the following warning:
I realize that this generates a new array each time, but it is both being assigned to and read, so this appears to be a false positive.
Another similar scenario:
The text was updated successfully, but these errors were encountered: