diff --git a/lib/checkother.cpp b/lib/checkother.cpp index a809fce86..28317dd3e 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1363,12 +1363,12 @@ void CheckOther::checkComparisonOfBoolWithInt() } } else if (Token::Match(tok, "( ! %var% ==|!= %num% )")) { const Token *numTok = tok->tokAt(4); - if (numTok && numTok->str() != "0") { + if (numTok && numTok->str() != "0" && numTok->str() != "1") { comparisonOfBoolWithIntError(numTok, "!"+tok->strAt(2)); } } else if (Token::Match(tok, "( %num% ==|!= ! %var% )")) { const Token *numTok = tok->tokAt(1); - if (numTok && numTok->str() != "0") { + if (numTok && numTok->str() != "0" && numTok->str() != "1") { comparisonOfBoolWithIntError(numTok, "!"+tok->strAt(4)); } } @@ -1378,8 +1378,10 @@ void CheckOther::checkComparisonOfBoolWithInt() void CheckOther::comparisonOfBoolWithIntError(const Token *tok, const std::string &expression) { reportError(tok, Severity::warning, "comparisonOfBoolWithInt", - "Comparison of a boolean with a non-zero integer\n" - "The expression \"" + expression + "\" is of type 'bool' but is compared against a non-zero 'int'."); + "Comparison of a boolean with integer that is neither 1 nor 0\n" + "The expression \"" + expression + "\" is of type 'bool' " + "and it is compared against a integer value that is " + "neither 1 nor 0."); } //--------------------------------------------------------------------------- diff --git a/test/testother.cpp b/test/testother.cpp index eec9e87f2..b0af1fca7 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -134,6 +134,7 @@ private: TEST_CASE(comparisonOfBoolWithInt2); TEST_CASE(comparisonOfBoolWithInt3); TEST_CASE(comparisonOfBoolWithInt4); + TEST_CASE(comparisonOfBoolWithInt5); TEST_CASE(duplicateIf); TEST_CASE(duplicateBranch); @@ -3171,14 +3172,14 @@ private: " printf(\"foo\");\n" " }\n" "}"); - ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean with a non-zero integer\n", errout.str()); + ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean with integer that is neither 1 nor 0\n", errout.str()); check("void f(bool x) {\n" " if (10 >= x) {\n" " printf(\"foo\");\n" " }\n" "}"); - ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean with a non-zero integer\n", errout.str()); + ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean with integer that is neither 1 nor 0\n", errout.str()); check("void f(bool x) {\n" " if (x != 0) {\n" @@ -3192,14 +3193,14 @@ private: " printf(\"foo\");\n" " }\n" "}"); - ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean with a non-zero integer\n", errout.str()); + ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean with integer that is neither 1 nor 0\n", errout.str()); check("void f(bool x) {\n" " if (x == 10) {\n" " printf(\"foo\");\n" " }\n" "}"); - ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean with a non-zero integer\n", errout.str()); + ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean with integer that is neither 1 nor 0\n", errout.str()); check("void f(bool x) {\n" " if (x == 0) {\n" @@ -3215,14 +3216,14 @@ private: " printf(\"foo\");\n" " }\n" "}"); - ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean with a non-zero integer\n", errout.str()); + ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean with integer that is neither 1 nor 0\n", errout.str()); check("void f(int x, bool y) {\n" " if (x == y) {\n" " printf(\"foo\");\n" " }\n" "}"); - ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean with a non-zero integer\n", errout.str()); + ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean with integer that is neither 1 nor 0\n", errout.str()); check("void f(bool x, bool y) {\n" " if (x == y) {\n" @@ -3245,14 +3246,14 @@ private: " printf(\"foo\");\n" " }\n" "}"); - ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean with a non-zero integer\n", errout.str()); + ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean with integer that is neither 1 nor 0\n", errout.str()); check("void f(int y) {\n" " if (true == y) {\n" " printf(\"foo\");\n" " }\n" "}"); - ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean with a non-zero integer\n", errout.str()); + ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean with integer that is neither 1 nor 0\n", errout.str()); check("void f(bool y) {\n" " if (y == true) {\n" @@ -3268,14 +3269,14 @@ private: " printf(\"x not equal to 10\");\n" " }\n" "}"); - ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean with a non-zero integer\n", errout.str()); + ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean with integer that is neither 1 nor 0\n", errout.str()); check("void f(int x) {\n" " if (!x != 10) {\n" " printf(\"x not equal to 10\");\n" " }\n" "}"); - ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean with a non-zero integer\n", errout.str()); + ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean with integer that is neither 1 nor 0\n", errout.str()); check("void f(int x) {\n" " if (x != 10) {\n" @@ -3289,14 +3290,14 @@ private: " printf(\"x not equal to 10\");\n" " }\n" "}"); - ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean with a non-zero integer\n", errout.str()); + ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean with integer that is neither 1 nor 0\n", errout.str()); check("void f(int x) {\n" " if (10 != !x) {\n" " printf(\"x not equal to 10\");\n" " }\n" "}"); - ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean with a non-zero integer\n", errout.str()); + ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean with integer that is neither 1 nor 0\n", errout.str()); check("void f(int x) {\n" " if (10 != x) {\n" @@ -3306,6 +3307,13 @@ private: ASSERT_EQUALS("", errout.str()); } + void comparisonOfBoolWithInt5() { + check("void f(int x) {\n" + " if (!x == 1) { }\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } + void duplicateIf() { check("void f(int a, int &b) {\n" " if (a) { b = 1; }\n"