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 all 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 @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Add SECURITY.md ([#1147](https://github.com/josefpihrt/roslynator/pull/1147))
- Add custom FixAllProvider for [RCS1014](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1014.md) ([#1070](https://github.com/JosefPihrt/Roslynator/pull/1070)).
- 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 @@ -48,12 +48,7 @@ public static void Analyze(SyntaxNodeAnalysisContext context, in SimpleMemberInv

IMethodSymbol methodSymbol = semanticModel.GetMethodSymbol(invocationExpression, cancellationToken);

if (methodSymbol?.DeclaredAccessibility == Accessibility.Public
&& !methodSymbol.IsStatic
&& !methodSymbol.IsGenericMethod
&& string.Equals(methodSymbol.Name, "ToString", StringComparison.Ordinal)
&& methodSymbol.ReturnType.SpecialType == SpecialType.System_String
&& !methodSymbol.Parameters.Any())
if (IsToString(methodSymbol))
{
INamedTypeSymbol containingType = methodSymbol.ContainingType;

Expand All @@ -64,15 +59,38 @@ 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.Kind())
{
return true;
case SyntaxKind.Interpolation:
{
return IsNotHidden(methodSymbol, containingType);
}
case SyntaxKind.AddExpression:
{
var addExpression = (BinaryExpressionSyntax)expression.Parent;
if (addExpression.Right == expression)
return semanticModel.GetTypeInfo(addExpression.Left, cancellationToken).Type?.SpecialType == SpecialType.System_String;

return semanticModel.GetTypeInfo(addExpression.Right, cancellationToken).Type?.SpecialType == SpecialType.System_String
&& (addExpression.Right.WalkDownParentheses() is not InvocationExpressionSyntax invocationExpression2
|| !IsToString(semanticModel.GetMethodSymbol(invocationExpression2, cancellationToken)));
}
}
}
}

return false;

static bool IsToString(IMethodSymbol methodSymbol)
{
return methodSymbol?.DeclaredAccessibility == Accessibility.Public
&& !methodSymbol.IsStatic
&& !methodSymbol.IsGenericMethod
&& string.Equals(methodSymbol.Name, "ToString", StringComparison.Ordinal)
&& methodSymbol.ReturnType.SpecialType == SpecialType.System_String
&& !methodSymbol.Parameters.Any();
}
}

private static bool IsNotHidden(IMethodSymbol methodSymbol, INamedTypeSymbol containingType)
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