Skip to content

SA1137: fix closing brace indentation when inline - #4216

Merged
AArnott merged 2 commits into
masterfrom
aarnott-sa1137-brace-fix
Oct 10, 2026
Merged

AArnott merged 2 commits into
masterfrom
aarnott-sa1137-brace-fix

Conversation

@AArnott

@AArnott AArnott commented Oct 10, 2026

Copy link
Copy Markdown
Contributor

Fixes #3296

The SA1137 code fix previously inserted indentation before an inline closing brace without moving the brace to its own line. It now replaces only the whitespace immediately before an inline closing brace or bracket with a matching line ending and the expected indentation. Comments are preserved, and individual fixes and Fix All use the same text-change logic. Analyzer diagnostics and valid formatting behavior are unchanged.

Regression evidence: before changing product code, five VerifyCSharpFixAsync cases failed on master (0931d709f59f428a084728c38ec4b27f670d1076) at Iterative code fix application, showing 42 }; instead of 42 followed by a newline and };. The extra spaces accumulate because the diagnostic remains after the broken fix. The C#6 reproduction declares int[] _; to retain the reporter's _ = new[] assignment while compiling on the oldest supported compiler.

Coverage includes no preceding whitespace, spaces, a tab, block comments, LF/CRLF, and the shared C#12 collection-expression closing-bracket path. The verifier checks individual fixes and Fix All. Existing tests cover correctly positioned braces, single-line initializers, and ordinary indentation corrections.

Validation with SDK 10.0.401 (allowed by global.json):

  • C#15 Debug build, both target frameworks: 0 warnings, 0 errors.
  • Full SA1137 suites in the lowest/latest projects: 44/79 passing, respectively, on both net6.0 and net472.
  • Inherited regression tests in C#7–14 on net6.0: all pass (6 cases per project through C#11, 8 cases per project from C#12).

Performance: code-fix-only change; no analyzer changes, registrations, semantic-model work, or analyzer runtime cost. The fix reuses the document text and the existing line-ending helper.

Inspected the closed, unmerged #3544 for prior intent; this is a narrower implementation in the code fix rather than its analyzer-side behavior change. Documentation remains unchanged because rule behavior is unchanged.

This PR remains a draft pending CI; humans review and merge.

Insert a line break before inline closing braces and brackets while preserving comments and the document's line endings. Share the correction between individual fixes and Fix All without changing analyzer behavior.

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

codecov Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.70992% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.57%. Comparing base (0931d70) to head (60a98da).
⚠️ Report is 12 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff            @@
##           master    #4216    +/-   ##
========================================
  Coverage   97.57%   97.57%            
========================================
  Files        1276     1276            
  Lines      135692   135819   +127     
  Branches     4383     4385     +2     
========================================
+ Hits       132397   132529   +132     
+ Misses       2135     2132     -3     
+ Partials     1160     1158     -2     
🚀 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 10, 2026 14:55
@AArnott
AArnott requested a balanced review from Copilot October 10, 2026 14:55

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

Closing delimiters following leading comments still bypass the new fix path.

1 open finding
What changed in this PR

Fixes SA1137’s code fix so inline closing delimiters move to the correctly indented next line.

Changes:

  • Shares newline-aware text-change logic between individual and Fix All operations.
  • Preserves preceding comments and source line endings.
  • Adds brace and C# 12 collection-expression regression tests.
File Description
SA1137UnitTests.cs Tests inline initializer braces, trivia, comments, and line endings.
SA1137CSharp12UnitTests.cs Tests inline collection-expression brackets.
IndentationCodeFixProvider.cs Inserts a matching line ending before inline closing delimiters.

🧠 Review effort: Balanced


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

Use the whitespace-only line-start check so comments preceding a closing delimiter do not bypass newline insertion. Add LF and CRLF regression coverage for braces and brackets.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@AArnott
AArnott merged commit ed64bed into master Oct 10, 2026
25 checks passed
@AArnott
AArnott deleted the aarnott-sa1137-brace-fix branch October 10, 2026 15:32
This was referenced Nov 11, 2026
This was referenced Nov 20, 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.

Fix for SA1137 Elements should have the same indentation incorrectly applies for closing brace that's not at the start of line

2 participants