Skip to content

Commit 83f9503

Browse files
committed
CheckBool: Fixed false positives for non-bool expressions that contain ! operator
1 parent 023d0e7 commit 83f9503

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

lib/checkbool.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -378,9 +378,11 @@ static bool isNonBoolLHSExpr(const Token *tok)
378378
nonBoolExpr = true;
379379
else if (tok->varId() && isNonBoolStdType(tok->variable()))
380380
nonBoolExpr = true;
381-
else if (tok->isArithmeticalOp())
381+
else if (tok->isArithmeticalOp()) {
382+
if (indentlevel == 0)
383+
return true;
382384
nonBoolExpr = true;
383-
else if (tok->isComparisonOp() || (tok->str() == "!" && tok->previous()->str()=="("))
385+
} else if (tok->isComparisonOp() || (tok->str() == "!" && tok->previous()->str()=="("))
384386
return false;
385387
else if (indentlevel == 0 && Token::Match(tok,"[;{}=?:&|^,]"))
386388
break;
@@ -425,7 +427,7 @@ void CheckBool::checkComparisonOfBoolExpressionWithInt()
425427
op = opTok->str()[0]=='>'?'<':'>';
426428
}
427429

428-
else if (Token::Match(tok, "! %var% %comp% %any%")) {
430+
else if (Token::Match(tok, "! %var% %comp% %any%") && !isNonBoolLHSExpr(tok)) {
429431
numTok = tok->tokAt(3);
430432
opTok = tok->tokAt(2);
431433
if (Token::Match(opTok, "<|>"))

test/testbool.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,11 @@ class TestBool : public TestFixture {
307307
"}");
308308
ASSERT_EQUALS("", errout.str());
309309

310-
check("int f() { return !a+b<c; }");
311-
ASSERT_EQUALS("",errout.str());
310+
check("int f() { return !a+b<c; }"); // #5072
311+
ASSERT_EQUALS("",errout.str());
312+
313+
check("int f() { return (!a+b<c); }");
314+
ASSERT_EQUALS("",errout.str());
312315
}
313316

314317
void comparisonOfBoolExpressionWithInt2() {
@@ -377,6 +380,9 @@ class TestBool : public TestFixture {
377380
" return (x()+1 == !a);\n"
378381
"}");
379382
TODO_ASSERT_EQUALS("error", "", errout.str());
383+
384+
check("void f() { if (!!a+!!b+!!c>1){} }");
385+
ASSERT_EQUALS("",errout.str());
380386
}
381387

382388
void comparisonOfBoolExpressionWithInt3() {

0 commit comments

Comments
 (0)