Skip to content

SA1137: align malformed array initializer brackets - #4272

Merged
AArnott merged 1 commit into
masterfrom
aarnott-sa1137-indentation
Oct 13, 2026
Merged

AArnott merged 1 commit into
masterfrom
aarnott-sa1137-indentation

Conversation

@AArnott

@AArnott AArnott commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

Fixes #2854

The SA1137 code fix used an unindented opening array-initializer brace as its anchor, moving the closing brace to column 1 while leaving the initializer elements over-indented. For this malformed layout, the fix now aligns both braces with the containing expression and places the elements one configured indentation level deeper. Only indentation whitespace is changed; comments, blank lines, shared-line elements, and line endings are preserved. Fix All handles overlapping element and brace corrections.

Added the reporter's exact layout to SA1137UnitTests before changing product code. The regression test fails on master (5fb0ffaf999434bf5b78b7c8bb86c8334ac46b37): its fixed-output diff shows both braces at column 1 instead of the expected expression indentation. Additional tests cover implicit/explicit arrays, declaration initializers, comments, tabs, two-space indentation, LF/CRLF, and the unchanged zero-indentation-expression behavior. Updated the English, Czech, and Russian rule documentation.

Validation (SDK 10.0.401):

  • dotnet build -c Release -maxcpucount:1: succeeded, zero warnings/errors.
  • C# 15 Debug project build: succeeded, zero warnings/errors.
  • Entire SA1137 suite in the lowest C# 6 and latest C# 15 projects: 60/60 and 97/97 passed on both net10.0 and net472.
  • All 14 added test cases passed in each C# 7–14 project on net10.0.

Performance: the analyzer is unchanged, so analyzer performance and diagnostic reporting are unchanged. The additional syntax and whitespace work runs only in the code fix; indentation settings are read through the existing settings helper. The behavior change is limited to malformed array initializers with an unindented opening brace and mismatched braces on separate lines.

Draft for CI and human review; no merge or issue comment/closure performed.

Use the containing expression to align unindented array initializer braces and elements, preserving trivia and configured indentation. Cover the reported layout, Fix All, comments, tabs, and line endings.

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

codecov Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.82609% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.61%. Comparing base (5fb0ffa) to head (6f56257).
⚠️ Report is 6 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff            @@
##           master    #4272    +/-   ##
========================================
  Coverage   97.61%   97.61%            
========================================
  Files        1285     1285            
  Lines      137307   137442   +135     
  Branches     4451     4456     +5     
========================================
+ Hits       134029   134167   +138     
+ Misses       2113     2111     -2     
+ Partials     1165     1164     -1     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@AArnott
AArnott marked this pull request as ready for review October 13, 2026 16:04
Copilot AI balanced review requested due to automatic review settings October 13, 2026 16:04
@AArnott
AArnott merged commit d992ddb into master Oct 13, 2026
30 checks passed
@AArnott
AArnott deleted the aarnott-sa1137-indentation branch October 13, 2026 16:04

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

Fix All can discard nested SA1137 corrections contained within an initializer-wide edit.

1 open finding
What changed in this PR

Fixes SA1137 array-initializer indentation by anchoring malformed initializers to their containing expression.

Changes:

  • Reworks array-initializer indentation and overlapping Fix All handling.
  • Adds regression coverage for layouts, settings, trivia, and line endings.
  • Updates English, Czech, and Russian documentation.
File Description
test/​StyleCop.Analyzers.Test/​ReadabilityRules/​SA1137UnitTests.cs Adds regression and configuration coverage.
src/​StyleCop.Analyzers.CodeFixes/​ReadabilityRules/​IndentationCodeFixProvider.cs Implements initializer-wide alignment and overlap handling.
documentation/​SA1137.md Documents the new behavior.
documentation/​ru-ru/​SA1137.md Updates Russian documentation.
documentation/​cs-cz/​SA1137.md Updates Czech documentation.

🧠 Review effort: Balanced


💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +177 to +180
if (!changes.Exists(change => change.Span.Contains(textChange.Span)))
{
changes.RemoveAll(change => textChange.Span.Contains(change.Span));
changes.Add(textChange);
This was referenced Nov 11, 2026
This was referenced Nov 29, 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.

SA1137 picks the wrong bracket as correct indentation

2 participants