Repository navigation
Flag unused variables in multiple assignments in UseDeclaredVarsMoreThanAssignments - #2227
Open
Ethan Aldrich (0x000NULL) wants to merge 2 commits into
Open
Ethan Aldrich (0x000NULL) wants to merge 2 commits into
Ethan Aldrich (0x000NULL) wants to merge 2 commits into
Conversation
UseDeclaredVarsMoreThanAssignments only looked at assignments whose left-hand side is a single variable, so `$a, $b = $null` was never flagged. Treat each variable of an array literal on the left-hand side like a single-variable assignment, and stop at the first matching variable when checking whether a reference is the assignment itself. Fix PowerShell#2164
Ethan Aldrich (0x000NULL)
requested review from
a team and
Christoph Bergmeister (bergmeister)
as code owners
October 19, 2026 14:49
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Reference classification still mistakes repeated writes for uses and index reads for writes, producing missed and incorrect warnings.
2 open issues
What changed in this PR
Extends PSUseDeclaredVarsMoreThanAssignments to detect unused variables in multiple assignments, addressing #2164.
Changes:
- Tracks individual variables on multiple-assignment left-hand sides.
- Stops reference matching at the first match.
- Adds three regression tests for unused and used variables.
| File | Description |
|---|---|
| Tests/Rules/UseDeclaredVarsMoreThanAssignments.tests.ps1 | Tests multiple-assignment diagnostics. |
| Rules/UseDeclaredVarsMoreThanAssignments.cs | Adds array-target tracking and first-match handling. |
Review effort: Balanced
Comment on lines
+159
to
+161
| if (!assignmentsDictionary_OrdinalIgnoreCase.ContainsKey(variableName)) | ||
| { | ||
| assignmentsDictionary_OrdinalIgnoreCase.Add(variableName, assignmentAst); |
Comment on lines
+204
to
+207
| // A multiple assignment has several variables on the lhs, so stop at the first match | ||
| if (inAssignment) | ||
| { | ||
| break; |
Author
|
@microsoft-github-policy-service agree |
Reassigning a variable from a multiple assignment, as in `$a, $b = 1, 2; $a = 3`, was counted as a use, unlike `$a = 1; $a = 3`. Variables read inside another target, as in `$i, $x[$i] = 0, 42`, were counted as writes, so `$i` was wrongly flagged.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

PR Summary
PSUseDeclaredVarsMoreThanAssignmentsnow flags unused variables in a multiple assignment such as$a, $b = $null. Before, it only flagged assignments whose left-hand side was a single variable.[int]$a, $b = 1, 2are covered by the existingConvertExpressionAsthandling.$a, $b) as its extent, the same as the existing code. The suppression ID is the variable name, so suppressing by name still works.Fixes #2164
How tested:
Tests/Rules/UseDeclaredVarsMoreThanAssignments.tests.ps1:UseDeclaredVarsMoreThanAssignments.tests.ps1passes 32/32 and all ofTests/Rulespasses 1742, with 0 failed and 7 skipped (PowerShell 7.6.7, Windows 11).This only touches
AnalyzeScriptBlockAst, so it doesn't conflict with the expression-bodied member changes in #2182.PR Checklist
.cs,.ps1and.psm1files have the correct copyright header (no new files)