Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code fix for RCS1197 should add parentheses if necessary #928

Merged
merged 7 commits into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

-----
<!-- Content below does not adhere to 'Keep a Changelog' format -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ private static InvocationExpressionSyntax CreateInvocationExpression(
Argument(
SubtractExpression(
SimpleMemberAccessExpression(invocationInfo.Expression, IdentifierName("Length")),
arguments[0].Expression)));
arguments[0].Expression.Parenthesize())));

return CreateNewInvocationExpression(outerInvocationExpression, "Append", argumentList);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down