Skip to content

Add analyzer 'Unnecessary null coalescing' - #1826

Open
josefpihrt wants to merge 3 commits into
mainfrom
feature/1658-unnecessary-null-coalescing
Open

Add analyzer 'Unnecessary null coalescing'#1826
josefpihrt wants to merge 3 commits into
mainfrom
feature/1658-unnecessary-null-coalescing

Conversation

@josefpihrt

Copy link
Copy Markdown
Collaborator
  • I've read the contributing guide
  • Bug fixes / features have an issue, or new analyzers / refactorings / fixes were discussed first
  • Unit tests added for bug fixes / features
  • Changelog updated
  • Build and tests pass locally (also verified by CI)

Summary

  • Add RCS1269 to flag ?? and ??= when nullable flow analysis shows the left operand is a never-null reference type.
  • Code fix keeps the left operand, removes a statement-level ??=, or replaces an expression-level ??= with the left side.
  • Skip cases already covered by RCS1143 (literals, this, new, constants) and skip nested ?? that would conflict with FixAll.

Test plan

  • dotnet test filter FullyQualifiedName~RCS1269
  • Confirm #nullable enable samples from the issue (errors ?? [], _context ??= ..., property ??=)
  • Confirm no diagnostic without nullable context, for string?, and for RCS1143 overlap ("" ?? "a", this ?? x)

Related issue

Fixes #1658

Made with Cursor

Josef Pihrt and others added 3 commits August 16, 2026 21:27
Flag ?? and ??= when nullable flow analysis shows the left operand is never null.

Co-authored-by: Cursor <cursoragent@cursor.com>
Replace embedded coalesce-assignment statements with an empty block so the fix stays valid inside if/while/for. Drop the implicit-new skip that RCS1143 does not cover, and restore the unrelated config URL change.

Co-authored-by: Cursor <cursoragent@cursor.com>
Register only on C# 8+ and return before GetTypeInfo unless GetNullableContext reports AnnotationsEnabled at the node.

Co-authored-by: Cursor <cursoragent@cursor.com>
@josefpihrt josefpihrt mentioned this pull request Aug 21, 2026
2 tasks
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.

New analyzer for unnecessary null coalescing

1 participant