CheckAssignIf: Better handling of various expressions in assignments

This commit is contained in:
Daniel Marjamäki 2012-11-29 10:19:52 +01:00
parent ddad2d45cf
commit 7760a92930
2 changed files with 31 additions and 4 deletions

View File

@ -42,15 +42,29 @@ 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% =")) {
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)); char bitop = '\0';
MathLib::bigint num = 0;
const MathLib::bigint num = MathLib::toLongNumber(tok->strAt(3)); if (Token::Match(tok->next(), "%num% [&|]")) {
if (num < 0) bitop = tok->strAt(2).at(0);
num = MathLib::toLongNumber(tok->next()->str());
} else {
const Token * const endToken = Token::findmatch(tok, ";");
if (endToken && Token::Match(endToken->tokAt(-2), "[&|] %num% ;")) {
bitop = endToken->strAt(-2).at(0);
num = MathLib::toLongNumber(endToken->previous()->str());
}
}
if (bitop == '\0')
continue;
if (num < 0 && bitop == '|')
continue; continue;
bool islocal = false; bool islocal = false;

View File

@ -87,6 +87,19 @@ private:
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
// various simple assignments
check("void foo(int x) {\n"
" int y = (x+1) | 1;\n"
" if (y == 2);\n"
"}");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) Mismatching assignment and comparison, comparison 'y==2' is always false.\n", errout.str());
check("void foo() {\n"
" int y = 1 | x();\n"
" if (y == 2);\n"
"}");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (style) Mismatching assignment and comparison, comparison 'y==2' is always false.\n", errout.str());
// multiple conditions // multiple conditions
check("void foo(int x) {\n" check("void foo(int x) {\n"
" int y = x & 4;\n" " int y = x & 4;\n"