Repository navigation
SA1501/SA1503: Allow configurable braceless if guards - #4319
Merged
Merged
Conversation
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>
There was a problem hiding this comment.
🟢 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, andnextLinesettings 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>
Codecov Report✅ All modified and coverable lines are covered by tests. 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:
|
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 Nov 23, 2026
Open
[DPDBT] Bump Microsoft.CodeAnalysis.Analyzers and 6 others
sergey-rybalkin/CommandLine.Generators#63
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.
Allow projects to permit compact guard clauses without disabling brace requirements for other statements. The default behavior remains unchanged.
Fixes #2252
Approach
Adds
layoutRules.allowBracelessIfStatementsin stylecop.json andstylecop.layout.allowBracelessIfStatementsin EditorConfig/globalconfig, withoff(default),sameLine, andnextLinevalues.ifstatements qualify. Every branch of anif/else if/elsechain still requires braces.ifheader and the jump statement must each fit on one line.sameLinerequires the whole statement on one line;nextLinerequires the jump on the immediately following line.return,throw,break,continue,goto(including case/default), andyield break. Other statements, includingyield return, remain subject to the existing rules.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.