Skip to content
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 @@ -223,10 +223,28 @@ private static SyntaxNode FindNodeToKeep(BinaryExpressionSyntax binary)

#endregion

#region ==/!= one side boolean
Comment thread
gregory-paidis-sonarsource marked this conversation as resolved.
Outdated

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);
}
Comment thread
costin-zaharia-sonarsource marked this conversation as resolved.
}

return CSharpEquivalenceChecker.AreEquivalent(binary.Left, CSharpSyntaxHelper.TrueLiteralExpression)
|| CSharpEquivalenceChecker.AreEquivalent(binary.Left, CSharpSyntaxHelper.FalseLiteralExpression)
? binary.Right
: binary.Left;

#endregion
}

private static bool TwoSidesAreDifferentBooleans(BinaryExpressionSyntax binary) =>
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
Comment thread
costin-zaharia-sonarsource marked this conversation as resolved.
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
Copy Markdown
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