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() != "=")
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());
if (varid == 0)
continue;
const char bitop(tok->strAt(2).at(0));
const MathLib::bigint num = MathLib::toLongNumber(tok->strAt(3));
if (num < 0)
continue;
@ -60,9 +62,9 @@ void CheckAssignIf::assignIf()
{
const std::string op(tok2->strAt(3));
const MathLib::bigint num2 = MathLib::toLongNumber(tok2->strAt(4));
if (op == "==" && (num & num2) != num2)
if (op == "==" && (num & num2) != ((bitop=='&') ? num2 : num))
assignIfError(tok2, false);
else if (op == "!=" && (num & num2) != num2)
else if (op == "!=" && (num & num2) != ((bitop=='&') ? num2 : num))
assignIfError(tok2, true);
break;
}

View File

@ -64,6 +64,7 @@ private:
void assignAndCompare()
{
// &
check("void foo(int x)\n"
"{\n"
" int y = x & 4;\n"
@ -77,6 +78,19 @@ private:
" if (y != 3);\n"
"}\n");
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()