From 927d139488c4e68c7c95dbdfedce2f1b136fdc89 Mon Sep 17 00:00:00 2001 From: Ken-Patrick Date: Thu, 27 Jun 2019 07:48:44 +0200 Subject: [PATCH] Fix FP #9165 (#1928) Properly check the type of the expressions, instead of using the type of the tokens --- lib/checkbool.cpp | 5 ++++- test/testbool.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/lib/checkbool.cpp b/lib/checkbool.cpp index df694c13d..67c3ae924 100644 --- a/lib/checkbool.cpp +++ b/lib/checkbool.cpp @@ -350,6 +350,9 @@ void CheckBool::checkComparisonOfBoolExpressionWithInt() // but it is probably written this way by design. continue; + if (astIsBool(numTok)) + continue; + if (numTok->isNumber()) { const MathLib::bigint num = MathLib::toLongNumber(numTok->str()); if (num==0 && @@ -361,7 +364,7 @@ void CheckBool::checkComparisonOfBoolExpressionWithInt() : Token::Match(tok, ">|==|!="))) continue; comparisonOfBoolExpressionWithIntError(tok, true); - } else if (isNonBoolStdType(numTok->variable()) && mTokenizer->isCPP()) + } else if (astIsIntegral(numTok, false) && mTokenizer->isCPP()) comparisonOfBoolExpressionWithIntError(tok, false); } } diff --git a/test/testbool.cpp b/test/testbool.cpp index daf379b5e..893c8ace5 100644 --- a/test/testbool.cpp +++ b/test/testbool.cpp @@ -53,6 +53,7 @@ private: TEST_CASE(comparisonOfBoolWithInt5); TEST_CASE(comparisonOfBoolWithInt6); // #4224 - integer is casted to bool TEST_CASE(comparisonOfBoolWithInt7); // #4846 - (!x == true) + TEST_CASE(comparisonOfBoolWithInt8); // #9165 TEST_CASE(checkComparisonOfFuncReturningBool1); TEST_CASE(checkComparisonOfFuncReturningBool2); @@ -995,6 +996,50 @@ private: ASSERT_EQUALS("", errout.str()); } + void comparisonOfBoolWithInt8() { // #9165 + check("bool Fun();\n" + "void Test(bool expectedResult) {\n" + " auto res = Fun();\n" + " if (expectedResult == res)\n" + " throw 2;\n" + "}"); + ASSERT_EQUALS("", errout.str()); + + check("int Fun();\n" + "void Test(bool expectedResult) {\n" + " auto res = Fun();\n" + " if (expectedResult == res)\n" + " throw 2;\n" + "}"); + ASSERT_EQUALS("[test.cpp:4]: (warning) Comparison of a boolean expression with an integer.\n", errout.str()); + + check("bool Fun();\n" + "void Test(bool expectedResult) {\n" + " auto res = Fun();\n" + " if (5 + expectedResult == res)\n" + " throw 2;\n" + "}"); + ASSERT_EQUALS("[test.cpp:4]: (warning) Comparison of a boolean expression with an integer.\n", errout.str()); + + fprintf(stderr, "last case\n"); + check("int Fun();\n" + "void Test(bool expectedResult) {\n" + " auto res = Fun();\n" + " if (5 + expectedResult == res)\n" + " throw 2;\n" + "}"); + ASSERT_EQUALS("", errout.str()); + + check("int Fun();\n" + "void Test(bool expectedResult) {\n" + " auto res = Fun();\n" + " if (expectedResult == res + 5)\n" + " throw 2;\n" + "}"); + ASSERT_EQUALS("[test.cpp:4]: (warning) Comparison of a boolean expression with an integer.\n", errout.str()); + } + + void pointerArithBool1() { // #5126 check("void f(char *p) {\n" " if (p+1){}\n"