Skip to content
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.
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 @@ -181,8 +181,7 @@ private static void RegisterBinaryExpressionReplacement(SonarCodeFixContext cont

private static SyntaxNode FindNodeToKeep(BinaryExpressionSyntax binary)
{
#region logical and false, logical or true

// logical and false, logical or true
if (binary.IsKind(SyntaxKind.LogicalAndExpression)
&& (CSharpEquivalenceChecker.AreEquivalent(binary.Left, CSharpSyntaxHelper.FalseLiteralExpression)
|| CSharpEquivalenceChecker.AreEquivalent(binary.Right, CSharpSyntaxHelper.FalseLiteralExpression)))
Expand All @@ -196,10 +195,7 @@ private static SyntaxNode FindNodeToKeep(BinaryExpressionSyntax binary)
return CSharpSyntaxHelper.TrueLiteralExpression;
}

#endregion

#region ==/!= both sides booleans

// ==/!= both sides booleans
if (binary.IsKind(SyntaxKind.EqualsExpression)
&& TwoSidesAreDifferentBooleans(binary))
{
Expand All @@ -221,7 +217,20 @@ private static SyntaxNode FindNodeToKeep(BinaryExpressionSyntax binary)
return CSharpSyntaxHelper.TrueLiteralExpression;
}

#endregion
// ==/!= one side boolean
if (binary.IsKind(SyntaxKind.EqualsExpression))
{
// edge case [condition == false] -> !condition
if (CSharpEquivalenceChecker.AreEquivalent(binary.Right, CSharpSyntaxHelper.FalseLiteralExpression))
{
return SyntaxFactory.PrefixUnaryExpression(SyntaxKind.LogicalNotExpression, binary.Left);
}
// edge case [false == condition] -> !condition
if (CSharpEquivalenceChecker.AreEquivalent(binary.Left, CSharpSyntaxHelper.FalseLiteralExpression))
{
return SyntaxFactory.PrefixUnaryExpression(SyntaxKind.LogicalNotExpression, binary.Right);
}
}

return CSharpEquivalenceChecker.AreEquivalent(binary.Left, CSharpSyntaxHelper.TrueLiteralExpression)
|| CSharpEquivalenceChecker.AreEquivalent(binary.Left, CSharpSyntaxHelper.FalseLiteralExpression)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ public BooleanLiteralUnnecessary(bool a, bool b, bool? c, Item item)
var x = false; // Fixed
x = true; // Fixed
x = true; // Fixed
x = a; // Fixed
x = !a; // Fixed
x = !a; // Fixed

x = a; // Fixed
x = a; // Fixed
x = a; // Fixed
x = a; // Fixed
x = !a; // Fixed
x = a; // Fixed
x = !a; // Fixed
x = a; // Fixed
x = false is a; // Error [CS9135]
x = true is a; // Error [CS9135]
Expand Down Expand Up @@ -211,7 +211,17 @@ class Repro7999CodeFixError
{
void Method(bool cond)
{
if (cond) { } // Fixed
if (!cond) { } // Fixed
if (cond) { } // Fixed

if (cond) { } // Fixed
if (!cond) { } // Fixed

if (!cond) { } // Fixed
if (cond) { } // Fixed

if (cond) { } // Fixed
if (!cond) { } // Fixed
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,17 @@ class Repro7999CodeFixError
{
void Method(bool cond)
{
if (cond == false) { } // Noncompliant, TP but code fix is wrong - it should be fixed to "if (!cond)"
if (cond == false) { } // Noncompliant
if (cond != false) { } // Noncompliant

if (cond == true) { } // Noncompliant
if (cond != true) { } // Noncompliant

if (false == cond) { } // Noncompliant
if (false != cond) { } // Noncompliant

if (true == cond) { } // Noncompliant
if (true != cond) { } // Noncompliant
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

6/8 of these are not impacted, only x == false and false == x has special parsing.
I added all of them because they are best friends and like to hang out together (and I wanted to make sure I did not break them).

}
}

Expand Down