diff --git a/analyzers/src/SonarAnalyzer.CSharp/Rules/BooleanLiteralUnnecessaryCodeFix.cs b/analyzers/src/SonarAnalyzer.CSharp/Rules/BooleanLiteralUnnecessaryCodeFix.cs index 3944e313cd5..0b30816439e 100644 --- a/analyzers/src/SonarAnalyzer.CSharp/Rules/BooleanLiteralUnnecessaryCodeFix.cs +++ b/analyzers/src/SonarAnalyzer.CSharp/Rules/BooleanLiteralUnnecessaryCodeFix.cs @@ -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))) @@ -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)) { @@ -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) diff --git a/analyzers/tests/SonarAnalyzer.Test/TestCases/BooleanLiteralUnnecessary.Fixed.cs b/analyzers/tests/SonarAnalyzer.Test/TestCases/BooleanLiteralUnnecessary.Fixed.cs index 47f5436f08a..86681af25c8 100644 --- a/analyzers/tests/SonarAnalyzer.Test/TestCases/BooleanLiteralUnnecessary.Fixed.cs +++ b/analyzers/tests/SonarAnalyzer.Test/TestCases/BooleanLiteralUnnecessary.Fixed.cs @@ -34,7 +34,7 @@ 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 @@ -42,7 +42,7 @@ public BooleanLiteralUnnecessary(bool a, bool b, bool? c, Item item) 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] @@ -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 } } diff --git a/analyzers/tests/SonarAnalyzer.Test/TestCases/BooleanLiteralUnnecessary.cs b/analyzers/tests/SonarAnalyzer.Test/TestCases/BooleanLiteralUnnecessary.cs index 2f6f2608efa..6b5f06e165d 100644 --- a/analyzers/tests/SonarAnalyzer.Test/TestCases/BooleanLiteralUnnecessary.cs +++ b/analyzers/tests/SonarAnalyzer.Test/TestCases/BooleanLiteralUnnecessary.cs @@ -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 } }