Browse files

Fixed #4691 (False positive: Mismatching bitmasks in switch())

  • Loading branch information...
1 parent 38680e3 commit f43d732f3381b8aea3d3af9ccdd2fdebdf372eae @danmar committed Apr 7, 2013
Showing with 15 additions and 0 deletions.
  1. +6 −0 lib/checkassignif.cpp
  2. +9 −0 test/testassignif.cpp
View
6 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 (")) {
View
9 test/testassignif.cpp
@@ -177,6 +177,15 @@ class TestAssignIf : public TestFixture {
" 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() {

0 comments on commit f43d732

Please sign in to comment.