Skip to content

Add MA0191: Detect null! and default! usage#1081

Merged
meziantou merged 3 commits intomainfrom
copilot/add-analyzer-for-null-forgiving-operator
Mar 31, 2026
Merged

Add MA0191: Detect null! and default! usage#1081
meziantou merged 3 commits intomainfrom
copilot/add-analyzer-for-null-forgiving-operator

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 31, 2026

The null-forgiving operator (!) suppresses nullable warnings and is often a code smell — especially when used as null! or default! to defer initialization. This adds a new opt-in rule to flag such usages.

New rule: MA0191 — Do not use the null-forgiving operator

  • Detects null!, default!, and default(T)! via SyntaxKind.SuppressNullableWarningExpression
  • Other uses of ! on non-null/default expressions are not reported (e.g. someExpression! is valid)
  • Disabled by default — enable via .editorconfig
  • Severity: Warning, Category: Design
// MA0191 reported on these
HttpClient _httpClient = null!;
string TempDir { get; set; } = default!;
string _value = default(string)!;

// MA0191 not reported (other uses of ! are valid)
_ = model.Value!.Length;
dotnet_diagnostic.MA0191.severity = warning

Files

  • RuleIdentifiers.cs — adds MA0191 constant
  • DoNotUseNullForgivenessAnalyzer.cs — syntax-based analyzer
  • docs/Rules/MA0191.md — rule documentation
  • Auto-generated: README, editorconfig pack files updated via DocumentationGenerator

Copilot AI changed the title [WIP] Add analyzer for detecting null! and default! usage Add MA0191: Detect null-forgiving operator usage Mar 31, 2026
Copilot AI requested a review from meziantou March 31, 2026 17:30
Comment thread docs/Rules/MA0191.md Outdated

The null-forgiving operator (`!`) suppresses nullable warnings from the compiler. While it can be necessary in some cases (such as when using dependency injection, deserialization, or value binding where the compiler cannot determine that a value will be non-null at runtime), its usage is often a code smell that hides potential null reference issues.

This rule reports all usages of the null-forgiving operator, including `null!`, `default!`, and applying `!` to any expression.
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

@copilot It should only report usage of null! and default!. Other usages are valid

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated in 2a79bf7 — the analyzer now only reports null!, default!, and default(T)!. Other uses of the null-forgiving operator (e.g. model.Value!) are no longer flagged.

Copilot AI changed the title Add MA0191: Detect null-forgiving operator usage Add MA0191: Detect null! and default! usage Mar 31, 2026
Copilot AI requested a review from meziantou March 31, 2026 17:37
@meziantou meziantou marked this pull request as ready for review March 31, 2026 18:12
@meziantou meziantou merged commit b2b0617 into main Mar 31, 2026
13 checks passed
@meziantou meziantou deleted the copilot/add-analyzer-for-null-forgiving-operator branch March 31, 2026 18:12
This was referenced Apr 13, 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.

Detect usages of default! / null! / Null forgiving operator

2 participants