Fix false negatives in checkIncrementBoolean (#2210)

Detect incrementing boolean expressions involving pointer dereferences,
array element accesses, etc.
This commit is contained in:
Tyson Nottingham 2019-09-25 04:07:39 -07:00 committed by amai2012
parent 0fadae78d3
commit ca5f2562fc
2 changed files with 12 additions and 4 deletions

View File

@ -57,10 +57,8 @@ void CheckBool::checkIncrementBoolean()
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase(); const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
for (const Scope * scope : symbolDatabase->functionScopes) { for (const Scope * scope : symbolDatabase->functionScopes) {
for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) { for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) {
if (Token::Match(tok, "%var% ++")) { if (astIsBool(tok) && tok->astParent() && tok->astParent()->str() == "++") {
const Variable *var = tok->variable(); incrementBooleanError(tok);
if (isBool(var))
incrementBooleanError(tok);
} }
} }
} }

View File

@ -857,6 +857,16 @@ private:
"}"); "}");
ASSERT_EQUALS("[test.cpp:2]: (style) Incrementing a variable of type 'bool' with postfix operator++ is deprecated by the C++ Standard. You should assign it the value 'true' instead.\n", errout.str()); ASSERT_EQUALS("[test.cpp:2]: (style) Incrementing a variable of type 'bool' with postfix operator++ is deprecated by the C++ Standard. You should assign it the value 'true' instead.\n", errout.str());
check("void f(bool* test){\n"
" (*test)++;\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (style) Incrementing a variable of type 'bool' with postfix operator++ is deprecated by the C++ Standard. You should assign it the value 'true' instead.\n", errout.str());
check("void f(bool* test){\n"
" test[0]++;\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (style) Incrementing a variable of type 'bool' with postfix operator++ is deprecated by the C++ Standard. You should assign it the value 'true' instead.\n", errout.str());
check("void f(int test){\n" check("void f(int test){\n"
" test++;\n" " test++;\n"
"}"); "}");