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() != "=")
continue;
if (Token::Match(tok->tokAt(-2), "[;{}] %var% = %var% [&|] %num% ;")) {
if (Token::Match(tok->tokAt(-2), "[;{}] %var% =")) {
const unsigned int varid(tok->previous()->varId());
if (varid == 0)
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 (num < 0)
if (Token::Match(tok->next(), "%num% [&|]")) {
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;
bool islocal = false;

View File

@ -87,6 +87,19 @@ private:
"}");
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
check("void foo(int x) {\n"
" int y = x & 4;\n"