From bbba6ef10eea4988365bc4a705eb93f80c6d8552 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl-Johan=20Sj=C3=B6gren?= Date: Sun, 10 Jul 2022 10:57:39 +0200 Subject: [PATCH 1/6] Now adding parenthesis around non literal expressions in OptimizeStringBuilderAppendCallCodeFixProvider --- ...eStringBuilderAppendCallCodeFixProvider.cs | 8 ++++- ...197OptimizeStringBuilderAppendCallTests.cs | 36 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/Analyzers.CodeFixes/CSharp/CodeFixes/OptimizeStringBuilderAppendCallCodeFixProvider.cs b/src/Analyzers.CodeFixes/CSharp/CodeFixes/OptimizeStringBuilderAppendCallCodeFixProvider.cs index 6c9e1081dc..43853eb2b8 100644 --- a/src/Analyzers.CodeFixes/CSharp/CodeFixes/OptimizeStringBuilderAppendCallCodeFixProvider.cs +++ b/src/Analyzers.CodeFixes/CSharp/CodeFixes/OptimizeStringBuilderAppendCallCodeFixProvider.cs @@ -258,13 +258,19 @@ private static InvocationExpressionSyntax CreateInvocationExpression( { case 1: { + ExpressionSyntax argumentExpression = arguments[0].Expression; + if (argumentExpression is not LiteralExpressionSyntax) + { + argumentExpression = ParenthesizedExpression(arguments[0].Expression); + } + ArgumentListSyntax argumentList = ArgumentList( Argument(invocationInfo.Expression), arguments[0], Argument( SubtractExpression( SimpleMemberAccessExpression(invocationInfo.Expression, IdentifierName("Length")), - arguments[0].Expression))); + argumentExpression))); return CreateNewInvocationExpression(outerInvocationExpression, "Append", argumentList); } diff --git a/src/Tests/Analyzers.Tests/RCS1197OptimizeStringBuilderAppendCallTests.cs b/src/Tests/Analyzers.Tests/RCS1197OptimizeStringBuilderAppendCallTests.cs index b83a65dfbb..123b11d7e9 100644 --- a/src/Tests/Analyzers.Tests/RCS1197OptimizeStringBuilderAppendCallTests.cs +++ b/src/Tests/Analyzers.Tests/RCS1197OptimizeStringBuilderAppendCallTests.cs @@ -44,6 +44,42 @@ void M() "); } + [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.OptimizeStringBuilderAppendCall)] + public async Task Test_Substring_Int32_Int32_Calculation() + { + await VerifyDiagnosticAndFixAsync(@" +using System.Text; + +class C +{ + void M() + { + string s = null; + var start = 2; + var len = 5; + var sb = new StringBuilder(); + + sb.Append([|s.Substring(start + len)|]); + } +} +", @" +using System.Text; + +class C +{ + void M() + { + string s = null; + var start = 2; + var len = 5; + var sb = new StringBuilder(); + + sb.Append(s, start + len, s.Length - (start + len)); + } +} +"); + } + [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.OptimizeStringBuilderAppendCall)] public async Task Test_Substring_Int32() { From 8ccbd3ba709d99a15bc201c55f278a8fcb291ed0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl-Johan=20Sj=C3=B6gren?= Date: Sun, 10 Jul 2022 11:02:08 +0200 Subject: [PATCH 2/6] Removed uneccessary array access --- .../CodeFixes/OptimizeStringBuilderAppendCallCodeFixProvider.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Analyzers.CodeFixes/CSharp/CodeFixes/OptimizeStringBuilderAppendCallCodeFixProvider.cs b/src/Analyzers.CodeFixes/CSharp/CodeFixes/OptimizeStringBuilderAppendCallCodeFixProvider.cs index 43853eb2b8..d6ba583c8b 100644 --- a/src/Analyzers.CodeFixes/CSharp/CodeFixes/OptimizeStringBuilderAppendCallCodeFixProvider.cs +++ b/src/Analyzers.CodeFixes/CSharp/CodeFixes/OptimizeStringBuilderAppendCallCodeFixProvider.cs @@ -261,7 +261,7 @@ private static InvocationExpressionSyntax CreateInvocationExpression( ExpressionSyntax argumentExpression = arguments[0].Expression; if (argumentExpression is not LiteralExpressionSyntax) { - argumentExpression = ParenthesizedExpression(arguments[0].Expression); + argumentExpression = ParenthesizedExpression(argumentExpression); } ArgumentListSyntax argumentList = ArgumentList( From 77afd84cc38849ec0c187913244f027166f22f10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karl-Johan=20Sj=C3=B6gren?= Date: Sun, 17 Jul 2022 23:26:44 +0200 Subject: [PATCH 3/6] Switched to using Expression.Parenthesize() instead of manually doing the check --- .../OptimizeStringBuilderAppendCallCodeFixProvider.cs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/Analyzers.CodeFixes/CSharp/CodeFixes/OptimizeStringBuilderAppendCallCodeFixProvider.cs b/src/Analyzers.CodeFixes/CSharp/CodeFixes/OptimizeStringBuilderAppendCallCodeFixProvider.cs index d6ba583c8b..85a63747ad 100644 --- a/src/Analyzers.CodeFixes/CSharp/CodeFixes/OptimizeStringBuilderAppendCallCodeFixProvider.cs +++ b/src/Analyzers.CodeFixes/CSharp/CodeFixes/OptimizeStringBuilderAppendCallCodeFixProvider.cs @@ -258,19 +258,13 @@ private static InvocationExpressionSyntax CreateInvocationExpression( { case 1: { - ExpressionSyntax argumentExpression = arguments[0].Expression; - if (argumentExpression is not LiteralExpressionSyntax) - { - argumentExpression = ParenthesizedExpression(argumentExpression); - } - ArgumentListSyntax argumentList = ArgumentList( Argument(invocationInfo.Expression), arguments[0], Argument( SubtractExpression( SimpleMemberAccessExpression(invocationInfo.Expression, IdentifierName("Length")), - argumentExpression))); + arguments[0].Expression.Parenthesize()))); return CreateNewInvocationExpression(outerInvocationExpression, "Append", argumentList); } From 937337d4b5fe9544f780369a34074e3fcc483ad4 Mon Sep 17 00:00:00 2001 From: Josef Pihrt Date: Mon, 18 Jul 2022 13:34:56 +0200 Subject: [PATCH 4/6] Update changelog --- ChangeLog.md | 1 + 1 file changed, 1 insertion(+) diff --git a/ChangeLog.md b/ChangeLog.md index 62e4256d4c..ce750ff83b 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 - [CLI] Fix filtering of projects (relates to `--projects` or `--ignored-projects` parameter) ([#914](https://github.com/josefpihrt/roslynator/pull/914)). - Refactoring "Add using directive" (RR0014) now works when file-scoped namespace is used ([#932](https://github.com/josefpihrt/roslynator/pull/932)). +- Add parentheses if necessary in a code fix for [RCS1197](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1197.md) ([#928](https://github.com/josefpihrt/roslynator/pull/928) by @karl-sjogren). ----- From b08769fffdb41e8e6a54008fda2c6013da9a315a Mon Sep 17 00:00:00 2001 From: Josef Pihrt Date: Mon, 18 Jul 2022 13:38:53 +0200 Subject: [PATCH 5/6] Update changelog --- ChangeLog.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ChangeLog.md b/ChangeLog.md index ce750ff83b..374e6e64b0 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -25,7 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - [CLI] Fix filtering of projects (relates to `--projects` or `--ignored-projects` parameter) ([#914](https://github.com/josefpihrt/roslynator/pull/914)). - Refactoring "Add using directive" (RR0014) now works when file-scoped namespace is used ([#932](https://github.com/josefpihrt/roslynator/pull/932)). -- Add parentheses if necessary in a code fix for [RCS1197](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1197.md) ([#928](https://github.com/josefpihrt/roslynator/pull/928) by @karl-sjogren). +- Add parentheses if necessary in a code fix for [RCS1197](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1197.md) ([#928](https://github.com/josefpihrt/roslynator/pull/928) by [https://github.com/karl-sjogren](karl-sjogren)). ----- From 860092e9ddabf3acc9fa7c648cba2838e957eb1a Mon Sep 17 00:00:00 2001 From: Josef Pihrt Date: Mon, 18 Jul 2022 13:40:29 +0200 Subject: [PATCH 6/6] Update changelog --- ChangeLog.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ChangeLog.md b/ChangeLog.md index 374e6e64b0..ce750ff83b 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -25,7 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - [CLI] Fix filtering of projects (relates to `--projects` or `--ignored-projects` parameter) ([#914](https://github.com/josefpihrt/roslynator/pull/914)). - Refactoring "Add using directive" (RR0014) now works when file-scoped namespace is used ([#932](https://github.com/josefpihrt/roslynator/pull/932)). -- Add parentheses if necessary in a code fix for [RCS1197](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1197.md) ([#928](https://github.com/josefpihrt/roslynator/pull/928) by [https://github.com/karl-sjogren](karl-sjogren)). +- Add parentheses if necessary in a code fix for [RCS1197](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1197.md) ([#928](https://github.com/josefpihrt/roslynator/pull/928) by @karl-sjogren). -----