Skip to content

Commit

Permalink
Fix #7462
Browse files Browse the repository at this point in the history
  • Loading branch information
CristianAmbrosini committed Aug 3, 2023
1 parent 0a50eb2 commit 32926cc
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 @@ -257,7 +257,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 @@ -269,7 +269,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 @@ -283,7 +283,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 @@ -295,23 +295,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 @@ -152,6 +152,12 @@ public void ThrowExpressionIsIgnored(bool condition, int number)
x = condition != 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 @@ -164,6 +164,12 @@ public void ThrowExpressionIsIgnored(bool condition, int number)
x = condition != 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 32926cc

Please sign in to comment.