diff --git a/lib/checkcondition.cpp b/lib/checkcondition.cpp index 64c025a9f..67cbbbc1b 100644 --- a/lib/checkcondition.cpp +++ b/lib/checkcondition.cpp @@ -143,6 +143,9 @@ bool CheckCondition::assignIfParseScope(const Token * const assignTok, else if (op == "!=" && (num & num2) != ((bitop=='&') ? num2 : num)) assignIfError(assignTok, tok2, condition, true); } + if (Token::Match(tok2, "%varid% %op%", varid) && tok2->next()->isAssignmentOp()) { + return true; + } } bool ret1 = assignIfParseScope(assignTok, end->tokAt(2), varid, islocal, bitop, num); diff --git a/test/testcondition.cpp b/test/testcondition.cpp index 3e5b261bb..3a6385963 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -255,6 +255,22 @@ private: " }\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + check("void f(int a) {\n" // #6662 + " int x = a & 1;\n" + " while (x <= 4) {\n" + " if (x != 5) {}\n" + " }\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:4]: (style) Mismatching assignment and comparison, comparison 'x!=5' is always true.\n", errout.str()); + + check("void f(int a) {\n" // #6662 + " int x = a & 1;\n" + " while ((x += 4) < 10) {\n" + " if (x != 5) {}\n" + " }\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void mismatchingBitAnd() {