fix #2569 check postfix increment on boolean

This commit is contained in:
Sébastien Debrard 2011-02-11 23:38:23 +01:00
parent 2525b9db40
commit f5ed52b84b
3 changed files with 59 additions and 0 deletions

View File

@ -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()
{

View File

@ -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";

View File

@ -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)