Skip to content

PSUseDeclaredVarsMoreThanAssignment not correctly handling array adds and final assignment #1103

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

Closed
HowardWolosky opened this issue Nov 21, 2018 · 4 comments

Comments

@HowardWolosky
Copy link

This is a continuation of Issue #699 which I previously opened. It was closed as being resolved, but this issue remains.

Steps to reproduce

$resourceParams = @('a1', 'b1', c1')
$params = Get-Variable -Name $resourceParams -ErrorAction SilentlyContinue |
                  ForEach-Object { $m = @{} } { $m[$_.Name] = $_.Value } { $m } # foreach begin{} process{} end{}

Expected behavior

No reported issue

Actual behavior

PSUseDeclaredVarsMoreThanAssignments    Warning    The variable 'm' is assigned but never used.

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      5.1.18287.1001
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.18287.1001
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

> (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }
1.17.1
1.10.0
1.4.0
@JeremyTBradshaw
Copy link

+1, I notice this issue too. Another scenario that exposes it is this:

if ( 1+1 = 2) {$a = 'b'} else {$a = 'c'}
Write-Output $a

Since $a is declared twice but used once, a problem is reported.

@Panzerbjrn
Copy link

Panzerbjrn commented Jul 4, 2019

I just wanted to +1 this.
My code:
`
$Roles = Get-SCUserRoleMembership -UserName $Username
$Roles | ForEach-Object {IF ($_.UserRoleProfile -eq "Administrator") {$IsAdmin = $True}}

IF ($IsAdmin -ne $True)
{
"User is not admin"
}`

$ISAdmin is both declared and then used, but I still get "The variable 'IsAdmin' is assigned but never used."

@jonfortescue
Copy link

jonfortescue commented Jul 11, 2019

+1, I'm seeing this as well.

        $found = $false
        $repos | % {
            if ($_.Item1 -ieq $r) {
                $found = $true
            }
        }
        if (-Not $found) {
            throw "..."
        }

$found is said to be declared but never used within the ForEach-Object scope.

@rjmholt
Copy link
Contributor

rjmholt commented Feb 9, 2021

Duplicate of #1163

@rjmholt rjmholt marked this as a duplicate of #1163 Feb 9, 2021
@rjmholt rjmholt closed this as completed Feb 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants