Fixed false positive #5772: Use AST to check if modulo operation is preceded by an arithmetical operation.

This commit is contained in:
PKEuS 2014-05-09 21:35:41 +02:00
parent 6384aba214
commit c0fc47643f
2 changed files with 7 additions and 1 deletions

View File

@ -3005,7 +3005,7 @@ void CheckOther::checkModuloAlwaysTrueFalse()
const Scope * scope = symbolDatabase->functionScopes[i];
for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
if ((Token::Match(tok, "% %num% %comp% %num%")) &&
(!tok->tokAt(4) || !tok->tokAt(4)->isArithmeticalOp())) {
(!tok->tokAt(4) || !tok->tokAt(4)->isArithmeticalOp()) && (!tok->astParent() || !tok->astParent()->isArithmeticalOp())) {
if (MathLib::isLessEqual(tok->strAt(1), tok->strAt(3)))
moduloAlwaysTrueFalseError(tok, tok->strAt(1));
}

View File

@ -3721,6 +3721,12 @@ private:
ASSERT_EQUALS("[test.cpp:2]: (warning) Comparison of modulo result is predetermined, because it is always less than 5.\n"
"[test.cpp:3]: (warning) Comparison of modulo result is predetermined, because it is always less than 5.\n"
"[test.cpp:4]: (warning) Comparison of modulo result is predetermined, because it is always less than 5.\n", errout.str());
check("void f() {\n"
" if (a % 2 + b % 2 == 2)\n"
" foo();\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void incorrectLogicOperator1() {