Fixed #4691 (False positive: Mismatching bitmasks in switch())
This commit is contained in:
parent
38680e3440
commit
f43d732f33
|
@ -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 (")) {
|
||||
|
|
|
@ -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() {
|
||||
|
|
Loading…
Reference in New Issue