diff --git a/lib/checkassignif.cpp b/lib/checkassignif.cpp index abe6a2bfa..31fe1635a 100644 --- a/lib/checkassignif.cpp +++ b/lib/checkassignif.cpp @@ -42,12 +42,14 @@ void CheckAssignIf::assignIf() if (tok->str() != "=") continue; - if (Token::Match(tok->tokAt(-2), "[;{}] %var% = %var% & %num% ;")) + if (Token::Match(tok->tokAt(-2), "[;{}] %var% = %var% [&|] %num% ;")) { const unsigned int varid(tok->previous()->varId()); if (varid == 0) continue; + const char bitop(tok->strAt(2).at(0)); + const MathLib::bigint num = MathLib::toLongNumber(tok->strAt(3)); if (num < 0) continue; @@ -60,9 +62,9 @@ void CheckAssignIf::assignIf() { const std::string op(tok2->strAt(3)); const MathLib::bigint num2 = MathLib::toLongNumber(tok2->strAt(4)); - if (op == "==" && (num & num2) != num2) + if (op == "==" && (num & num2) != ((bitop=='&') ? num2 : num)) assignIfError(tok2, false); - else if (op == "!=" && (num & num2) != num2) + else if (op == "!=" && (num & num2) != ((bitop=='&') ? num2 : num)) assignIfError(tok2, true); break; } diff --git a/test/testassignif.cpp b/test/testassignif.cpp index 4e072519a..f7f8f730f 100644 --- a/test/testassignif.cpp +++ b/test/testassignif.cpp @@ -64,6 +64,7 @@ private: void assignAndCompare() { + // & check("void foo(int x)\n" "{\n" " int y = x & 4;\n" @@ -77,6 +78,19 @@ private: " if (y != 3);\n" "}\n"); ASSERT_EQUALS("[test.cpp:4]: (style) Mismatching assignment and comparison, comparison is always true\n", errout.str()); + + // | + check("void foo(int x) {\n" + " int y = x | 0x14;\n" + " if (y == 0x710);\n" + "}"); + ASSERT_EQUALS("[test.cpp:3]: (style) Mismatching assignment and comparison, comparison is always false\n", errout.str()); + + check("void foo(int x) {\n" + " int y = x | 0x14;\n" + " if (y == 0x71f);\n" + "}"); + ASSERT_EQUALS("", errout.str()); } void compare()