From bf91454136a072492734d28d9f59272027918763 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 2 Dec 2012 08:34:30 +0100 Subject: [PATCH] Fixed #4224 (False positive: Comparison of a boolean with an integer (neglecting a cast)) --- lib/checkother.h | 2 +- test/testother.cpp | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/checkother.h b/lib/checkother.h index 251a8d09c..1ffca78a5 100644 --- a/lib/checkother.h +++ b/lib/checkother.h @@ -75,6 +75,7 @@ public: checkOther.checkVariableScope(); checkOther.clarifyCondition(); // not simplified because ifAssign checkOther.checkComparisonOfBoolExpressionWithInt(); + checkOther.checkComparisonOfBoolWithInt(); checkOther.checkSignOfUnsignedVariable(); // don't ignore casts (#3574) checkOther.checkIncompleteArrayFill(); checkOther.checkSuspiciousStringCompare(); @@ -103,7 +104,6 @@ public: checkOther.checkMemsetZeroBytes(); checkOther.checkIncorrectStringCompare(); checkOther.checkIncrementBoolean(); - checkOther.checkComparisonOfBoolWithInt(); checkOther.checkSwitchCaseFallThrough(); checkOther.checkAlwaysTrueOrFalseStringCompare(); checkOther.checkModuloAlwaysTrueFalse(); diff --git a/test/testother.cpp b/test/testother.cpp index d86707703..9a964c6c1 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -144,6 +144,7 @@ private: TEST_CASE(comparisonOfBoolWithInt3); TEST_CASE(comparisonOfBoolWithInt4); TEST_CASE(comparisonOfBoolWithInt5); + TEST_CASE(comparisonOfBoolWithInt6); // #4224 - integer is casted to bool TEST_CASE(checkComparisonOfFuncReturningBool1); TEST_CASE(checkComparisonOfFuncReturningBool2); @@ -3604,8 +3605,7 @@ private: " a++;\n" "}\n" ); - ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean expression with an integer other than 0 or 1.\n" - "[test.cpp:2]: (warning) Comparison of a boolean with an integer.\n", errout.str()); // The second message appears because the code is simplified to "true == 6". Its neither wrong nor necessary. + ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean expression with an integer other than 0 or 1.\n", errout.str()); check("void f(int x) {\n" " if ((x || 0x0f)==0)\n" @@ -4730,6 +4730,13 @@ private: ASSERT_EQUALS("", errout.str()); } + void comparisonOfBoolWithInt6() { // #4224 - integer is casted to bool + check("void SetVisible(bool b, int i) {\n" + " if (b == (bool)i) { }\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } + void duplicateIf() { check("void f(int a, int &b) {\n" " if (a) { b = 1; }\n"