diff --git a/lib/checkbool.cpp b/lib/checkbool.cpp index 4775ccead..5d1cd9cea 100644 --- a/lib/checkbool.cpp +++ b/lib/checkbool.cpp @@ -353,11 +353,12 @@ void CheckBool::checkComparisonOfBoolExpressionWithInt() continue; if (numTok->isNumber()) { - if (numTok->str() == "0" && + const MathLib::bigint num = MathLib::toLongNumber(numTok->str()); + if (num==0 && (numInRhs ? Token::Match(tok, ">|==|!=") : Token::Match(tok, "<|==|!="))) continue; - if (numTok->str() == "1" && + if (num==1 && (numInRhs ? Token::Match(tok, "<|==|!=") : Token::Match(tok, ">|==|!="))) continue; diff --git a/test/testbool.cpp b/test/testbool.cpp index ad6e22bfd..f54156b8e 100644 --- a/test/testbool.cpp +++ b/test/testbool.cpp @@ -538,6 +538,19 @@ private: " return a > b or c < d;\n" "}"); ASSERT_EQUALS("", errout.str()); + + check("int f() {\n" + " return (a < b) != 0U;\n" + "}"); + ASSERT_EQUALS("", errout.str()); + check("int f() {\n" + " return (a < b) != 0x0;\n" + "}"); + ASSERT_EQUALS("", errout.str()); + check("int f() {\n" + " return (a < b) != 42U;\n" + "}"); + ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of a boolean expression with an integer other than 0 or 1.\n", errout.str()); } void checkComparisonOfFuncReturningBool1() {