From f43d732f3381b8aea3d3af9ccdd2fdebdf372eae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 7 Apr 2013 04:25:10 +0200 Subject: [PATCH] Fixed #4691 (False positive: Mismatching bitmasks in switch()) --- lib/checkassignif.cpp | 6 ++++++ test/testassignif.cpp | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/lib/checkassignif.cpp b/lib/checkassignif.cpp index 1b42405db..d660aacde 100644 --- a/lib/checkassignif.cpp +++ b/lib/checkassignif.cpp @@ -78,6 +78,8 @@ bool CheckAssignIf::assignIfParseScope(const Token * const assignTok, const char bitop, const MathLib::bigint num) { + bool ret = false; + for (const Token *tok2 = startTok; tok2; tok2 = tok2->next()) { if (Token::Match(tok2->tokAt(2), "%varid% %cop% %num% ;", varid) && tok2->strAt(3) == std::string(1U, bitop)) { const MathLib::bigint num2 = MathLib::toLongNumber(tok2->strAt(4)); @@ -96,6 +98,10 @@ bool CheckAssignIf::assignIfParseScope(const Token * const assignTok, return true; if (tok2->str() == "}") return false; + if (Token::Match(tok2, "break|continue|return")) + ret = true; + if (ret && tok2->str() == ";") + return false; if (!islocal && Token::Match(tok2, "%var% (") && !Token::simpleMatch(tok2->next()->link(), ") {")) return true; if (Token::Match(tok2, "if|while (")) { diff --git a/test/testassignif.cpp b/test/testassignif.cpp index 910817012..0e61f78d8 100644 --- a/test/testassignif.cpp +++ b/test/testassignif.cpp @@ -177,6 +177,15 @@ private: " int c = b & 1;\n" "}"); ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) Mismatching bitmasks. Result is always 0 (X = Y & 0xf0; Z = X & 0x1; => Z=0).\n", errout.str()); + + check("void f(int a) {\n" + " int b = a;" + " switch (x) {\n" + " case 1: b &= 1; break;\n" + " case 2: b &= 2; break;\n" + " };\n" + "}"); + ASSERT_EQUALS("", errout.str()); } void compare() {