Skip to content
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

PSScriptAnalyzer Does Not Recognize Variable Use in ForEach-Object or .net ForEach #3910

Closed
4 of 6 tasks
JAK1047 opened this issue Apr 7, 2022 · 1 comment
Closed
4 of 6 tasks
Labels
Issue-Bug A bug to squash. Resolution-External Will close automatically.

Comments

@JAK1047
Copy link

JAK1047 commented Apr 7, 2022

Prerequisites

  • I have written a descriptive issue title.
  • I have searched all issues to ensure it has not already been reported.
  • I have read the troubleshooting guide.
  • I am sure this issue is with the extension itself and does not reproduce in a standalone PowerShell instance.
  • I have verified that I am using the latest version of Visual Studio Code and the PowerShell extension.
  • If this is a security issue, I have read the security issue reporting guidance.

Summary

I'm not entirely sure if this lies more with PSScriptAnalyzer itself or with a combination of it and the extension. A common thing I will do is set a boolean in a ForEach\ForEach-Object loop that I use for validation later in the script. If I set the value in either a ForEach-Object loop or a .net ForEach loop then the analyzer will inform me the variable is never used. If I set it in a ForEach loop it works fine.

PowerShell Version

Name                           Value
----                           -----
PSVersion                      7.2.2
PSEdition                      Core
GitCommitId                    7.2.2
OS                             Microsoft Windows 10.0.17763
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visual Studio Code Version

1.66.0
e18005f0f1b33c29e81d732535d8c0e47cafb0b5
x64

Extension Version

ms-vscode.powershell@2021.12.0

Steps to Reproduce

[bool]$Foo = $false

(1..3) | ForEach-Object {
    $Foo = $true
}

ForEach ($Object in (1..3)) {
    $Foo = $true
}

(1..3).ForEach({
    $Foo = $true
})

If ($Foo) {
    'Do thing'
}

Visuals

Screenshot 2022-04-07 105406

Logs

No response

@JAK1047 JAK1047 added the Issue-Bug A bug to squash. label Apr 7, 2022
@ghost ghost added the Needs: Triage Maintainer attention needed! label Apr 7, 2022
@SeeminglyScience
Copy link
Collaborator

Thanks for the report! This issue is tracked in PowerShell/PSScriptAnalyzer#1641

@SeeminglyScience SeeminglyScience added Resolution-External Will close automatically. and removed Needs: Triage Maintainer attention needed! labels Apr 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Bug A bug to squash. Resolution-External Will close automatically.
Projects
None yet
Development

No branches or pull requests

2 participants