Skip to content

Fix RCS0053 for anonymous objects and top-level statements - #1858

Merged
josefpihrt merged 3 commits into
dotnet:mainfrom
Hanif6731:fix/rcs0053-anonymous-object-and-top-level
Oct 31, 2026
Merged

josefpihrt merged 3 commits into
dotnet:mainfrom
Hanif6731:fix/rcs0053-anonymous-object-and-top-level

Conversation

@Hanif6731

Copy link
Copy Markdown
Contributor
  • I've read the contributing guide
  • Bug fixes / features have an issue, or new analyzers / refactorings / fixes were discussed first
  • Unit tests added for bug fixes / features
  • Changelog updated
  • Build and tests pass locally (also verified by CI)

Summary

Two RCS0053 fixes in FixFormattingOfListAnalyzer, one commit each.

Anonymous object braces (#1851)

The braces of an anonymous object creation belong to AnonymousObjectCreationExpressionSyntax itself, not to an ObjectInitializerExpression, so GetBracesBlock never 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, which dotnet format whitespace (IDE0055) moves back, so the two never converged.

  • AnalyzeToken now accepts braces whose parent is AnonymousObjectCreationExpression; the unreachable kind is removed from the object initializer check.
  • Anonymous objects are handled like 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 for new 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.cs shape (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 when indent_size is not set and no indentation size can be determined from the file. Related: #890.

  • The brace is now reported only when the expected indentation is not zero, matching the branch for a brace with leading whitespace.

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 block f => {. with initializers (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_AnonymousObjectCreationIsLambdaBody
  • Test_Multiline_AnonymousObjectCreation_WrongIndentation, Test_Multiline_AnonymousObjectCreationIsLambdaBody_WrongIndentation
  • TestNoDiagnostic_LambdaBlockBodyIsOnlyArgumentInGlobalStatement, TestNoDiagnostic_LambdaBlockBodyIsOnlyArgumentInGlobalStatement_OpenBraceAtEndOfLine, TestNoDiagnostic_SwitchExpressionIsOnlyArgumentInGlobalStatement
  • Test_Multiline_LambdaBlockBodyInGlobalStatement_WrongIndentation
  • TestNoDiagnostic_AnonymousObjectCreationIsLambdaBodyInGlobalStatement, Test_Multiline_AnonymousObjectCreationIsLambdaBodyInGlobalStatement_WrongIndentation

Related 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)
  • Issue repros with the locally built analyzer: no RCS0053, and dotnet format (analyzers + whitespace) reaches a fixed point
  • Before/after RCS0053 run over dotnet/eShop, dotnet/aspire-samples, both CleanArchitecture templates and this repository's src: diagnostics only disappear, and each one that disappears is an instance of one of the two issues

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
josefpihrt merged commit 480132d into dotnet:main Oct 31, 2026
17 checks passed
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants