Repository navigation
SA1408: Include null-coalescing precedence - #4323
Merged
Merged
Conversation
Require explicit parentheses when null-coalescing operators are mixed with logical operators. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🟢 Approval recommended
The focused syntax-only change has regression coverage, consistent documentation, and no identified blocking issues.
0 open findings
What changed in this PR
Extends SA1408 to detect ambiguous precedence when ?? is mixed with && or ||, addressing #3167.
Changes:
- Adds null-coalescing analysis while allowing same-operator chains.
- Adds diagnostic, code-fix, and parenthesized-expression regression tests.
- Updates English and three localized rule descriptions.
| File | Description |
|---|---|
| test/StyleCop.Analyzers.Test/MaintainabilityRules/SA1408UnitTests.cs | Adds null-coalescing regression coverage. |
| src/StyleCop.Analyzers/MaintainabilityRules/SA1408ConditionalExpressionsMustDeclarePrecedence.cs | Includes ?? in precedence analysis. |
| documentation/zh-cn/SA1408.md | Adds Chinese guidance and examples. |
| documentation/SA1408.md | Documents null-coalescing precedence. |
| documentation/ru-ru/SA1408.md | Adds Russian guidance and examples. |
| documentation/cs-cz/SA1408.md | Adds Czech guidance and examples. |
🧠 Review effort: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #4323 +/- ##
=======================================
Coverage 97.63% 97.63%
=======================================
Files 1321 1321
Lines 144864 144930 +66
Branches 4873 4874 +1
=======================================
+ Hits 141437 141504 +67
Misses 2173 2173
+ Partials 1254 1253 -1 🚀 New features to boost your workflow:
|
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
This was referenced Dec 1, 2026
Open
Open
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 #3167
SA1408 currently misses expressions that mix
??with&&or||, which can make the actual grouping surprising and lead to incorrect assumptions about which conditions are evaluated. The analyzer now treats??as a conditional operator and reports mixed operators unless parentheses make the grouping explicit. Same-operator??chains remain allowed, and the existing logical and pattern behavior is unchanged.Regression tests for both logical operators failed on the unchanged analyzer with no diagnostics. After the change, all 14 SA1408 tests passed on the C# 6 compiler and all 18 passed on C# 15. The new analyzer callback is registered only for coalescing expressions; other syntax kinds incur no additional analyzer work. Documentation has been updated in English and the existing Czech, Russian, and Chinese translations.