From 5c2960713f8f56da0062d35f3cee5d2636c46772 Mon Sep 17 00:00:00 2001 From: Alexander Mai Date: Fri, 19 Feb 2016 21:38:54 +0100 Subject: [PATCH] Correct false positives compareBoolExpressionWithInt (0U) --- lib/checkbool.cpp | 5 +++-- test/testbool.cpp | 13 +++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) 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() {