From 1d4933439834871174ac00e46232c6c079d6c49c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 20 Jun 2015 16:23:16 +0200 Subject: [PATCH] Fixed #6662 (False positive assignIfError (assignment in while condition)) --- lib/checkcondition.cpp | 3 +++ test/testcondition.cpp | 16 ++++++++++++++++ 2 files changed, 19 insertions(+) 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() {