Fix MA0184 code fix keeping doubled braces as literal text - #1465
Merged
Merged
Conversation
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
This was referenced Sep 12, 2026
This was referenced Sep 17, 2026
Merged
Open
Open
Open
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.
Problem
The MA0184 code fix ("Convert to regular string") copied
InterpolatedStringTextSyntax.TextToken.ValueTextinto 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: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
IInterpolatedStringTextOperationparts, which have the braces unescaped. The replacement is computed inRegisterCodeFixesAsync, 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/DocumentationGeneratorproduces no changes.Notes for reviewers
$, 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.