Skip to content

SA1312: add a code fix that replaces underscore-only locals with discards - #4208

Merged
AArnott merged 2 commits into
masterfrom
fix-2631-sa1312-discard-codefix
Oct 9, 2026
Merged

AArnott merged 2 commits into
masterfrom
fix-2631-sa1312-discard-codefix

Conversation

@AArnott

@AArnott AArnott commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

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+)

Before After
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 scope out 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

  • The variable is referenced anywhere (read or written). The check covers the whole containing member, or the whole file for top-level statements.
  • The declaration has more than one declarator, has no initializer, or is a ref/scoped local. const locals aren't reported, and using declarations and foreach variables are exempt since SA1312: allow _ for foreach variables and using declarations #4203.
  • The discard would get a different value or conversion: numeric conversions (long __ = 1;), typeless values (string __ = null;, default, lambdas and anonymous methods), user-defined implicit conversions, and stackalloc.
  • _ = ... would bind to another symbol named _ in scope, such as a field, a parameter, or another local.
  • for initializers, catch declarations, and query range variables (no discard form here).
  • C# 6 and earlier.

How this coexists with the rename fix

RenameToLowerCaseCodeFixProvider already 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 because RenameToLowerCaseCodeFixProvider is shared with SA1306 and SA1313.

Tests

All in SA1312CSharp7UnitTests (C# 7 is the lowest version with discards), and inherited by C# 8–15:

  • Offered: local declarations (var, explicit types, reference and boxing conversions, comments and trivia preserved), out var, out T, deconstruction, is and case patterns.
  • out var _ is kept when a field _ is in scope, and the fix isn't offered for var __ = this._; there.
  • Not offered: 14 cases (referenced local or pattern variable, multiple declarators, no initializer, numeric, null and user-defined conversions, lambda, comment between type and name, for, catch, query, ref local), plus C# 6.
  • Fix All: across several methods. The test framework checks document, project and solution Fix All, plus applying the fixes one at a time.

Design notes

  • A local declaration that comes after another local named _ 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.
  • The fix changes text, and Roslyn re-parses it, so _ becomes a real discard syntax node. No discard syntax factory is needed (the product builds against Roslyn 1.2.1).

@codecov

codecov Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.67430% with 17 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.58%. Comparing base (ff4ed74) to head (078ee34).
⚠️ Report is 2 commits behind head on master.

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:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@AArnott
AArnott marked this pull request as ready for review October 9, 2026 18:35
@AArnott
AArnott merged commit 61b4c85 into master Oct 9, 2026
25 checks passed
@AArnott
AArnott deleted the fix-2631-sa1312-discard-codefix branch October 9, 2026 19:06
This was referenced Nov 11, 2026
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.

Update SA1312 code fix to replace var _ with _

1 participant