GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Flag unused variables in multiple assignments in UseDeclaredVarsMoreThanAssignments by 0x000NULL · Pull Request #2227 · PowerShell/PSScriptAnalyzer

Conversation

@0x000NULL

@0x000NULL Ethan Aldrich (0x000NULL) commented Oct 19, 2026

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@0x000NULL
Flag unused variables in multiple assignments
53cc1aa
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 balanced review requested due to automatic review settings October 19, 2026 14:49
@0x000NULL
Ethan Aldrich (0x000NULL) requested review from a team and Christoph Bergmeister (bergmeister) as code owners October 19, 2026 14:49
Copilot started reviewing on behalf of Ethan Aldrich (0x000NULL) October 19, 2026 14:50
View session
Copilot AI reviewed Oct 19, 2026
View reviewed changes

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

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
  • Medium severity Repeated writes incorrectly counted as variable uses · New
  • Medium severity Index references incorrectly classified as assignment targets · New
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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Rules/UseDeclaredVarsMoreThanAssignments.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Rules/UseDeclaredVarsMoreThanAssignments.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

@0x000NULL

Ethan Aldrich (0x000NULL) commented Oct 19, 2026

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@0x000NULL
Only treat assigned variables of a multiple assignment as writes
f875f8f
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.
@bergmeister
Christoph Bergmeister (bergmeister) requested a balanced review from Copilot October 22, 2026 21:41
Copilot started reviewing on behalf of Christoph Bergmeister (bergmeister) October 22, 2026 21:42
View session
Copilot AI reviewed Oct 22, 2026
View reviewed changes

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

🟡 Changes recommended

Typed targets in repeated assignments are misclassified as reads, suppressing valid unused-variable warnings.

1 open issue
  • Medium severity Handle typed assignment targets in variable reassignment matching · New
2 resolved since last review
  • Medium severity Index references incorrectly classified as assignment targets
  • Medium severity Repeated writes incorrectly counted as variable uses

Review effort: Balanced

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Rules/UseDeclaredVarsMoreThanAssignments.cs
Comment on lines +245 to +246
return variableAst.Parent as AssignmentStatementAst
?? (variableAst.Parent as ArrayLiteralAst)?.Parent as AssignmentStatementAst;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

Copilot code review Copilot
Copilot review effort
Applies to this pull request for everyone.Learn more about Copilot code review.
Copilot left review comments
@bergmeister Christoph Bergmeister (bergmeister) Awaiting requested review from bergmeister bergmeister is a code owner

At least 1 approving review is required to merge this pull request.

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

PSUseDeclaredVarsMoreThanAssignments and assigning multiple variables

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@0x000NULL
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.