diff --git a/src/Analyzers/CSharp/Analysis/OptimizeStringBuilderAppendCallAnalysis.cs b/src/Analyzers/CSharp/Analysis/OptimizeStringBuilderAppendCallAnalysis.cs index f0f80e7c04..f235597ff2 100644 --- a/src/Analyzers/CSharp/Analysis/OptimizeStringBuilderAppendCallAnalysis.cs +++ b/src/Analyzers/CSharp/Analysis/OptimizeStringBuilderAppendCallAnalysis.cs @@ -33,6 +33,9 @@ public static void Analyze(SyntaxNodeAnalysisContext context, in SimpleMemberInv SimpleMemberInvocationExpressionInfo invocationInfo2 = SyntaxInfo.SimpleMemberInvocationExpressionInfo(invocationInfo.Expression); + if (!invocationInfo2.Success) + return; + if (invocationInfo2.NameText != "Append") return; diff --git a/src/Tests/Analyzers.Tests/RCS1197OptimizeStringBuilderAppendCallTests.cs b/src/Tests/Analyzers.Tests/RCS1197OptimizeStringBuilderAppendCallTests.cs index 24ab0fdb35..96cee0eff5 100644 --- a/src/Tests/Analyzers.Tests/RCS1197OptimizeStringBuilderAppendCallTests.cs +++ b/src/Tests/Analyzers.Tests/RCS1197OptimizeStringBuilderAppendCallTests.cs @@ -952,6 +952,25 @@ void M() var sb = new StringBuilder().Append($""{Foo}Bar""); } } +"); + } + + [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.OptimizeStringBuilderAppendCall)] + public async Task TestNoDiagnostic_NoAppendMethodChain() + { + await VerifyNoDiagnosticAsync(@" +using System.Text; + +class C +{ + string M() + { + var sb = new StringBuilder(); + _ = sb.AppendLine(); + + return sb.ToString(); + } +} "); } } diff --git a/src/VisualStudioCode/package/CHANGELOG.md b/src/VisualStudioCode/package/CHANGELOG.md index 36c9e18566..f76c1c1288 100644 --- a/src/VisualStudioCode/package/CHANGELOG.md +++ b/src/VisualStudioCode/package/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- Fix [RCS1197](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1197) ([PR](https://github.com/dotnet/roslynator/pull/1226)). + ## [4.6.0] - 2023-10-18 ### Added