Skip to content

Flag unused variables in multiple assignments in UseDeclaredVarsMoreThanAssignments - #2227

Open
Ethan Aldrich (0x000NULL) wants to merge 2 commits into
PowerShell:mainfrom
0x000NULL:fix/2164-multi-assign
Open

Ethan Aldrich (0x000NULL) wants to merge 2 commits into
PowerShell:mainfrom
0x000NULL:fix/2164-multi-assign

Conversation

@0x000NULL

Copy link
Copy Markdown

PR Summary

PSUseDeclaredVarsMoreThanAssignments now 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.

  • Each variable in an array literal on the left-hand side is now treated like a single-variable assignment. Typed elements such as [int]$a, $b = 1, 2 are covered by the existing ConvertExpressionAst handling.
  • When the rule checks whether a variable reference is the assignment itself, it now stops at the first matching variable. Otherwise, with several variables on the left-hand side, the last one would win.
  • Each finding uses the whole left-hand side ($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:

  • Added three Pester cases to Tests/Rules/UseDeclaredVarsMoreThanAssignments.tests.ps1:
    • both variables flagged
    • only the unused one flagged
    • none flagged when both are used
  • The two "flagged" cases fail without the fix: expected 2 got 0, and expected 1 got 0.
  • With the fix, UseDeclaredVarsMoreThanAssignments.tests.ps1 passes 32/32 and all of Tests/Rules passes 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

  • PR has a meaningful title
  • Summarized changes
  • Change is not breaking
  • Make sure all .cs, .ps1 and .psm1 files have the correct copyright header (no new files)
  • Make sure you've added a new test if existing tests do not effectively test the code changed and/or updated documentation
  • This PR is ready to merge and is not Work in Progress.

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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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;
@0x000NULL

Copy link
Copy Markdown
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.
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

Successfully merging this pull request may close these issues.

PSUseDeclaredVarsMoreThanAssignments and assigning multiple variables

2 participants