Add MA0191: Detect null! and default! usage#1081
Merged
Conversation
Agent-Logs-Url: https://github.com/meziantou/Meziantou.Analyzer/sessions/33cd3b96-962c-4ec5-b912-6711a948990a Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Add analyzer for detecting null! and default! usage
Add MA0191: Detect null-forgiving operator usage
Mar 31, 2026
meziantou
reviewed
Mar 31, 2026
|
|
||
| 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. |
Owner
There was a problem hiding this comment.
@copilot It should only report usage of null! and default!. Other usages are valid
Contributor
Author
There was a problem hiding this comment.
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.
…xpressions Agent-Logs-Url: https://github.com/meziantou/Meziantou.Analyzer/sessions/be41a4ca-4bce-4cc6-99b9-81690be323f3 Co-authored-by: meziantou <509220+meziantou@users.noreply.github.com>
Copilot
AI
changed the title
Add MA0191: Detect null-forgiving operator usage
Add MA0191: Detect null! and default! usage
Mar 31, 2026
This was referenced Mar 31, 2026
Merged
This was referenced Apr 13, 2026
Open
This was referenced Apr 20, 2026
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.
The null-forgiving operator (
!) suppresses nullable warnings and is often a code smell — especially when used asnull!ordefault!to defer initialization. This adds a new opt-in rule to flag such usages.New rule: MA0191 — Do not use the null-forgiving operator
null!,default!, anddefault(T)!viaSyntaxKind.SuppressNullableWarningExpression!on non-null/default expressions are not reported (e.g.someExpression!is valid).editorconfigFiles
RuleIdentifiers.cs— addsMA0191constantDoNotUseNullForgivenessAnalyzer.cs— syntax-based analyzerdocs/Rules/MA0191.md— rule documentationDocumentationGenerator