Skip to content

SA1408: Include null-coalescing precedence - #4323

Merged
AArnott merged 1 commit into
masterfrom
aarnott-sa1408-non-arithmetic-precedence
Oct 16, 2026
Merged

AArnott merged 1 commit into
masterfrom
aarnott-sa1408-non-arithmetic-precedence

Conversation

@AArnott

@AArnott AArnott commented Oct 16, 2026

Copy link
Copy Markdown
Contributor

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.

Require explicit parentheses when null-coalescing operators are mixed with logical operators.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@AArnott
AArnott marked this pull request as ready for review October 16, 2026 14:37
Copilot AI balanced review requested due to automatic review settings October 16, 2026 14:37

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 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.

@AArnott
AArnott merged commit 91b325e into master Oct 16, 2026
30 checks passed
@AArnott
AArnott deleted the aarnott-sa1408-non-arithmetic-precedence branch October 16, 2026 14:46
@codecov

codecov Bot commented Oct 16, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.57143% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 97.63%. Comparing base (8dc1343) to head (20f0604).
⚠️ Report is 1 commits behind head on master.

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:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

This was referenced Nov 11, 2026
This was referenced Dec 1, 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.

Non-arithmetic operator precedence

2 participants