Skip to content

Preserve interpolation alignment in MA0052 code fix - #1454

Merged
meziantou merged 2 commits into
mainfrom
feature/ma0052-interpolation-alignment-d5ac30
Sep 11, 2026
Merged

meziantou merged 2 commits into
mainfrom
feature/ma0052-interpolation-alignment-d5ac30

Conversation

@meziantou

Copy link
Copy Markdown
Owner

Summary

The MA0052 code fix (replace constant Enum.ToString with nameof) dropped the alignment clause of interpolations, which changed the output of the code:

enum Color { Red }

_ = $"[{Color.Red,10}]";          // "[       Red]"
_ = $"[{nameof(Color.Red)}]";     // "[Red]" — previous fix result
_ = $"[{nameof(Color.Red),10}]";  // "[       Red]" — new fix result

Changes

  • ReplaceEnumToStringWithNameofFixer now updates the existing InterpolationSyntax instead of creating a new one: the expression is replaced by nameof(...) and the alignment clause is kept.
  • The format clause is still removed, as before. This is safe because the analyzer only reports interpolations whose format produces the name of the value (G, g, F, f, or none).
  • Added InterpolatedString_Alignment test covering positive and negative widths, with and without a format clause, plus a non-reported :D case.

Validation

  • The new test fails without the fix and passes with it.
  • ReplaceEnumToStringWithNameofAnalyzerTests pass on roslyn4.8, 4.14, 5.0, 5.6 and 5.9 (15/15 each).
  • dotnet run --project src/DocumentationGenerator produces no changes.

The fixer replaced the interpolation with a new one containing only the
nameof expression, which dropped the alignment clause and changed the
padded output (e.g. $"[{Color.Red,10}]" became $"[{nameof(Color.Red)}]").

The fixer now only replaces the expression and removes the format clause,
which is safe as the reported formats (G, F, empty) already produce the
name of the value.
…olation-alignment-d5ac30

# Conflicts:
#	src/Meziantou.Analyzer.CodeFixers/Rules/ReplaceEnumToStringWithNameofFixer.cs
@meziantou
meziantou enabled auto-merge (squash) September 11, 2026 19:24
@meziantou
meziantou merged commit 8c43706 into main Sep 11, 2026
13 checks passed
@meziantou
meziantou deleted the feature/ma0052-interpolation-alignment-d5ac30 branch September 11, 2026 19:30
This was referenced Sep 11, 2026
This was referenced Sep 17, 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.

1 participant