Skip to content

SA1143: Preserve multiline branches when aligning closing delimiters - #4392

Merged
AArnott merged 2 commits into
masterfrom
aarnott-sa1143-branch-indentation-fix
Oct 30, 2026
Merged

AArnott merged 2 commits into
masterfrom
aarnott-sa1143-branch-indentation-fix

Conversation

@AArnott

@AArnott AArnott commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

SA1143's existing layout fixes flatten multiline branch expressions. In the #4390 repro, aligning the collection expression's closing bracket with its opening bracket resolves the warning without collapsing the collection.

Fixes #4390

Approach

  • Add a separate closing-delimiter alignment action for collection expressions, object and collection initializers, tuples, and parenthesized expressions. Existing single-line, expanded, and aligned-chain actions remain unchanged.
  • Edit only leading whitespace before underindented closing delimiters, matching syntax-paired opening delimiters and respecting configured tab widths and indentation style. Preserve comments, blank lines, line breaks, literal contents, and unrelated formatting.
  • Offer the action only when a meaningful edit makes the complete conditional expression, including nested conditionals, satisfy SA1143. Leave directives and unsupported layouts for manual correction; avoid rewriting interpolation contents.
  • Update all existing documentation editions, including translating the previously English Czech page.

Validation

The exact issue repro was added first and failed against unchanged master product code because the alignment action was missing. Regression coverage includes nested collections, initializers, tuples, parentheses, comments, multiline/raw/interpolated literals, LF/CRLF, tabs, Fix All, existing actions, and cases where alignment must not be offered.

All 754 SA1143 tests passed across the C# 6 through C# 15 test projects on net10.0. The C# 15 project and all inherited projects built using SDK 10.0.403 with --no-incremental -maxcpucount:1 -warnaserror, with zero warnings or errors. git diff --check passed, and the resource accessor was regenerated by the build.

Scope and performance

Analyzer behavior and registrations are unchanged; its existing multiline-layout predicate is shared with the provider. Syntax traversal and candidate reparsing occur only in the code fix. This is intentionally a delimiter-only correction, not a general branch reformatter. The new action title uses neutral-English resource fallback for cultures without a translation.

Offer a separate syntax-aware alignment action only when whitespace-only edits resolve the conditional layout. Preserve existing actions and cover the issue 4390 repro, trivia, literals, tabs, and applicability boundaries.

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

The code fix can throw during registration for conditionals in unnamed invocation arguments or collection-expression elements and miss safely fixable nested-conditional alignment changes.

2 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

Adds an SA1143 code fix for #4390 that aligns closing delimiters without flattening multiline conditional branches, while preserving existing layout actions and analyzer behavior.

Changes:

  • Adds delimiter-only alignment with whitespace preservation and validation of the resulting conditional layout.
  • Adds regression coverage for supported syntax, formatting preservation, Fix All, and inapplicable cases.
  • Updates all four documentation editions and adds the action’s resource string.
File Description
test/​StyleCop.Analyzers.Test/​ReadabilityRules/​SA1143UnitTests.cs Tests initializers, parentheses, preservation, Fix All, and rejected layouts.
test/​StyleCop.Analyzers.Test.CSharp7/​ReadabilityRules/​SA1143CSharp7UnitTests.cs Tests tuple delimiter alignment.
test/​StyleCop.Analyzers.Test.CSharp12/​ReadabilityRules/​SA1143CSharp12UnitTests.cs Tests collections, nested layouts, raw strings, tabs, and existing actions.
src/​StyleCop.Analyzers/​ReadabilityRules/​SA1143ConditionalExpressionsMustBeFormattedConsistently.cs Shares the existing multiline-layout predicate with the provider.
src/​StyleCop.Analyzers/​ReadabilityRules/​ReadabilityResources.resx Adds the alignment action title.
src/​StyleCop.Analyzers/​Lightup/​.generated/​Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp/​Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp.CSharpResxGenerator/​ReadabilityResources.Designer.cs Adds the generated resource accessor.
src/​StyleCop.Analyzers.CodeFixes/​ReadabilityRules/​SA1143CodeFixProvider.cs Implements and validates delimiter-only alignment.
documentation/​zh-cn/​SA1143.md Documents alignment behavior in Simplified Chinese.
documentation/​SA1143.md Explains the new action and its safety limits.
documentation/​ru-ru/​SA1143.md Documents alignment behavior in Russian.
documentation/​cs-cz/​SA1143.md Translates the page into Czech and documents alignment.

Review effort: Balanced

Select the innermost matching conditional when syntax wrappers share its span, and align nested branches against their own operators without duplicating delimiter edits. Add regressions for invocation arguments, collection elements, and nested branches.

Translate the new action title into Russian and Simplified Chinese to satisfy existing resource completeness checks.

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

codecov Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.39024% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.64%. Comparing base (a29da78) to head (c14103b).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff            @@
##           master    #4392    +/-   ##
========================================
  Coverage   97.63%   97.64%            
========================================
  Files        1347     1348     +1     
  Lines      152420   152728   +308     
  Branches     5241     5258    +17     
========================================
+ Hits       148816   149126   +310     
  Misses       2274     2274            
+ Partials     1330     1328     -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.

@AArnott
AArnott marked this pull request as ready for review October 30, 2026 15:39
Copilot AI balanced review requested due to automatic review settings October 30, 2026 15:39
@AArnott
AArnott merged commit a9f5f3d into master Oct 30, 2026
29 checks passed
@AArnott
AArnott deleted the aarnott-sa1143-branch-indentation-fix branch October 30, 2026 15:39

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

The code fix can leave adjacent multiline parenthesized operands misaligned because it calculates closing-delimiter positions without accounting for preceding indentation edits on the opening delimiter’s line.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Account for prior indentation edits when aligning delimiters

src/​StyleCop.Analyzers.CodeFixes/​ReadabilityRules/​SA1143CodeFixProvider.cs:195

When two multiline parenthesized operands meet on a ) + ( line, aligning the first ) also shifts the second (, but this calculation still uses its original column. For example, the second opening delimiter moves from column 16 to 18 while its closing delimiter is placed at column 16; SA1143 accepts the result even though the pair remains misaligned. Account for preceding indentation edits on each opening delimiter's line when computing target columns, and add a regression for this layout.

2 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

This was referenced Nov 11, 2026
This was referenced Nov 24, 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.

Relax SA1143 for multi-line collection expressions

2 participants