Repository navigation
Fix RCS0053 for anonymous objects and top-level statements - #1858
Merged
josefpihrt merged 3 commits intoOct 31, 2026
Merged
josefpihrt merged 3 commits into
josefpihrt merged 3 commits into
Conversation
The braces of an anonymous object creation belong to
AnonymousObjectCreationExpressionSyntax itself, not to an
ObjectInitializerExpression, so GetBracesBlock never recognized them.
An anonymous object that is the only argument, or the body of a lambda
that is the only argument, was then analyzed as an ordinary multiline
argument: the formatter's layout was reported and the code fix
indented the braces one level deeper, which the formatter undoes.
Accept braces whose parent is AnonymousObjectCreationExpression and
drop the unreachable kind from the object initializer check. Anonymous
objects are now handled like `new D { ... }`: braces aligned with the
line are accepted, and braces one level deeper are reported.
Fixes dotnet#1851
When the only argument ends with a block whose closing brace starts a line (a lambda body, object initializer or switch expression), a brace without leading whitespace was reported without comparing its indentation with the expected one. In a top-level statement the expected indentation is zero, so the correctly formatted brace was reported and the code fix had nothing to change. The check is not reached when `indent_size` is not set and no indentation size can be determined from the file, which is why it shows up only with some configurations. Report the brace only when the expected indentation is not zero, as the branch for a brace with leading whitespace already does. Fixes dotnet#1852
Hanif6731
added a commit
to Hanif6731/HouseRules
that referenced
this pull request
Oct 26, 2026
…#13) ## Why The top-level lambda bug (dotnet/roslynator#1852) also appears without `indent_size`, because Roslynator infers the indentation step from the file's other code. Both RCS0053 bugs also cover more shapes than the README said, and both are now fixed upstream in dotnet/roslynator#1858, which is under review. ## Change - The #1851 bullet also covers an anonymous object passed directly as the only argument (`Serialize(new`). - The #1852 bullet covers any block (lambda body, object initializer, switch expression) at column 0 in a top-level statement, and explains when the bug appears. - A new line links the upstream fix and keeps the advice for until a Roslynator release includes it. The README is documentation only, so there is no CHANGELOG entry. The preview.3 notes stay unchanged.
josefpihrt
approved these changes
Oct 31, 2026
Hanif6731
added a commit
to Hanif6731/HouseRules
that referenced
this pull request
Oct 31, 2026
## Why dotnet/roslynator#1858, which fixes the two RCS0053 known limitations, was merged on 2026-10-31, after 5.0.1. The README still said it was under review. ## Change - README, known limitations: #1858 is "merged after 5.0.1" instead of "under review". The advice to baseline the sites stays until a Roslynator release includes the fix. The README is documentation only, so there is no CHANGELOG entry.
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.
Summary
Two RCS0053 fixes in
FixFormattingOfListAnalyzer, one commit each.Anonymous object braces (#1851)
The braces of an anonymous object creation belong to
AnonymousObjectCreationExpressionSyntaxitself, not to anObjectInitializerExpression, soGetBracesBlocknever recognized them as a block. An anonymous object that is the only argument (M(new+{), or the body of a lambda that is the only argument (.Select(r => new+{, the layout the Roslyn formatter produces), was then analyzed as an ordinary multiline argument: RCS0053 reported it and its code fix indented the braces one level deeper, whichdotnet format whitespace(IDE0055) moves back, so the two never converged.AnalyzeTokennow accepts braces whose parent isAnonymousObjectCreationExpression; the unreachable kind is removed from the object initializer check.new D { ... }: braces aligned with the line are accepted, and braces one level deeper (the layout the old code fix produced) are now reported, as they already are fornew D.Brace block in a top-level statement (#1852)
When the only argument ends with a block whose closing brace starts a line (lambda body, object initializer, switch expression), a brace without leading whitespace was reported without comparing its indentation with the expected one. In a top-level statement the expected indentation is zero, so the usual
Program.csshape (builder.Services.AddCors(options =>+{in column 0) was reported and the code fix had nothing to change. It depends on the configuration because the check is not reached whenindent_sizeis not set and no indentation size can be determined from the file. Related: #890.Both fixes are needed for an anonymous object that is a lambda body in a top-level statement (
args.Select(f => new+{in column 0); a test covers that combination.Not changed here: with the open brace at the end of the line (
f => new {) and a misplaced}, the existing code-fix path flattens the members to the brace's column, as it already does for a lambda blockf => {.withinitializers (M(r with+{) have the same issue as #1851 and are left for a follow-up.Tests added to
RCS0053FixFormattingOfListTests:TestNoDiagnostic_Multiline_AnonymousObjectCreation,TestNoDiagnostic_Multiline_AnonymousObjectCreationIsLambdaBodyTest_Multiline_AnonymousObjectCreation_WrongIndentation,Test_Multiline_AnonymousObjectCreationIsLambdaBody_WrongIndentationTestNoDiagnostic_LambdaBlockBodyIsOnlyArgumentInGlobalStatement,TestNoDiagnostic_LambdaBlockBodyIsOnlyArgumentInGlobalStatement_OpenBraceAtEndOfLine,TestNoDiagnostic_SwitchExpressionIsOnlyArgumentInGlobalStatementTest_Multiline_LambdaBlockBodyInGlobalStatement_WrongIndentationTestNoDiagnostic_AnonymousObjectCreationIsLambdaBodyInGlobalStatement,Test_Multiline_AnonymousObjectCreationIsLambdaBodyInGlobalStatement_WrongIndentationRelated issue
Fixes #1851
Fixes #1852
Test plan
dotnet test src/Tests/Formatting.Analyzers.Tests(561 passed; RCS0053 class 61 passed)dotnet build src/Roslynator.sln(0 warnings)dotnet format(analyzers + whitespace) reaches a fixed pointsrc: diagnostics only disappear, and each one that disappears is an instance of one of the two issues