Skip to content

PSUseDeclaredVarsMoreThanAssignment not correctly handling array adds #699

Closed

Description

Given the following code

$msg = @()
@("a", "b") | ForEach-Object { $msg += $_ }
Write-Information $msg -InformationAction Continue

I get the following warning:

  • The variable 'msg' is assigned but never used. (line 2)

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:

$params = (@{"a" = 1; "b" = 2}).GetEnumerator() |
    ForEach-Object { $m = @{} } { $m[$_.Name] = $_.Value } { $m } # foreach begin{} process{} end{}
$params
  • The variable 'm' is assigned but never used. (line 2)

Activity

  1. DexterPOSH commented on Jul 6, 2017

    @DexterPOSH

    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
    
    
  2. Jaykul commented on Dec 22, 2017

    @Jaykul
    Contributor

    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
  3. bergmeister commented on Mar 14, 2018

    @bergmeister
    Collaborator

    Joel Bennett (@Jaykul) Actually, it is a problem of both the += operator being treated as assignment (not usage) , I will therefore extract your examples (that I think are valid) because I found a solution to the originally raised issue that is due to +=

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions