Skip to content

SA1003: Preserve constructor initializer line breaks in code fix - #4280

Merged
AArnott merged 4 commits into
masterfrom
aarnott-sa1003-constructor-fix
Oct 13, 2026
Merged

AArnott merged 4 commits into
masterfrom
aarnott-sa1003-constructor-fix

Conversation

@AArnott

@AArnott AArnott commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

Fixes #2613

The SA1003 code fix collapsed a constructor initializer onto the constructor declaration when the colon was at the end of a line. The fix now moves the colon to the initializer's line, retaining the source line break, indentation, and comments. SA1128 is unchanged.

The regression test failed on master with a fixed-text diff: master produced public MyType() : base(3) instead of the expected separate-line initializer. It also verifies two constructors, preserved comments, configured indentation settings, and Fix All.

Validation: dotnet test test\StyleCop.Analyzers.Test --framework net10.0 --filter FullyQualifiedName~SA1003 (31 passed); latest StyleCop.Analyzers.Test.CSharp15 SA1003 and SA1128 suites (64 passed); dotnet build StyleCopAnalyzers.slnx --configuration Debug --no-restore --verbosity minimal -m:1 (0 warnings, 0 errors).

Performance: analyzer code is unchanged; the added syntax/trivia work runs only when the SA1003 code fix is invoked.

Keep a constructor initializer on its own indented line when fixing a colon at end of line. Add regression and Fix All coverage, and document the resulting layout.\n\nCo-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@AArnott
AArnott marked this pull request as ready for review October 13, 2026 18:32
Copilot AI balanced review requested due to automatic review settings October 13, 2026 18:32
Mirror the new constructor initializer example to satisfy documentation localization parity.\n\nCo-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

Single-line comments before the colon can cause the fix to produce invalid code.

1 open finding
What changed in this PR

Updates SA1003’s code fix to retain constructor-initializer line breaks.

Changes:

  • Moves the colon to the initializer line.
  • Adds regression and Fix All coverage.
  • Documents the corrected behavior.
File Description
SA1003UnitTests.cs Adds regression coverage.
SA1003CodeFixProvider.cs Redistributes initializer trivia.
SA1003.md Documents formatting behavior.

🧠 Review effort: Balanced


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

Copilot AI balanced review requested due to automatic review settings October 13, 2026 18:35
Keep the colon's leading trivia when it contains a single-line comment so the line-comment terminator is not lost during relocation. Add a regression test for comments before the colon.\n\nCo-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

Blank-line trivia can still be lost, and the Czech documentation is not synchronized.

1 open finding
1 resolved since last review
Previously missed (1)

In code that hasn't changed since last review

Medium severity Preserve blank-line trivia between colon and base or this

src/​StyleCop.Analyzers.CodeFixes/​SpacingRules/​SA1003CodeFixProvider.cs:105

This drops any EndOfLineTrivia owned by followingToken: the loop keeps only whitespace, and line 121 then removes the complete leading trivia list. With a blank line between : and base/this, the fix therefore deletes that blank line and concatenates its whitespace into the colon's indentation. Preserve the full whitespace/end-of-line sequence (and add a blank-line regression case) so this trivia-preserving path does not alter unrelated layout.

🧠 Review effort: Balanced

Comment thread documentation/SA1003.md Resolved
Copilot AI balanced review requested due to automatic review settings October 13, 2026 18:40
Keep the Czech SA1003 edition current with the new constructor initializer guidance and example.\n\nCo-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.

🔵 Needs a closer look

Inline comments can introduce an extra blank line, and the Czech documentation remains unsynchronized.

0 open findings

1 resolved since last review
Previously missed (1)

In code that hasn't changed since last review

Medium severity Avoid duplicate newline after inline constructor comments

src/​StyleCop.Analyzers.CodeFixes/​SpacingRules/​SA1003CodeFixProvider.cs:123

When the constructor declaration already ends with an inline comment, its trailing trivia includes the comment-terminating end-of-line. WithoutTrailingWhitespace() intentionally preserves that terminator, so appending the colon's end-of-line adds a blank line: MyType() // note\n :\n base() becomes MyType() // note\n\n : base(). Keep only one terminating end-of-line here, and add this inline-comment case to the regression test.

🧠 Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings October 13, 2026 18:43

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

Block comments before the colon can lose indentation and displace the colon.

0 open findings

Previously missed (1)

In code that hasn't changed since last review

Medium severity Preserve block comment layout before initializer colon

src/​StyleCop.Analyzers.CodeFixes/​SpacingRules/​SA1003CodeFixProvider.cs:96

This special case only recognizes // trivia. A block comment on the colon's line (for example, MyType()\n /* note */ :\n base()) is also stored in token.LeadingTrivia, but it takes the else branch: WithoutLeadingWhitespace() removes the comment's indentation and the initializer indentation is then inserted between the comment and colon, yielding /* note */ : base(). Treat any leading comment/non-whitespace trivia like the single-line-comment case, and add the equivalent regression so the fix preserves both the comment and its layout.

🧠 Review effort: Balanced

@codecov

codecov Bot commented Oct 13, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.63%. Comparing base (dfaef0c) to head (aa54c89).
⚠️ Report is 5 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff            @@
##           master    #4280    +/-   ##
========================================
  Coverage   97.63%   97.63%            
========================================
  Files        1287     1287            
  Lines      138447   138579   +132     
  Branches     4529     4536     +7     
========================================
+ Hits       135171   135303   +132     
  Misses       2108     2108            
  Partials     1168     1168            
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This was referenced Nov 21, 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.

Improve code fix for SA1003 in chained constructors

2 participants