Repository navigation
SA1504: preserve comments and blank lines in the multi-line fix - #4311
Merged
Merged
Conversation
Keep comments, directives, and blank lines while expanding accessors, without changing diagnostics or single-line conversion. Preserve existing inline-comment formatting and add readable regression coverage and test-authoring guidance. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
AArnott
marked this pull request as ready for review
October 15, 2026 16:41
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #4311 +/- ##
==========================================
+ Coverage 97.62% 97.63% +0.01%
==========================================
Files 1308 1308
Lines 142251 142748 +497
Branches 4749 4756 +7
==========================================
+ Hits 138869 139377 +508
+ Misses 2156 2146 -10
+ Partials 1226 1225 -1 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
🟡 Changes recommended
Header formatting still removes existing accessor separators and loses indentation after modifier line comments.
2 open findings
What changed in this PR
Updates the SA1504 multi-line code fix to preserve comments and blank lines, addressing #4310 without changing diagnostics or the single-line action.
Changes:
- Preserves trivia when expanding block and expression-bodied accessors.
- Adds regression coverage for accessor variants and Fix All.
- Documents guidance for readable test fixtures.
| File | Description |
|---|---|
| test/StyleCop.Analyzers.Test/LayoutRules/SA1504UnitTests.cs | Adds block-accessor trivia and Fix All tests. |
| test/StyleCop.Analyzers.Test.CSharp7/LayoutRules/SA1504ArrowAccessorCSharp7UnitTests.cs | Adds expression-bodied accessor regression tests. |
| src/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1504CodeFixProvider.cs | Revises multi-line trivia preservation. |
| .agents/skills/testing-and-coverage/SKILL.md | Adds readable fixture guidance. |
🧠 Review effort: Balanced
💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| .WithTrailingTrivia(newTrailingTrivia); | ||
| .WithLeadingTrivia(insertIdentation | ||
| ? ReformatLeadingTriviaAsMultipleLines(ReformatAccessorHeaderTrivia(keyword.LeadingTrivia), indentation) | ||
| : ReformatAccessorHeaderTrivia(keyword.LeadingTrivia)) |
| return trivia.All(t => t.IsKind(SyntaxKind.WhitespaceTrivia) | ||
| || t.IsKind(SyntaxKind.EndOfLineTrivia) | ||
| || t.IsKind(SyntaxKind.MultiLineCommentTrivia)) | ||
| && !trivia.ContainsBlankLines(startsOnNewLine: false) |
This was referenced Nov 11, 2026
Bump StyleCop.Analyzers from 1.2.0-beta.556 to 1.3.180
microsoftgraph/aspnetcore-webhooks-sample#436
Open
Closed
Open
This was referenced Nov 23, 2026
Open
Open
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

The SA1504 multi-line code fix discarded comments and blank lines when rewriting existing accessor bodies, including the other accessor when expanding an expression-bodied getter or setter.
Fixes #4310
Preserve trivia while adding the line breaks and indentation required for multi-line accessors. Keep existing inline block-comment compaction where it cannot discard line comments, directives, or blank lines. Diagnostics and the single-line code action are unchanged; all work remains in the code fix, with no analyzer performance impact.
Regression coverage includes getters and setters, properties and indexers, event accessors, arrow and throw expressions, comment-only bodies, directives, disabled text, and Fix All. The initial four getter/setter property/indexer cases failed against unchanged master product code with diffs showing the reported comment and blank-line loss. Tests use explicit before/after snippets, and the testing skill records the review guidance on readable fixtures and raw interpolated strings.
Validation: full Debug solution build succeeded with zero warnings and errors. All SA1504 tests passed across the C# 6-15 test projects (503 test executions). No rule documentation changes are included because this restores expected behavior.