Skip to content

SA1501/SA1503: Allow configurable braceless if guards - #4319

Merged
AArnott merged 2 commits into
masterfrom
aarnott-flow-control-braces
Oct 16, 2026
Merged

AArnott merged 2 commits into
masterfrom
aarnott-flow-control-braces

Conversation

@AArnott

@AArnott AArnott commented Oct 16, 2026

Copy link
Copy Markdown
Contributor

Allow projects to permit compact guard clauses without disabling brace requirements for other statements. The default behavior remains unchanged.

Fixes #2252

Approach

Adds layoutRules.allowBracelessIfStatements in stylecop.json and stylecop.layout.allowBracelessIfStatements in EditorConfig/globalconfig, with off (default), sameLine, and nextLine values.

  • Only standalone if statements qualify. Every branch of an if/else if/else chain still requires braces.
  • The entire if header and the jump statement must each fit on one line. sameLine requires the whole statement on one line; nextLine requires the jump on the immediately following line.
  • Supported jumps are return, throw, break, continue, goto (including case/default), and yield break. Other statements, including yield return, remain subject to the existing rules.
  • Correctly positioned guards are not reported or changed. Existing fixes remain valid: SA1503 adds braces, and SA1501 expands disallowed same-line statements when SA1503 is disabled. Existing braces remain allowed.

Updates the schema, settings reference/migration mapping, and rule documentation. New explanations in the existing localized documentation use English fallback rather than unverified translations.

Validation

The initial exemption tests failed against unchanged master with SA1503 diagnostics. Multiline-condition regression tests also failed before the shared exemption check was tightened.

Built all compiler-generation test projects with SDK 10.0.402 and zero warnings. New guard tests passed across C# 6 through C# 15; affected rule suites passed on the oldest and newest compilers. Guard tests also passed on net472 at both endpoints. After the multiline-header refinement, SA1501/SA1503 suites passed at both endpoints and the updated regressions passed across every intermediate compiler.

Performance

Uses existing syntax-node registrations and cached, settings-aware callbacks. The shared check uses syntax and line positions only, with an early exit when the option is off. No semantic-model calls, new tree walks, or per-node collections are introduced; no performance benchmark was run.

Add off, sameLine, and nextLine placement options for standalone if bodies containing a single-line jump. Require single-line conditions and retain braces for conditional chains and other statements.

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

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 blocking issues remain; shared eligibility checks preserve defaults, with focused coverage for settings, exclusions, and fixes.

0 open findings

What changed in this PR

Adds opt-in braceless guard clauses to SA1501 and SA1503 while preserving default brace requirements.

Changes:

  • Adds off, sameLine, and nextLine settings through JSON and EditorConfig.
  • Shares guard eligibility checks and adds configuration, diagnostic, and code-fix tests.
  • Updates the schema and English/localized documentation.
File Description
test/​StyleCop.Analyzers.Test/​Settings/​SettingsUnitTests.cs Tests defaults and JSON values.
test/​StyleCop.Analyzers.Test/​LayoutRules/​SA1503UnitTests.cs Tests guards, exclusions, placement, and fixes.
test/​StyleCop.Analyzers.Test/​LayoutRules/​SA1501UnitTests.cs Tests exemptions and statement expansion.
test/​StyleCop.Analyzers.Test.CSharp8/​Settings/​SettingsCSharp8UnitTests.cs Tests EditorConfig values and JSON precedence.
test/​StyleCop.Analyzers.Test.CSharp8/​LayoutRules/​SA1503CSharp8UnitTests.cs Tests EditorConfig exemptions during fixes.
StyleCop.Analyzers/​StyleCop.Analyzers/​Settings/​stylecop.schema.json Defines the new setting.
src/​StyleCop.Analyzers/​Settings/​ObjectModel/​LayoutSettings.cs Reads the setting through SettingsReader.
src/​StyleCop.Analyzers/​Settings/​ObjectModel/​BracelessIfStatements.cs Defines supported modes.
src/​StyleCop.Analyzers/​LayoutRules/​SA1503BracesMustNotBeOmitted.cs Exempts eligible guards from brace requirements.
src/​StyleCop.Analyzers/​LayoutRules/​SA1501StatementMustNotBeOnASingleLine.cs Applies configured guard exemptions.
src/​StyleCop.Analyzers/​Helpers/​BracelessIfStatementHelper.cs Centralizes guard eligibility checks.
documentation/​zh-cn/​SA1503.md Documents the brace exemption.
documentation/​zh-cn/​SA1501.md Documents guard placement options.
documentation/​zh-cn/​Configuration.md Adds configuration mappings and guidance.
documentation/​SA1503.md Documents the brace exemption.
documentation/​SA1501.md Documents guard placement options.
documentation/​ru-ru/​SA1503.md Documents the brace exemption.
documentation/​ru-ru/​SA1501.md Documents guard placement options.
documentation/​ru-ru/​Configuration.md Adds configuration mappings and guidance.
documentation/​cs-cz/​SA1503.md Documents the brace exemption.
documentation/​cs-cz/​SA1501.md Documents guard placement options.
documentation/​cs-cz/​Configuration.md Adds configuration mappings and guidance.
documentation/​Configuration.md Explains settings, restrictions, examples, and fixes.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Include both placement examples and preserve JSON formatting to satisfy documentation localization checks.

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

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 blocking issues remain; the scoped implementation preserves defaults and includes regression coverage for configuration, eligibility, and fixes.

0 open findings

🧠 Review effort: Balanced

@AArnott
AArnott merged commit efb240f into master Oct 16, 2026
30 checks passed
@AArnott
AArnott deleted the aarnott-flow-control-braces branch October 16, 2026 13:25
@codecov

codecov Bot commented Oct 16, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.63%. Comparing base (119e00b) to head (3105f4c).
⚠️ Report is 3 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff            @@
##           master    #4319    +/-   ##
========================================
  Coverage   97.62%   97.63%            
========================================
  Files        1319     1320     +1     
  Lines      144254   144710   +456     
  Branches     4864     4872     +8     
========================================
+ Hits       140825   141282   +457     
  Misses       2173     2173            
+ Partials     1256     1255     -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 Nov 23, 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.

Could a setting be considered for SA1501/SA1503 to relax on some kind of statements?

2 participants