PowerShell / PowerShell/PSScriptAnalyzer
unary not is fooling tracker of variable use when assignment is in foreach loop
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 2.2k
- Forks
- 414
- Avg merge
- 13h 1m
- Merged PRs (30d)
- 2
Description
Steps to reproduce
Get-AzAutomationSchedule $resourceGroup $automationAccount | ForEach-Object {
$PrimaryExists = $true
}
if (-not $PrimaryExists) {
Write-Output "hello"
}
Expected behavior
$PrimaryExists should not be flagged as assigned but never used.
Actual behavior
It is flagged.
Environment data
> $PSVersionTable
Name Value
---- -----
PSVersion 6.2.3
PSEdition Core
GitCommitId 6.2.3
OS Linux 4.15.0-1056-oem #65-Ubuntu SMP Thu Sep 12…
Platform Unix
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.18.3
Notes
I tried to create a more concise example, but the assignment needs to be a foreach { } at the end of a pipeline to exhibit the error.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the supplied PowerShell pipeline and checking the analyzer diagnostic for the assignment inside the trailing ForEach-Object block. Trace the variable-use analysis that produces the assigned-but-unused warning, then add a regression test for this example and confirm the warning is no longer reported while ordinary unused assignments remain flagged.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- powershell
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100