Skip to content

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

Closed
@sobjornstad

Description

@sobjornstad

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions