diff --git a/lib/checkassignif.cpp b/lib/checkassignif.cpp index d6168a3fb..5178a3f20 100644 --- a/lib/checkassignif.cpp +++ b/lib/checkassignif.cpp @@ -92,19 +92,27 @@ void CheckAssignIf::comparison() if (tok->str() != "&") continue; - if (Token::Match(tok, "& %num% ==|!= %num% &&|%oror%|)")) + if (Token::Match(tok, "& %num% )| ==|!= %num% &&|%oror%|)")) { const MathLib::bigint num1 = MathLib::toLongNumber(tok->strAt(1)); if (num1 < 0) continue; - const MathLib::bigint num2 = MathLib::toLongNumber(tok->strAt(3)); + const Token *compareToken = tok->tokAt(2); + if (compareToken->str() == ")") + { + if (!Token::Match(compareToken->link()->previous(), "(|%oror%|&&")) + continue; + compareToken = compareToken->next(); + } + + const MathLib::bigint num2 = MathLib::toLongNumber(compareToken->strAt(1)); if (num2 < 0) continue; if ((num1 & num2) != num2) { - const std::string op(tok->strAt(2)); + const std::string op(compareToken->str()); comparisonError(tok, op=="==" ? false : true); } } diff --git a/test/testassignif.cpp b/test/testassignif.cpp index 7a0a3bb70..7ea5d12d0 100644 --- a/test/testassignif.cpp +++ b/test/testassignif.cpp @@ -85,6 +85,12 @@ private: "}\n"); ASSERT_EQUALS("[test.cpp:3]: (style) Comparison is always false\n", errout.str()); + check("void foo(int x)\n" + "{\n" + " if ((x & 4) == 3);\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:3]: (style) Comparison is always false\n", errout.str()); + check("void foo(int x)\n" "{\n" " if (x & 4 != 3);\n"