Skip to content

Commit

Permalink
Fix #7462
Browse files Browse the repository at this point in the history
  • Loading branch information
CristianAmbrosini authored and martin-strecker-sonarsource committed Aug 4, 2023
1 parent cbdb886 commit 5f8a553
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ private static Document RewriteConditional(Document document, SyntaxNode root, S
root,
SyntaxKind.LogicalOrExpression,
conditional.Condition,
conditional.WhenFalse);
ParenthesizeIfComplex(conditional.WhenFalse));

return document.WithSyntaxRoot(newRoot);
}
Expand All @@ -300,7 +300,7 @@ private static Document RewriteConditional(Document document, SyntaxNode root, S
root,
SyntaxKind.LogicalAndExpression,
GetNegatedExpression(conditional.Condition),
conditional.WhenFalse);
ParenthesizeIfComplex(conditional.WhenFalse));

return document.WithSyntaxRoot(newRoot);
}
Expand All @@ -314,7 +314,7 @@ private static Document RewriteConditional(Document document, SyntaxNode root, S
root,
SyntaxKind.LogicalOrExpression,
GetNegatedExpression(conditional.Condition),
conditional.WhenTrue);
ParenthesizeIfComplex(conditional.WhenTrue));

return document.WithSyntaxRoot(newRoot);
}
Expand All @@ -326,23 +326,21 @@ private static Document RewriteConditional(Document document, SyntaxNode root, S
root,
SyntaxKind.LogicalAndExpression,
conditional.Condition,
conditional.WhenTrue);
ParenthesizeIfComplex(conditional.WhenTrue));

return document.WithSyntaxRoot(newRoot);
}

return document;
}

private static ExpressionSyntax GetNegatedExpression(ExpressionSyntax expression)
{
var exp = expression;
if (expression is BinaryExpressionSyntax or ConditionalExpressionSyntax
|| IsPatternExpressionSyntaxWrapper.IsInstance(expression))
{
exp = SyntaxFactory.ParenthesizedExpression(expression);
}
return SyntaxFactory.PrefixUnaryExpression(SyntaxKind.LogicalNotExpression, exp);
}
private static ExpressionSyntax GetNegatedExpression(ExpressionSyntax expression) =>
SyntaxFactory.PrefixUnaryExpression(SyntaxKind.LogicalNotExpression, ParenthesizeIfComplex(expression));

private static ExpressionSyntax ParenthesizeIfComplex(ExpressionSyntax expression) =>
expression is BinaryExpressionSyntax or ConditionalExpressionSyntax
|| IsPatternExpressionSyntaxWrapper.IsInstance(expression)
? SyntaxFactory.ParenthesizedExpression(expression)
: expression;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ public void ThrowExpressionIsIgnored(bool condition, int number)
x = condition is true ? throw new Exception() : false;
}
}

// https://github.com/SonarSource/sonar-dotnet/issues/7462
public void Repro_7462(object obj)
{
var a = !(obj == null) && (obj.ToString() == "x" || obj.ToString() == "y"); // Fixed
}
}

public class Item
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,12 @@ public void ThrowExpressionIsIgnored(bool condition, int number)
x = condition is true ? throw new Exception() : false;
}
}

// https://github.com/SonarSource/sonar-dotnet/issues/7462
public void Repro_7462(object obj)
{
var a = obj == null ? false : obj.ToString() == "x" || obj.ToString() == "y"; // Noncompliant
}
}

public class Item
Expand Down

0 comments on commit 5f8a553

Please sign in to comment.