Properly check the type of the expressions, instead of using the type of the tokens
This commit is contained in:
parent
2c71b47d4e
commit
927d139488
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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"
|
||||
|
|
Loading…
Reference in New Issue