Skip to content

SA1500/SA1513: Allow continuations after switch expression closing braces - #4334

Merged
AArnott merged 2 commits into
masterfrom
aarnott-switch-expression-brace-spacing
Oct 17, 2026
Merged

AArnott merged 2 commits into
masterfrom
aarnott-switch-expression-brace-spacing

Conversation

@AArnott

@AArnott AArnott commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

Switch expressions can continue after their closing brace, but SA1500 and SA1513 currently require a new line or blank line before operators such as ??.

Fixes #4332

Changes

  • Allow expression continuations on the same line as a switch expression's closing brace, without requiring a following blank line.
  • Preserve SA1500's existing multiline layout requirements: the opening brace still occupies its own line, and the closing brace cannot share a line with the final arm. Entirely single-line switch expressions remain allowed.
  • Preserve trailing continuation code when the SA1500 code fix moves a misplaced closing brace.
  • Keep SA1012/SA1013 whitespace requirements unchanged and update the English, Czech, Russian, and Chinese rule documentation.

This narrows the policy introduced by #4035 rather than exempting switch expressions from SA1500 entirely. Member and element access require parentheses around the switch expression in C#; regression coverage uses those valid forms.

Verification

The reported null-coalescing cases failed against the unchanged analyzers with SA1500/SA1513 diagnostics before the product changes. Added coverage for other operators, comments, whitespace and spacing fixes, single-line expressions, and closing braces sharing the final arm's line with and without a trailing comma.

Built the C# 15 test project and its inherited projects with SDK 10.0.402, with zero warnings or errors. The complete SA1500, SA1513, SA1012, and SA1013 suites pass under C# 6 and every compiler from C# 8 through C# 15.

Performance

No new callbacks, semantic-model queries, tree walks, or allocations. SA1500 adds a token/parent-kind guard within its existing brace callback; SA1513 adds a parent-kind check for closing braces and skips the remaining analysis for switch expressions.

Preserve SA1500 multiline brace placement while allowing expression continuations. Exempt switch expression closing braces from SA1513 and preserve continuations in the SA1500 code fix.

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

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.

🟡 Changes recommended

Multiline-string final arms can bypass SA1500, and nested-switch Fix All can leave a diagnostic unresolved.

2 open issues
What changed in this PR

Addresses #4332 by allowing switch-expression continuations under SA1500 and SA1513 while retaining multiline brace-layout requirements.

Changes:

  • Exempt switch-expression continuations from newline and blank-line checks.
  • Update the SA1500 code fix and add layout and spacing regression tests.
  • Synchronize rule documentation across four languages.
File Description
test/​StyleCop.Analyzers.Test.CSharp8/​SpacingRules/​SA1013CSharp8UnitTests.cs Tests closing-brace spacing and result access.
test/​StyleCop.Analyzers.Test.CSharp8/​SpacingRules/​SA1012CSharp8UnitTests.cs Tests opening-brace spacing.
test/​StyleCop.Analyzers.Test.CSharp8/​LayoutRules/​SA1513CSharp8UnitTests.cs Tests continuations without blank lines.
test/​StyleCop.Analyzers.Test.CSharp8/​LayoutRules/​SA1500CSharp8UnitTests.cs Tests continuations, brace placement, and fixes.
src/​StyleCop.Analyzers/​LayoutRules/​SA1513ClosingBraceMustBeFollowedByBlankLine.cs Exempts switch-expression closing braces.
src/​StyleCop.Analyzers/​LayoutRules/​SA1500BracesForMultiLineStatementsMustNotShareLine.cs Allows same-line continuations.
src/​StyleCop.Analyzers.CodeFixes/​LayoutRules/​SA1500CodeFixProvider.cs Preserves trailing continuations.
documentation/​zh-cn/​SA1513.md Updates Chinese blank-line guidance.
documentation/​zh-cn/​SA1500.md Updates Chinese brace-layout guidance.
documentation/​SA1513.md Documents the blank-line exemption.
documentation/​SA1500.md Clarifies switch-expression brace layout.
documentation/​ru-ru/​SA1513.md Updates Russian blank-line guidance.
documentation/​ru-ru/​SA1500.md Updates Russian brace-layout guidance.
documentation/​cs-cz/​SA1513.md Updates Czech blank-line guidance.
documentation/​cs-cz/​SA1500.md Updates Czech brace-layout guidance.

Review effort: Balanced

@codecov

codecov Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.67%. Comparing base (9b0f9e7) to head (b9dac59).

Additional details and impacted files
@@           Coverage Diff            @@
##           master    #4334    +/-   ##
========================================
  Coverage   97.66%   97.67%            
========================================
  Files        1331     1331            
  Lines      147246   147515   +269     
  Branches     4977     4979     +2     
========================================
+ Hits       143813   144080   +267     
  Misses       2176     2176            
- Partials     1257     1259     +2     
🚀 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.

Use the preceding token's end line and retain separators between closing braces processed by Fix All. Add regressions for both review findings.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 17, 2026 17:03

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

No unresolved findings remain, and focused regressions cover the intended behavior and previously reported code-fix edge cases.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

@AArnott
AArnott merged commit 08b1299 into master Oct 17, 2026
30 checks passed
@AArnott
AArnott deleted the aarnott-switch-expression-brace-spacing branch October 17, 2026 17:48
This was referenced Nov 11, 2026
This was referenced Nov 21, 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.

SA1513/SA1500 triggered on switch expressions followed by a null-coalescing operator

2 participants