From ca5f2562fca393eb850538a20981a9e4b9e74116 Mon Sep 17 00:00:00 2001 From: Tyson Nottingham Date: Wed, 25 Sep 2019 04:07:39 -0700 Subject: [PATCH] Fix false negatives in checkIncrementBoolean (#2210) Detect incrementing boolean expressions involving pointer dereferences, array element accesses, etc. --- lib/checkbool.cpp | 6 ++---- test/testbool.cpp | 10 ++++++++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/checkbool.cpp b/lib/checkbool.cpp index 6f847cdeb..a385d8336 100644 --- a/lib/checkbool.cpp +++ b/lib/checkbool.cpp @@ -57,10 +57,8 @@ void CheckBool::checkIncrementBoolean() const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase(); for (const Scope * scope : symbolDatabase->functionScopes) { for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) { - if (Token::Match(tok, "%var% ++")) { - const Variable *var = tok->variable(); - if (isBool(var)) - incrementBooleanError(tok); + if (astIsBool(tok) && tok->astParent() && tok->astParent()->str() == "++") { + incrementBooleanError(tok); } } } diff --git a/test/testbool.cpp b/test/testbool.cpp index c50066d77..385a984c5 100644 --- a/test/testbool.cpp +++ b/test/testbool.cpp @@ -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()); + 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" " test++;\n" "}");