Skip to content

Fix MA0184 code fix keeping doubled braces as literal text - #1465

Merged
meziantou merged 2 commits into
mainfrom
feature/ma0184-doubled-braces-bug-1bddef
Sep 12, 2026
Merged

meziantou merged 2 commits into
mainfrom
feature/ma0184-doubled-braces-bug-1bddef

Conversation

@meziantou

Copy link
Copy Markdown
Owner

Problem

The MA0184 code fix ("Convert to regular string") copied InterpolatedStringTextSyntax.TextToken.ValueText into a regular string literal. For non-raw interpolated strings, that text still contains the brace escapes ({{ and }}), which a regular string literal does not interpret, so the fix changed the value of the string:

public static object Run() => $"{{text}}"; // returns {text}
// after the fix
public static object Run() => "{{text}}";  // returns {{text}}

Both versions compile, so JSON fragments and other brace-containing text were silently corrupted.

Fix

For non-raw interpolated strings, the fixer now builds the literal from the value computed by the compiler: the constant values of the IInterpolatedStringTextOperation parts, which have the braces unescaped. The replacement is computed in RegisterCodeFixesAsync, and the fix is not registered when the value cannot be obtained. The raw string path is unchanged.

Tests

Added to DoNotUseInterpolatedStringWithoutParametersAnalyzerTests:

  • $"{{text}}""{text}"
  • $@"{{""text"": ""\""}}""{\"text\": \"\\\"}" (verbatim string with escaped braces, quotes, and a backslash)
  • $""""

The two brace tests fail with the previous fixer. The test class passes (16/16) on Roslyn 4.8, 4.14, 5.0, 5.6 and 5.9. dotnet run --project src/DocumentationGenerator produces no changes.

Notes for reviewers

  • Not addressed here: the raw string path removes only the first $, so $$"""{text}""" becomes $"""{text}""", which turns {text} into an interpolation. Testing it is awkward because the testing library's markup parser treats $$ as the caret position marker, so I left it for a separate change.
  • Verbatim interpolated strings are still converted to regular (escaped) literals rather than verbatim ones, as before.

The code fix copied the token text of the interpolated string into a
regular string literal. That text still contains the escaped braces
("{{" and "}}"), so $"{{text}}" was converted to "{{text}}" instead of
"{text}", changing the value of the string.

The fix now uses the value computed by the compiler (the constant values
of the IInterpolatedStringTextOperation parts), and is only registered
when that value is available.
…d-braces-bug-1bddef

# Conflicts:
#	src/Meziantou.Analyzer.CodeFixers/Rules/DoNotUseInterpolatedStringWithoutParametersFixer.cs
@meziantou
meziantou merged commit 1bf8753 into main Sep 12, 2026
13 checks passed
@meziantou
meziantou deleted the feature/ma0184-doubled-braces-bug-1bddef branch September 12, 2026 03:32
This was referenced Sep 12, 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