Skip to content

Commit 927d139

Browse files
Ken-Patrickdanmar
authored andcommitted
Properly check the type of the expressions, instead of using the type of the tokens
1 parent 2c71b47 commit 927d139

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

lib/checkbool.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,9 @@ void CheckBool::checkComparisonOfBoolExpressionWithInt()
350350
// but it is probably written this way by design.
351351
continue;
352352

353+
if (astIsBool(numTok))
354+
continue;
355+
353356
if (numTok->isNumber()) {
354357
const MathLib::bigint num = MathLib::toLongNumber(numTok->str());
355358
if (num==0 &&
@@ -361,7 +364,7 @@ void CheckBool::checkComparisonOfBoolExpressionWithInt()
361364
: Token::Match(tok, ">|==|!=")))
362365
continue;
363366
comparisonOfBoolExpressionWithIntError(tok, true);
364-
} else if (isNonBoolStdType(numTok->variable()) && mTokenizer->isCPP())
367+
} else if (astIsIntegral(numTok, false) && mTokenizer->isCPP())
365368
comparisonOfBoolExpressionWithIntError(tok, false);
366369
}
367370
}

test/testbool.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class TestBool : public TestFixture {
5353
TEST_CASE(comparisonOfBoolWithInt5);
5454
TEST_CASE(comparisonOfBoolWithInt6); // #4224 - integer is casted to bool
5555
TEST_CASE(comparisonOfBoolWithInt7); // #4846 - (!x == true)
56+
TEST_CASE(comparisonOfBoolWithInt8); // #9165
5657

5758
TEST_CASE(checkComparisonOfFuncReturningBool1);
5859
TEST_CASE(checkComparisonOfFuncReturningBool2);
@@ -995,6 +996,50 @@ class TestBool : public TestFixture {
995996
ASSERT_EQUALS("", errout.str());
996997
}
997998

999+
void comparisonOfBoolWithInt8() { // #9165
1000+
check("bool Fun();\n"
1001+
"void Test(bool expectedResult) {\n"
1002+
" auto res = Fun();\n"
1003+
" if (expectedResult == res)\n"
1004+
" throw 2;\n"
1005+
"}");
1006+
ASSERT_EQUALS("", errout.str());
1007+
1008+
check("int Fun();\n"
1009+
"void Test(bool expectedResult) {\n"
1010+
" auto res = Fun();\n"
1011+
" if (expectedResult == res)\n"
1012+
" throw 2;\n"
1013+
"}");
1014+
ASSERT_EQUALS("[test.cpp:4]: (warning) Comparison of a boolean expression with an integer.\n", errout.str());
1015+
1016+
check("bool Fun();\n"
1017+
"void Test(bool expectedResult) {\n"
1018+
" auto res = Fun();\n"
1019+
" if (5 + expectedResult == res)\n"
1020+
" throw 2;\n"
1021+
"}");
1022+
ASSERT_EQUALS("[test.cpp:4]: (warning) Comparison of a boolean expression with an integer.\n", errout.str());
1023+
1024+
fprintf(stderr, "last case\n");
1025+
check("int Fun();\n"
1026+
"void Test(bool expectedResult) {\n"
1027+
" auto res = Fun();\n"
1028+
" if (5 + expectedResult == res)\n"
1029+
" throw 2;\n"
1030+
"}");
1031+
ASSERT_EQUALS("", errout.str());
1032+
1033+
check("int Fun();\n"
1034+
"void Test(bool expectedResult) {\n"
1035+
" auto res = Fun();\n"
1036+
" if (expectedResult == res + 5)\n"
1037+
" throw 2;\n"
1038+
"}");
1039+
ASSERT_EQUALS("[test.cpp:4]: (warning) Comparison of a boolean expression with an integer.\n", errout.str());
1040+
}
1041+
1042+
9981043
void pointerArithBool1() { // #5126
9991044
check("void f(char *p) {\n"
10001045
" if (p+1){}\n"

0 commit comments

Comments
 (0)