From f5ed52b84b437c1494e331ba05c2d5f92d6960c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Debrard?= Date: Fri, 11 Feb 2011 23:38:23 +0100 Subject: [PATCH] fix #2569 check postfix increment on boolean --- lib/checkother.cpp | 32 ++++++++++++++++++++++++++++++++ lib/checkother.h | 7 +++++++ test/testother.cpp | 20 ++++++++++++++++++++ 3 files changed, 59 insertions(+) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 1e94d1b33..799fadbcf 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -34,6 +34,38 @@ CheckOther instance; //--------------------------------------------------------------------------- +void CheckOther::checkIncrementBoolean() +{ + if (!_settings->_checkCodingStyle) + return; + + for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) + { + if (Token::Match(tok, "%var% ++")) + { + if (tok->varId()) + { + const Token *declTok = Token::findmatch(_tokenizer->tokens(), "bool %varid%", tok->varId()); + if (declTok) + incrementBooleanError(tok); + } + } + } +} + +void CheckOther::incrementBooleanError(const Token *tok) +{ + reportError( + tok, + Severity::style, + "incrementboolean", + "The use of a variable of type bool with the ++ postfix operator is always true and deprecated by the C++ Standard.\n" + "The operand of a postfix increment operator may be of type bool but it is deprecated by C++ Standard (Annex D-1) and the operand is always set to true\n" + ); +} + +//--------------------------------------------------------------------------- + void CheckOther::clarifyCalculation() { diff --git a/lib/checkother.h b/lib/checkother.h index 07df64867..cb0266cc1 100644 --- a/lib/checkother.h +++ b/lib/checkother.h @@ -88,6 +88,7 @@ public: checkOther.checkCatchExceptionByValue(); checkOther.checkMemsetZeroBytes(); checkOther.checkIncorrectStringCompare(); + checkOther.checkIncrementBoolean(); } /** @brief Clarify calculation for ".. a * b ? .." */ @@ -184,6 +185,9 @@ public: /** @brief %Check for using bad usage of strncmp and substr */ void checkIncorrectStringCompare(); + /** @brief %Check for using postfix increment on bool */ + void checkIncrementBoolean(); + // Error messages.. void cstyleCastError(const Token *tok); void dangerousUsageStrtolError(const Token *tok); @@ -209,6 +213,7 @@ public: void memsetZeroBytesError(const Token *tok, const std::string &varname); void sizeofForArrayParameterError(const Token *tok); void incorrectStringCompareError(const Token *tok, const std::string& func, const std::string &string, const std::string &len); + void incrementBooleanError(const Token *tok); void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) { @@ -249,6 +254,7 @@ public: c.memsetZeroBytesError(0, "varname"); c.clarifyCalculationError(0); c.incorrectStringCompareError(0, "substr", "\"Hello World\"", "12"); + c.incrementBooleanError(0); } std::string myName() const @@ -289,6 +295,7 @@ public: "* mutual exclusion over || always evaluating to true\n" "* exception caught by value instead of by reference\n" "* Clarify calculation with parantheses\n" + "* using increment on boolean\n" // optimisations "* optimisation: detect post increment/decrement\n"; diff --git a/test/testother.cpp b/test/testother.cpp index f5adf548d..1e7eca1d3 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -104,6 +104,8 @@ private: TEST_CASE(clarifyCalculation); TEST_CASE(incorrectStringCompare); + + TEST_CASE(incrementBoolean); } void check(const char code[], const char *filename = NULL) @@ -141,6 +143,7 @@ private: checkOther.checkMemsetZeroBytes(); checkOther.clarifyCalculation(); checkOther.checkIncorrectStringCompare(); + checkOther.checkIncrementBoolean(); } @@ -1892,6 +1895,23 @@ private: ASSERT_EQUALS("", errout.str()); } + + void incrementBoolean() + { + check("bool bValue = true;\n" + "bValue++;\n"); + ASSERT_EQUALS("[test.cpp:2]: (style) The use of a variable of type bool with the ++ postfix operator is always true and deprecated by the C++ Standard.\n", errout.str()); + + check("void f(bool test){\n" + " test++;\n" + "}"); + ASSERT_EQUALS("[test.cpp:2]: (style) The use of a variable of type bool with the ++ postfix operator is always true and deprecated by the C++ Standard.\n", errout.str()); + + check("void f(int test){\n" + " test++;\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } }; REGISTER_TEST(TestOther)