Repository navigation
SA1312: add a code fix that replaces underscore-only locals with discards - #4208
Merged
Merged
Conversation
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #4208 +/- ##
==========================================
- Coverage 97.58% 97.58% -0.01%
==========================================
Files 1275 1276 +1
Lines 134681 135074 +393
Branches 4309 4333 +24
==========================================
+ Hits 131428 131807 +379
- Misses 2112 2119 +7
- Partials 1141 1148 +7 🚀 New features to boost your workflow:
|
AArnott
marked this pull request as ready for review
October 9, 2026 18:35
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
Open
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 #2631
Adds
SA1312CodeFixProvider. For SA1312 diagnostics on a local whose name is only underscores (_,__, ...), it replaces the variable with a discard when that's legal and keeps the meaning. The analyzer is unchanged, so this adds no analysis cost. The cheap syntax checks run first, and the semantic checks only run inside the code fix, when it's asked for.When the fix is offered (C# 7+)
var _ = Foo();/var __ = Foo();_ = Foo();string _ = Foo();(identity conversion)_ = Foo();object _ = "x";,object _ = 1;,IComparable<string> ___ = Foo();(implicit reference or boxing conversion)_ = ...;out var __out _out var __when another symbol named_is in scopeout var _(still a discard)out int __out int _(keeps the type, which can matter for overload resolution)var (__, x) = ...var (_, x) = ...is int __,case string __:is int _,case string _:When it's not offered
ref/scopedlocal.constlocals aren't reported, andusingdeclarations andforeachvariables are exempt since SA1312: allow _ for foreach variables and using declarations #4203.long __ = 1;), typeless values (string __ = null;,default, lambdas and anonymous methods), user-defined implicit conversions, andstackalloc._ = ...would bind to another symbol named_in scope, such as a field, a parameter, or another local.forinitializers,catchdeclarations, and query range variables (no discard form here).How this coexists with the rename fix
RenameToLowerCaseCodeFixProvideralready declines to offer a fix when the name is only underscores. So for these names the discard fix is the only fix offered, and for every other name only the rename fix is offered. They never compete for the same diagnostic. The discard fix still has its own provider and equivalence key (SA1312CodeFixProvider), so Fix All for one never applies the other. It's a separate provider becauseRenameToLowerCaseCodeFixProvideris shared with SA1306 and SA1313.Tests
All in
SA1312CSharp7UnitTests(C# 7 is the lowest version with discards), and inherited by C# 8–15:var, explicit types, reference and boxing conversions, comments and trivia preserved),out var,out T, deconstruction,isandcasepatterns.out var _is kept when a field_is in scope, and the fix isn't offered forvar __ = this._;there.for,catch, query,reflocal), plus C# 6.Design notes
_in the same scope (var _ = A(); var __ = B();) can't become_ = B();while_is still a local. Fix All therefore takes two passes there. That's deliberately conservative._becomes a real discard syntax node. No discard syntax factory is needed (the product builds against Roslyn 1.2.1).