diff --git a/lib/checkassignif.cpp b/lib/checkassignif.cpp index f84568041..e993d24ab 100644 --- a/lib/checkassignif.cpp +++ b/lib/checkassignif.cpp @@ -56,12 +56,14 @@ void CheckAssignIf::check() { if (tok2->str() == "(" || tok2->str() == "}" || tok2->str() == "=") break; - if (Token::Match(tok2,"if ( %varid% %any% %num% ) ", varid)) + if (Token::Match(tok2,"if ( %varid% %any% %num% &&|%oror%|)", varid)) { const std::string op(tok2->strAt(3)); const MathLib::bigint num2 = MathLib::toLongNumber(tok2->strAt(4)); if (op == "==" && (num & num2) != num2) - mismatchError(tok2); + mismatchError(tok2, false); + else if (op == "!=" && (num & num2) != num2) + mismatchError(tok2, true); break; } } @@ -69,9 +71,9 @@ void CheckAssignIf::check() } } -void CheckAssignIf::mismatchError(const Token *tok) +void CheckAssignIf::mismatchError(const Token *tok, bool result) { reportError(tok, Severity::style, "assignIfMismatchError", - "Mismatching assignment and condition, condition is always false"); + "Mismatching assignment and comparison, comparison is always " + std::string(result ? "true" : "false")); } diff --git a/lib/checkassignif.h b/lib/checkassignif.h index 66c87d260..13f708629 100644 --- a/lib/checkassignif.h +++ b/lib/checkassignif.h @@ -58,12 +58,12 @@ public: private: - void mismatchError(const Token *tok); + void mismatchError(const Token *tok, bool result); void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) { CheckAssignIf c(0, settings, errorLogger); - c.mismatchError(0); + c.mismatchError(0, false); } std::string myName() const @@ -74,7 +74,7 @@ private: std::string classInfo() const { return "Match assignments and conditions:\n" - " mismatching assignment and condition"; + " Mismatching assignment and comparison => comparison is always true/false"; } }; /// @} diff --git a/test/testassignif.cpp b/test/testassignif.cpp index e3bd421bc..56cc40b15 100644 --- a/test/testassignif.cpp +++ b/test/testassignif.cpp @@ -65,7 +65,14 @@ private: " int y = x & 4;\n" " if (y == 3);\n" "}\n"); - ASSERT_EQUALS("[test.cpp:4]: (style) Mismatching assignment and condition, condition is always false\n", errout.str()); + ASSERT_EQUALS("[test.cpp:4]: (style) Mismatching assignment and comparison, comparison is always false\n", errout.str()); + + check("void foo(int x)\n" + "{\n" + " int y = x & 4;\n" + " if (y != 3);\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4]: (style) Mismatching assignment and comparison, comparison is always true\n", errout.str()); } };