diff --git a/ChangeLog.md b/ChangeLog.md index ca9d42ed1a..acc4d249b7 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fix analyzer [RCS1158](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1158) ([PR](https://github.com/dotnet/roslynator/pull/1288)) - Fix analyzer [RCS1032](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1032) ([PR](https://github.com/dotnet/roslynator/pull/1289)) - Fix analyzer [RCS1176](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1176) ([PR](https://github.com/dotnet/roslynator/pull/1291)) +- Fix analyzer [RCS1197](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1197) ([PR](https://github.com/dotnet/roslynator/pull/1166)) ## [4.6.4] - 2023-11-24 diff --git a/src/Tests/Analyzers.Tests/RCS1197OptimizeStringBuilderAppendCallTests.cs b/src/Tests/Analyzers.Tests/RCS1197OptimizeStringBuilderAppendCallTests.cs index 96cee0eff5..fdf2df679e 100644 --- a/src/Tests/Analyzers.Tests/RCS1197OptimizeStringBuilderAppendCallTests.cs +++ b/src/Tests/Analyzers.Tests/RCS1197OptimizeStringBuilderAppendCallTests.cs @@ -370,6 +370,69 @@ void M() "); } + [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.OptimizeStringBuilderAppendCall)] + public async Task Test_InterpolatedRawString_ContainingQuotes() + { + await VerifyDiagnosticAndFixAsync(@" +using System.Text; +class C +{ + void M() + { + string s = null; + var sb = new StringBuilder(); + sb.Append([|$""""""""{s}""""""""|]); + } +} +", @" +using System.Text; +class C +{ + void M() + { + string s = null; + var sb = new StringBuilder(); + sb.Append("""""""""""""""").Append(s).Append(""""""""""""""""); + } +} +", options: WellKnownCSharpTestOptions.Default_CSharp11); + } + + [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.OptimizeStringBuilderAppendCall)] + public async Task Test_InterpolatedMultilineRawString_ContainingQuotes() + { + await VerifyDiagnosticAndFixAsync(@" +using System.Text; +class C +{ + void M() + { + string s = null; + var sb = new StringBuilder(); + sb.Append([|$"""""" + {s} +""""""|] + ); + } +} +", @" +using System.Text; +class C +{ + void M() + { + string s = null; + var sb = new StringBuilder(); + sb.Append("""""" + +"""""").Append(s).Append("""""" + +""""""); + } +} +", options: WellKnownCSharpTestOptions.Default_CSharp11); + } + [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.OptimizeStringBuilderAppendCall)] public async Task Test_InterpolatedString_Char() { diff --git a/src/Tests/Tests.Common/Testing/CSharp/WellKnownCSharpTestOptions.cs b/src/Tests/Tests.Common/Testing/CSharp/WellKnownCSharpTestOptions.cs index 86ac09b976..7375a62376 100644 --- a/src/Tests/Tests.Common/Testing/CSharp/WellKnownCSharpTestOptions.cs +++ b/src/Tests/Tests.Common/Testing/CSharp/WellKnownCSharpTestOptions.cs @@ -14,6 +14,7 @@ public static class WellKnownCSharpTestOptions private static CSharpTestOptions _default_CSharp7_3; private static CSharpTestOptions _default_CSharp8; private static CSharpTestOptions _default_CSharp9; + private static CSharpTestOptions _default_CSharp11; private static CSharpTestOptions _default_NullableReferenceTypes; public static CSharpTestOptions Default_CSharp5 @@ -94,6 +95,19 @@ public static CSharpTestOptions Default_CSharp9 } } + public static CSharpTestOptions Default_CSharp11 + { + get + { + if (_default_CSharp11 is null) + Interlocked.CompareExchange(ref _default_CSharp11, Create(), null); + + return _default_CSharp11; + + static CSharpTestOptions Create() => DefaultCSharpTestOptions.Value.WithParseOptions(DefaultCSharpTestOptions.Value.ParseOptions.WithLanguageVersion(LanguageVersion.CSharp11)); + } + } + public static CSharpTestOptions Default_NullableReferenceTypes { get diff --git a/src/Workspaces.Common/CSharp/Refactorings/ConvertInterpolatedStringToStringBuilderMethodRefactoring.cs b/src/Workspaces.Common/CSharp/Refactorings/ConvertInterpolatedStringToStringBuilderMethodRefactoring.cs index b936643a74..d3ae5e834e 100644 --- a/src/Workspaces.Common/CSharp/Refactorings/ConvertInterpolatedStringToStringBuilderMethodRefactoring.cs +++ b/src/Workspaces.Common/CSharp/Refactorings/ConvertInterpolatedStringToStringBuilderMethodRefactoring.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Immutable; using System.Text; +using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using Roslynator.Text; @@ -70,12 +71,30 @@ public static (SyntaxKind contentKind, string methodName, ImmutableArray