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

"Variable assigned but never used" false positive in a script block passed to ForEach-Object #1942

Closed
ForNeVeR opened this issue Oct 1, 2023 · 3 comments

Comments

@ForNeVeR
Copy link

ForNeVeR commented Oct 1, 2023

Steps to reproduce

(Initially reported in downstream repo, see ant-druha/intellij-powershell#96, but with my adjustments to better demonstrate the behavior.)

(1..3) | ForEach-Object {
    $Hashes = "test"
} {
    $Hashes += "Some-String "
} {
    $Hashes
}

Expected behavior

There should be no warnings about the initial value of the $Hashes variable not being used in this code.

To demonstrate that it is in fact used, run the code, and you'll see output testSome-String Some-String Some-String: test value was used.

Actual behavior

The variable 'Hashes' is assigned but never used.PSScriptAnalyzer(PSUseDeclaredVarsMoreThanAssignments)

Environment data

While it was reported in intellij-powershell where we bundle v1.21.0, I was also able to reproduce the same in VSCode.

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      7.3.7
PSEdition                      Core
GitCommitId                    7.3.7
OS                             Microsoft Windows 10.0.22621
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

> (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }
1.21.0

Additional Information

From a quick look, it may be related to #1472.

@iRon7
Copy link

iRon7 commented Oct 4, 2023

Duplicate with #1163?

@ForNeVeR
Copy link
Author

ForNeVeR commented Oct 4, 2023

I think it's a duplicate. Thank you!

@ForNeVeR ForNeVeR closed this as not planned Won't fix, can't repro, duplicate, stale Oct 4, 2023
@iRon7
Copy link

iRon7 commented Oct 4, 2023

Anyways, I think that these two rules (PSUseDeclaredVarsMoreThanAssignments and PSReviewUnusedParameter) have been overcomplicated the issue were it could just be: don't warn if the concerned variable can be found back (anywhere) in the script (even this might not capture everything, it is better than a false-possitive)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants