Improve check: Tweaked the assignAndCompare to handle | also in addition to &

This commit is contained in:
Daniel Marjamäki 2011-08-19 16:10:09 +02:00
parent c107fdd2d4
commit 72b01d1ca0
2 changed files with 19 additions and 3 deletions

View File

@ -42,12 +42,14 @@ void CheckAssignIf::assignIf()
if (tok->str() != "=") if (tok->str() != "=")
continue; continue;
if (Token::Match(tok->tokAt(-2), "[;{}] %var% = %var% & %num% ;")) if (Token::Match(tok->tokAt(-2), "[;{}] %var% = %var% [&|] %num% ;"))
{ {
const unsigned int varid(tok->previous()->varId()); const unsigned int varid(tok->previous()->varId());
if (varid == 0) if (varid == 0)
continue; continue;
const char bitop(tok->strAt(2).at(0));
const MathLib::bigint num = MathLib::toLongNumber(tok->strAt(3)); const MathLib::bigint num = MathLib::toLongNumber(tok->strAt(3));
if (num < 0) if (num < 0)
continue; continue;
@ -60,9 +62,9 @@ void CheckAssignIf::assignIf()
{ {
const std::string op(tok2->strAt(3)); const std::string op(tok2->strAt(3));
const MathLib::bigint num2 = MathLib::toLongNumber(tok2->strAt(4)); const MathLib::bigint num2 = MathLib::toLongNumber(tok2->strAt(4));
if (op == "==" && (num & num2) != num2) if (op == "==" && (num & num2) != ((bitop=='&') ? num2 : num))
assignIfError(tok2, false); assignIfError(tok2, false);
else if (op == "!=" && (num & num2) != num2) else if (op == "!=" && (num & num2) != ((bitop=='&') ? num2 : num))
assignIfError(tok2, true); assignIfError(tok2, true);
break; break;
} }

View File

@ -64,6 +64,7 @@ private:
void assignAndCompare() void assignAndCompare()
{ {
// &
check("void foo(int x)\n" check("void foo(int x)\n"
"{\n" "{\n"
" int y = x & 4;\n" " int y = x & 4;\n"
@ -77,6 +78,19 @@ private:
" if (y != 3);\n" " if (y != 3);\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:4]: (style) Mismatching assignment and comparison, comparison is always true\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (style) Mismatching assignment and comparison, comparison is always true\n", errout.str());
// |
check("void foo(int x) {\n"
" int y = x | 0x14;\n"
" if (y == 0x710);\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (style) Mismatching assignment and comparison, comparison is always false\n", errout.str());
check("void foo(int x) {\n"
" int y = x | 0x14;\n"
" if (y == 0x71f);\n"
"}");
ASSERT_EQUALS("", errout.str());
} }
void compare() void compare()