Fixed #4846 (false positive: (warning) Comparison of a boolean with an integer.)

This commit is contained in:
Daniel Marjamäki 2013-08-21 16:17:19 +02:00
parent 4c0ba2b36a
commit f257c62858
2 changed files with 9 additions and 1 deletions

View File

@ -127,7 +127,7 @@ void CheckBool::checkComparisonOfBoolWithInt()
for (std::size_t i = 0; i < functions; ++i) {
const Scope * scope = symbolDatabase->functionScopes[i];
for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
if (tok->next() && tok->next()->type() == Token::eComparisonOp && (!tok->previous() || !tok->previous()->isArithmeticalOp()) && (!tok->tokAt(3) || !tok->tokAt(3)->isArithmeticalOp())) {
if ((!Token::Match(tok->previous(), "%cop%")) && Token::Match(tok->next(), "%comp%") && (!Token::Match(tok->tokAt(3), "%cop%"))) {
const Token* const right = tok->tokAt(2);
if ((tok->varId() && right->isNumber()) || (tok->isNumber() && right->varId())) { // Comparing variable with number
const Token* varTok = tok;

View File

@ -47,6 +47,7 @@ private:
TEST_CASE(comparisonOfBoolWithInt4);
TEST_CASE(comparisonOfBoolWithInt5);
TEST_CASE(comparisonOfBoolWithInt6); // #4224 - integer is casted to bool
TEST_CASE(comparisonOfBoolWithInt7); // #4846 - (!x == true)
TEST_CASE(checkComparisonOfFuncReturningBool1);
TEST_CASE(checkComparisonOfFuncReturningBool2);
@ -757,6 +758,13 @@ private:
"}");
ASSERT_EQUALS("", errout.str());
}
void comparisonOfBoolWithInt7() { // #4846 - (!x==true)
check("void f(int x) {\n"
" if (!x == true) { }\n"
"}");
ASSERT_EQUALS("", errout.str());
}
};
REGISTER_TEST(TestBool)