Repository navigation
SA1309: avoid out-variable name collisions - #4275
Merged
Merged
Conversation
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>
There was a problem hiding this comment.
🟡 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>
Codecov Report❌ Patch coverage is 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:
|
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>
This was referenced Nov 11, 2026
Bump StyleCop.Analyzers from 1.2.0-beta.556 to 1.3.180
microsoftgraph/aspnetcore-webhooks-sample#436
Open
Closed
This was referenced Nov 23, 2026
[DPDBT] Bump Microsoft.CodeAnalysis.Analyzers and 6 others
sergey-rybalkin/CommandLine.Generators#63
Open
Open
Open
Closed
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.


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
SA1309UnitTestsfor 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.net10.0across C# 6–15 (16 tests in C# 6/7; 18 in each later project).net10.0in the lowest and latest projects (16 and 18 tests).net472in the lowest and latest projects (16 and 18 tests).Opened as a draft; CI has not yet completed.