CheckBool: Fixed false positives for non-bool expressions that contain ! operator

This commit is contained in:
Daniel Marjamäki 2013-10-07 16:37:51 +02:00
parent 023d0e7cb8
commit 83f9503839
2 changed files with 13 additions and 5 deletions

View File

@ -378,9 +378,11 @@ static bool isNonBoolLHSExpr(const Token *tok)
nonBoolExpr = true;
else if (tok->varId() && isNonBoolStdType(tok->variable()))
nonBoolExpr = true;
else if (tok->isArithmeticalOp())
else if (tok->isArithmeticalOp()) {
if (indentlevel == 0)
return true;
nonBoolExpr = true;
else if (tok->isComparisonOp() || (tok->str() == "!" && tok->previous()->str()=="("))
} else if (tok->isComparisonOp() || (tok->str() == "!" && tok->previous()->str()=="("))
return false;
else if (indentlevel == 0 && Token::Match(tok,"[;{}=?:&|^,]"))
break;
@ -425,7 +427,7 @@ void CheckBool::checkComparisonOfBoolExpressionWithInt()
op = opTok->str()[0]=='>'?'<':'>';
}
else if (Token::Match(tok, "! %var% %comp% %any%")) {
else if (Token::Match(tok, "! %var% %comp% %any%") && !isNonBoolLHSExpr(tok)) {
numTok = tok->tokAt(3);
opTok = tok->tokAt(2);
if (Token::Match(opTok, "<|>"))

View File

@ -307,7 +307,10 @@ private:
"}");
ASSERT_EQUALS("", errout.str());
check("int f() { return !a+b<c; }");
check("int f() { return !a+b<c; }"); // #5072
ASSERT_EQUALS("",errout.str());
check("int f() { return (!a+b<c); }");
ASSERT_EQUALS("",errout.str());
}
@ -377,6 +380,9 @@ private:
" return (x()+1 == !a);\n"
"}");
TODO_ASSERT_EQUALS("error", "", errout.str());
check("void f() { if (!!a+!!b+!!c>1){} }");
ASSERT_EQUALS("",errout.str());
}
void comparisonOfBoolExpressionWithInt3() {