Skip to content

Commit f43d732

Browse files
committed
Fixed cppcheck-opensource#4691 (False positive: Mismatching bitmasks in switch())
1 parent 38680e3 commit f43d732

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

‎lib/checkassignif.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ bool CheckAssignIf::assignIfParseScope(const Token * const assignTok,
7878
const char bitop,
7979
const MathLib::bigint num)
8080
{
81+
bool ret = false;
82+
8183
for (const Token *tok2 = startTok; tok2; tok2 = tok2->next()) {
8284
if (Token::Match(tok2->tokAt(2), "%varid% %cop% %num% ;", varid) && tok2->strAt(3) == std::string(1U, bitop)) {
8385
const MathLib::bigint num2 = MathLib::toLongNumber(tok2->strAt(4));
@@ -96,6 +98,10 @@ bool CheckAssignIf::assignIfParseScope(const Token * const assignTok,
9698
return true;
9799
if (tok2->str() == "}")
98100
return false;
101+
if (Token::Match(tok2, "break|continue|return"))
102+
ret = true;
103+
if (ret && tok2->str() == ";")
104+
return false;
99105
if (!islocal && Token::Match(tok2, "%var% (") && !Token::simpleMatch(tok2->next()->link(), ") {"))
100106
return true;
101107
if (Token::Match(tok2, "if|while (")) {

‎test/testassignif.cpp‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,15 @@ class TestAssignIf : public TestFixture {
177177
" int c = b & 1;\n"
178178
"}");
179179
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());
180+
181+
check("void f(int a) {\n"
182+
" int b = a;"
183+
" switch (x) {\n"
184+
" case 1: b &= 1; break;\n"
185+
" case 2: b &= 2; break;\n"
186+
" };\n"
187+
"}");
188+
ASSERT_EQUALS("", errout.str());
180189
}
181190

182191
void compare() {

0 commit comments

Comments
 (0)