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

Fix S3247 FN: When cast expression contains parentheses #9492

Merged
merged 2 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ private static void ReportPatternAtCastLocation(SonarSyntaxNodeReportingContext

private static bool IsEquivalentVariable(ExpressionSyntax expression, SyntaxNode typedVariable)
{
var left = RemoveThisExpression(typedVariable).WithoutTrivia();
var right = RemoveThisExpression(expression).WithoutTrivia();
var left = CleanupExpression(typedVariable).WithoutTrivia();
var right = CleanupExpression(expression).WithoutTrivia();

return left.IsEquivalentTo(right)
|| (StandaloneIdentifier(left) is { } leftIdentifier && leftIdentifier == StandaloneIdentifier(right));
Expand All @@ -245,8 +245,17 @@ static string StandaloneIdentifier(SyntaxNode node) =>
_ when node.IsKind(SyntaxKindEx.SingleVariableDesignation) => ((SingleVariableDesignationSyntaxWrapper)node).Identifier.ValueText,
_ => null
};
}

private static SyntaxNode CleanupExpression(SyntaxNode node)
{
while (node is ParenthesizedExpressionSyntax parenthesized)
{
node = parenthesized.Expression;
}

node = node is MemberAccessExpressionSyntax { Expression: ThisExpressionSyntax } memberAccess ? memberAccess.Name : node;

static SyntaxNode RemoveThisExpression(SyntaxNode node) =>
node is MemberAccessExpressionSyntax { Expression: ThisExpressionSyntax } memberAccess ? memberAccess.Name : node;
return node;
mary-georgiou-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ public void IgnoreMemberAccess(Fruit arg)
_ = (Fruit)differentInstance.Property;
}

// https://github.com/SonarSource/sonar-dotnet/issues/9491
if (arg.Property is Fruit) // Noncompliant
{
_ = ((Fruit)(arg.Property)).Property; // Secondary
}

if (arg.Property is Fruit) // Noncompliant
{
_ = ((Fruit)((arg.Property))).Property; // Secondary
}

if (arg.Property is Fruit) // Noncompliant
{
_ = ((Fruit)arg.Property).Property; // Secondary
}

if (f.Property is Fruit) // Compliant, the cast is on a different instance
{
_ = (Fruit)differentInstance.Property;
Expand Down Expand Up @@ -128,6 +144,12 @@ public void TakeIdentifierIntoAccount(object x)
var fruit = (Fruit)this.someField;
// ^^^^^^^^^^^^^^^^^^^^^ Secondary
}

if (someField is Fruit) // Noncompliant
{
var fruit = ((Fruit)(this.someField));
// ^^^^^^^^^^^^^^^^^^^^^^^ Secondary
}
}

public void UnknownFoo(object x)
Expand Down
Loading