Skip to content

SA1137: accept inline collection bracket alignment - #4400

Merged
AArnott merged 2 commits into
masterfrom
aarnott-expert-doodle
Nov 6, 2026
Merged

AArnott merged 2 commits into
masterfrom
aarnott-expert-doodle

Conversation

@AArnott

@AArnott AArnott commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #4399

SA1137 currently rejects a common layout where a collection expression's closing bracket is vertically aligned with an opening bracket that follows code on the same line. Its standard fix also cannot preserve that preferred alignment.

The analyzer now accepts either the opening bracket line's indentation or the opening bracket's visual column. Both SA1137 and SA1500 offer an alternate fix to align the closing bracket with the opening bracket, while retaining their existing fixes. Alignment honors configured tab size and tab usage; SA1500 Fix All also tracks an opening bracket moved earlier in the same batch. Rule documentation and localized action text are updated.

The regression test failed on the pre-change code with SA1137 at the closing bracket. New tests cover accepted and rejected tab alignment, tab-aware fixes, and nested SA1500 Fix All. SA1137 and SA1500 suites pass on C# 12 and C# 15, and SA1137 plus resource localization tests pass on C# 6. The full solution Debug build succeeds with zero warnings. Relevant documentation and localization checks pass; the full documentation test run requires prebuilt DocFX output that was not present locally.

Performance impact: SA1137 remains limited to collection-expression nodes. Tab-aware column checks run only for inline opening brackets with closing brackets on another line; there is no syntax-tree walk or semantic analysis. Column generation in code fixes runs only when a fix is requested.

Allow collection-expression closing brackets to align with an inline opening bracket and offer that alignment as an alternate fix for SA1137 and SA1500.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

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

Incorrect tab-column handling causes false or missed alignment diagnostics and faulty fixes, while alternate Fix All can misalign nested collection brackets and introduce new violations.

3 open issues
1 file not reviewed
  • src/StyleCop.Analyzers/Lightup/.generated/Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp/Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp.CSharpResxGenerator/ReadabilityResources.Designer.cs: Generated file
What changed in this PR

This PR addresses #4399 by allowing collection-expression closing brackets to align with inline opening brackets, extending SA1137 and the related code fixes.

Changes:

  • Accepts either line indentation or opening-bracket alignment in SA1137.
  • Adds alternate alignment actions to SA1137 and SA1500, with regression tests.
  • Updates rule documentation and localized action text.
File Description
test/​StyleCop.Analyzers.Test.CSharp12/​ReadabilityRules/​SA1137CSharp12UnitTests.cs Tests accepted alignment and the alternate fix.
test/​StyleCop.Analyzers.Test.CSharp12/​LayoutRules/​SA1500CSharp12UnitTests.cs Tests the alternate closing-bracket fix.
src/​StyleCop.Analyzers/​ReadabilityRules/​SA1137ElementsShouldHaveTheSameIndentation.cs Allows opening-bracket column alignment.
src/​StyleCop.Analyzers/​ReadabilityRules/​ReadabilityResources.zh-CN.resx Adds Chinese action text.
src/​StyleCop.Analyzers/​ReadabilityRules/​ReadabilityResources.ru-RU.resx Adds Russian action text.
src/​StyleCop.Analyzers/​ReadabilityRules/​ReadabilityResources.resx Adds English action text.
src/​StyleCop.Analyzers/​Lightup/​.generated/​Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp/​Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp.CSharpResxGenerator/​ReadabilityResources.Designer.cs Exposes the new resource accessor.
src/​StyleCop.Analyzers.CodeFixes/​ReadabilityRules/​IndentationCodeFixProvider.cs Adds alternate alignment and Fix All handling.
src/​StyleCop.Analyzers.CodeFixes/​LayoutRules/​SA1500CodeFixProvider.cs Adds alternate alignment and Fix All handling.
src/​StyleCop.Analyzers.CodeFixes/​Helpers/​IndentationHelper.cs Adds shared column-based indentation generation.
documentation/​zh-cn/​SA1500.md Documents the alternate fix in Chinese.
documentation/​zh-cn/​SA1137.md Documents accepted alignments in Chinese.
documentation/​SA1500.md Documents the alternate fix in English.
documentation/​SA1137.md Documents accepted alignments in English.
documentation/​ru-ru/​SA1500.md Documents the alternate fix in Russian.
documentation/​ru-ru/​SA1137.md Documents accepted alignments in Russian.
documentation/​cs-cz/​SA1500.md Documents the alternate fix in Czech.
documentation/​cs-cz/​SA1137.md Documents accepted alignments in Czech.

Review effort: Balanced

@codecov

codecov Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.18605% with 15 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.64%. Comparing base (37d8d30) to head (32b0c2a).
⚠️ Report is 3 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4400      +/-   ##
==========================================
- Coverage   97.64%   97.64%   -0.01%     
==========================================
  Files        1350     1350              
  Lines      153772   154014     +242     
  Branches     5287     5297      +10     
==========================================
+ Hits       150157   150388     +231     
- Misses       2280     2289       +9     
- Partials     1335     1337       +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 configured tab stops when accepting and applying collection-expression bracket alignment. Preserve rewritten opening-bracket indentation when SA1500 Fix All processes nested closing brackets.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@AArnott
AArnott marked this pull request as ready for review November 6, 2026 15:23
Copilot AI balanced review requested due to automatic review settings November 6, 2026 15:23
@AArnott
AArnott merged commit 958b4ec into master Nov 6, 2026
30 checks passed
@AArnott
AArnott deleted the aarnott-expert-doodle branch November 6, 2026 15:24

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.

🔵 Needs a closer look

Alternate Fix All still introduces SA1137 by misaligning an inner closing bracket after moving preceding code, requiring corrected column calculations and regression tests.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Fix SA1137 alignment after alternate Fix All moves code

src/​StyleCop.Analyzers.CodeFixes/​LayoutRules/​SA1500CodeFixProvider.cs:144

Alternate Fix All still introduces SA1137 when it moves code before an inline opening bracket without replacing that bracket. For int[][] values = [(int[])[ followed by 1, 2]];, moving the cast leaves this fallback aligning the inner ] at column 29 instead of 15. Calculate the opening bracket's column after pending replacements to preceding tokens, and add regressions for wrapped collections and moved return statements.

3 resolved since last review
1 file not reviewed
  • src/StyleCop.Analyzers/Lightup/.generated/Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp/Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp.CSharpResxGenerator/ReadabilityResources.Designer.cs: Generated file

Review effort: Balanced

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.

SA1137 reports on close bracket tokens and codefix "breaks" formatting

2 participants