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

Improve RCS1097 #1160

Merged
merged 14 commits into from
Aug 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Add SECURITY.md ([#1147](https://github.com/josefpihrt/roslynator/pull/1147))
- Add more cases to [RCS1097](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1097.md) ([#1160](https://github.com/JosefPihrt/Roslynator/pull/1160)).

### Fixed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,30 @@ public static void Analyze(SyntaxNodeAnalysisContext context, in SimpleMemberInv
if (containingType.SpecialType == SpecialType.System_String)
return true;

if (invocationExpression.WalkUpParentheses().IsParentKind(SyntaxKind.Interpolation)
&& IsNotHidden(methodSymbol, containingType))
ExpressionSyntax expression = invocationExpression.WalkUpParentheses();
switch (expression.Parent)
{
return true;
case InterpolationSyntax:
{
if (IsNotHidden(methodSymbol, containingType))
return true;

break;
}
case BinaryExpressionSyntax { RawKind: (int)SyntaxKind.AddExpression } addExpression:
josefpihrt marked this conversation as resolved.
Show resolved Hide resolved
{
if (addExpression.Right == expression
&& semanticModel.GetTypeInfo(addExpression.Left, cancellationToken).Type?.SpecialType == SpecialType.System_String)
josefpihrt marked this conversation as resolved.
Show resolved Hide resolved
return true;
josefpihrt marked this conversation as resolved.
Show resolved Hide resolved

if (addExpression.Left == expression
josefpihrt marked this conversation as resolved.
Show resolved Hide resolved
&& semanticModel.GetTypeInfo(addExpression.Right, cancellationToken).Type?.SpecialType == SpecialType.System_String
jamesHargreaves12 marked this conversation as resolved.
Show resolved Hide resolved
&& (addExpression.Right.WalkDownParentheses() is not InvocationExpressionSyntax invocationExpression2
|| semanticModel.GetMethodSymbol(invocationExpression2, cancellationToken).Name != "ToString"))
josefpihrt marked this conversation as resolved.
Show resolved Hide resolved
return true;
josefpihrt marked this conversation as resolved.
Show resolved Hide resolved

break;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,32 @@ void M()
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.RemoveRedundantToStringCall)]
public async Task Test_PlusString()
{
await VerifyDiagnosticAndFixAsync(@"
class C
{
void M(object o)
{
string s = """" + o[|.ToString()|];
string s2 = o[|.ToString()|] + """";
string s3 = o.ToString() + o[|.ToString()|];
}
}
", @"
class C
{
void M(object o)
{
string s = """" + o;
string s2 = o + """";
string s3 = o.ToString() + o;
}
}
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.RemoveRedundantToStringCall)]
public async Task Test_Interpolation()
{
Expand Down