From 0b0223f1a7f934fa6d86c723622ee5c9dae3b5b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9rald=20Barr=C3=A9?= Date: Sat, 17 Oct 2026 23:12:04 -0400 Subject: [PATCH] MA0101: do not offer the code fix for interpolated strings --- ...tContainsNonDeterministicEndOfLineFixer.cs | 19 ++++++++---------- ...sNonDeterministicEndOfLineAnalyzerTests.cs | 20 +++++++++++++++++++ 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/Meziantou.Analyzer.CodeFixers/Rules/StringShouldNotContainsNonDeterministicEndOfLineFixer.cs b/src/Meziantou.Analyzer.CodeFixers/Rules/StringShouldNotContainsNonDeterministicEndOfLineFixer.cs index ff875caeb..a97ba7b05 100644 --- a/src/Meziantou.Analyzer.CodeFixers/Rules/StringShouldNotContainsNonDeterministicEndOfLineFixer.cs +++ b/src/Meziantou.Analyzer.CodeFixers/Rules/StringShouldNotContainsNonDeterministicEndOfLineFixer.cs @@ -13,8 +13,8 @@ public sealed class StringShouldNotContainsNonDeterministicEndOfLineFixer : Code public override async Task RegisterCodeFixesAsync(CodeFixContext context) { var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false); - var nodeToFix = root?.FindNode(context.Span, getInnermostNodeForTie: true); - if (nodeToFix is null) + // Only the string literals are fixed, not the interpolated strings + if (root?.FindNode(context.Span, getInnermostNodeForTie: true) is not LiteralExpressionSyntax nodeToFix) return; foreach (var newLine in new[] { null, "\n", "\r\n" }) @@ -33,7 +33,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context) } } - private static async Task FixString(Document document, SyntaxNode nodeToFix, string? newLine, CancellationToken cancellationToken) + private static async Task FixString(Document document, LiteralExpressionSyntax nodeToFix, string? newLine, CancellationToken cancellationToken) { var editor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false); var generator = editor.Generator; @@ -44,15 +44,12 @@ private static async Task FixString(Document document, SyntaxNode node var firstCharPosition = nodeToFix.GetLineSpan(cancellationToken)?.StartLinePosition.Character ?? 0; var newLineTrivia = TriviaList(lineEnding == "\n" ? LineFeed : CarriageReturnLineFeed, Whitespace(new string(' ', firstCharPosition))); - if (nodeToFix is LiteralExpressionSyntax literal) - { - var text = literal.GetText().ToString(); - var isVerbatim = text.StartsWith('@', StringComparison.Ordinal); - text = isVerbatim ? text[2..^1] : text[1..^1]; + var text = nodeToFix.GetText().ToString(); + var isVerbatim = text.StartsWith('@', StringComparison.Ordinal); + text = isVerbatim ? text[2..^1] : text[1..^1]; - var newNode = ReplaceString(generator, text, newLine, newLineTrivia); - editor.ReplaceNode(nodeToFix, newNode); - } + var newNode = ReplaceString(generator, text, newLine, newLineTrivia); + editor.ReplaceNode(nodeToFix, newNode); return editor.GetChangedDocument(); } diff --git a/tests/Meziantou.Analyzer.Test/Rules/StringShouldNotContainsNonDeterministicEndOfLineAnalyzerTests.cs b/tests/Meziantou.Analyzer.Test/Rules/StringShouldNotContainsNonDeterministicEndOfLineAnalyzerTests.cs index 3a83323f5..0e566bf52 100755 --- a/tests/Meziantou.Analyzer.Test/Rules/StringShouldNotContainsNonDeterministicEndOfLineAnalyzerTests.cs +++ b/tests/Meziantou.Analyzer.Test/Rules/StringShouldNotContainsNonDeterministicEndOfLineAnalyzerTests.cs @@ -246,4 +246,24 @@ void Test() return test.RunAsync(); } + + [Fact] + public Task InterpolatedString_NoCodeFix() + { + const string Source = """ + class Dummy + { + void Test() + { + _ = {|MA0101:$@"line1{0} + line2"|}; + } + } + """; + var test = CreateTest(); + test.TestCode = Source; + test.FixedCode = Source; + + return test.RunAsync(); + } }