Skip to content

SA1309: avoid out-variable name collisions - #4275

Merged
AArnott merged 3 commits into
masterfrom
aarnott-sa1309-collision
Oct 13, 2026
Merged

AArnott merged 3 commits into
masterfrom
aarnott-sa1309-collision

Conversation

@AArnott

@AArnott AArnott commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

Fixes #2939

SA1309's rename fix could change _documentIndex.TryGetValue(document, out int documentIndex) into an unqualified field reference hidden by the inline out variable, producing CS0844.

Qualify references that collide with locals or parameters before invoking Roslyn rename: this. for instance fields and the containing type for static fields. Preserve existing receivers and comments, including references in other documents/partial declarations. The change is confined to SA1309's code fix; analyzer performance is unchanged.

Added regression coverage in SA1309UnitTests for the exact inline-out collision on C# 7+, a C# 6-compatible out-local variant, static fields, non-colliding names, generic containing types, cross-document parameter collisions, existing qualification, and comment preservation. The regression test fails on master (5fb0ffaf999434bf5b78b7c8bb86c8334ac46b37) with the reported bad fixed-source diff before the product change. Tests assert exact fixed source and retain compiler-error checking, and exercise single fixes and Fix All. Updated all three documentation editions.

Validation with SDK 10.0.401:

  • dotnet build -c Release -maxcpucount:1: zero warnings and errors.
  • All SA1309 suites pass in Debug on net10.0 across C# 6–15 (16 tests in C# 6/7; 18 in each later project).
  • SA1309 suites pass in Release on net10.0 in the lowest and latest projects (16 and 18 tests).
  • SA1309 suites pass in Debug on net472 in the lowest and latest projects (16 and 18 tests).

Opened as a draft; CI has not yet completed.

Qualify field references that collide with local variables or parameters before invoking Roslyn rename. Preserve existing receivers and trivia, including references in other partial declarations.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@AArnott
AArnott marked this pull request as ready for review October 13, 2026 15:50
Copilot AI balanced review requested due to automatic review settings October 13, 2026 15:50

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Initializer member designators and inherited static fields on generic bases can produce invalid fixed code.

1 open finding
What changed in this PR

Updates SA1309’s code fix to qualify field references that would collide after renaming.

Changes:

  • Adds collision-aware field qualification.
  • Adds C# 6–15 regression coverage.
  • Documents the behavior in three editions.
File Description
test/​StyleCop.Analyzers.Test/​NamingRules/​SA1309UnitTests.cs Adds rename-collision tests.
src/​StyleCop.Analyzers.CodeFixes/​NamingRules/​SA1309CodeFixProvider.cs Qualifies conflicting field references before rename.
documentation/​SA1309.md Documents qualification behavior.
documentation/​ru-ru/​SA1309.md Updates Russian documentation.
documentation/​cs-cz/​SA1309.md Updates Czech documentation.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

…vers

Exclude contextual initializer member names from collision qualification. Bind static receivers at each reference so inherited fields retain constructed generic type arguments. Add object/with initializer and inherited static regression tests.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 13, 2026 16:03

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Qualification still mishandles property-pattern designators and assignments inside array or collection initializers.

1 open finding
1 resolved since last review

🧠 Review effort: Balanced

@codecov

codecov Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.83268% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.62%. Comparing base (5fb0ffa) to head (afc7e94).
⚠️ Report is 10 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4275      +/-   ##
==========================================
+ Coverage   97.61%   97.62%   +0.01%     
==========================================
  Files        1285     1287       +2     
  Lines      137307   138008     +701     
  Branches     4451     4509      +58     
==========================================
+ Hits       134029   134728     +699     
+ Misses       2113     2111       -2     
- Partials     1165     1169       +4     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Leave property-pattern paths unqualified and restrict initializer exclusions to object and with member designators. Cover simple/extended patterns and assignments in array and collection initializers.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 13, 2026 16:14

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Needs a closer look

The semantic rewrite appears well covered, but the full C# 6–15 CI and documentation jobs are still pending.

0 open findings

1 resolved since last review

🧠 Review effort: Balanced

This was referenced Nov 23, 2026
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.

SA1309 should consider clashes with out vars when renaming

2 participants