Skip to content

False positive on UseDeclaredVarsMoreThanAssignments with Foreach-Object -Begin #1129

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
sobjornstad opened this issue Jan 17, 2019 · 3 comments

Comments

@sobjornstad
Copy link

Steps to reproduce

Get-SomeKeyValuePairs |
    Where-Object { some validation script }
    Foreach-Object -Begin {
        $failedValidation = [System.Collections.ArrayList]@()
    } -Process {
        $failedValidation.Add($_.Name)
    } -End {
        if (@($failedValidation).Count -gt 0) {
            throw "The following required parameters were empty: $($failedValidation -join ', ')."
        }
    }

(Note: There are much better ways to write this, one of which I am now using; this code was just my first shot at the problem.)

Expected behavior

No output from the analyzer.

Actual behavior

I receive a warning indicating "The variable 'failedValidation' is assigned but never used."

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      5.1.15063.1563
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.15063.1563
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
@bergmeister
Copy link
Collaborator

Duplicate of #1031

@sba923
Copy link

sba923 commented Jun 26, 2019

Issue still present in 1.18.1 ;-(

@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

4 participants