Fixed #920 (new style check: find empty catch blocks)

This commit is contained in:
Zachary Blair 2010-06-14 23:45:46 -07:00
parent c27e631aa2
commit efefceabae
3 changed files with 95 additions and 0 deletions

View File

@ -3565,7 +3565,24 @@ void CheckOther::postIncrement()
} }
} }
void CheckOther::checkEmptyCatchBlock()
{
if (!_settings->_checkCodingStyle)
return;
const char pattern[] = "} catch (";
for (const Token *tok = Token::findmatch(_tokenizer->tokens(), pattern); tok;
tok = Token::findmatch(tok, pattern))
{
tok = tok->tokAt(2);
if (Token::Match(tok, "( const| %type% *|&| %var% ) { }") ||
Token::simpleMatch(tok, "( . . . ) { }"))
{
emptyCatchBlockError(tok);
}
}
}
void CheckOther::cstyleCastError(const Token *tok) void CheckOther::cstyleCastError(const Token *tok)
@ -3712,6 +3729,10 @@ void CheckOther::fflushOnInputStreamError(const Token *tok, const std::string &v
"fflushOnInputStream", "fflush() called on input stream \"" + varname + "\" may result in undefined behaviour"); "fflushOnInputStream", "fflush() called on input stream \"" + varname + "\" may result in undefined behaviour");
} }
void CheckOther::emptyCatchBlockError(const Token *tok)
{
reportError(tok, Severity::style, "emptyCatchBlock", "Empty catch block");
}
void CheckOther::sizeofsizeof() void CheckOther::sizeofsizeof()
{ {

View File

@ -61,6 +61,7 @@ public:
checkOther.checkStructMemberUsage(); checkOther.checkStructMemberUsage();
checkOther.strPlusChar(); checkOther.strPlusChar();
checkOther.sizeofsizeof(); checkOther.sizeofsizeof();
checkOther.checkEmptyCatchBlock();
} }
/** @brief Run checks against the simplified token list */ /** @brief Run checks against the simplified token list */
@ -170,6 +171,9 @@ public:
/** @brief %Check for using fflush() on an input stream*/ /** @brief %Check for using fflush() on an input stream*/
void checkFflushOnInputStream(); void checkFflushOnInputStream();
/** @brief %Check for empty catch() blocks*/
void checkEmptyCatchBlock();
/** @brief %Check for 'sizeof sizeof ..' */ /** @brief %Check for 'sizeof sizeof ..' */
void sizeofsizeof(); void sizeofsizeof();
void sizeofsizeofError(const Token *tok); void sizeofsizeofError(const Token *tok);
@ -200,6 +204,7 @@ public:
void postIncrementError(const Token *tok, const std::string &var_name, const bool isIncrement); void postIncrementError(const Token *tok, const std::string &var_name, const bool isIncrement);
void emptyStringTestError(const Token *tok, const std::string &var_name, const bool isTestForEmpty); void emptyStringTestError(const Token *tok, const std::string &var_name, const bool isTestForEmpty);
void fflushOnInputStreamError(const Token *tok, const std::string &varname); void fflushOnInputStreamError(const Token *tok, const std::string &varname);
void emptyCatchBlockError(const Token *tok);
void getErrorMessages() void getErrorMessages()
{ {
@ -228,6 +233,7 @@ public:
conditionAlwaysTrueFalse(0, "true/false"); conditionAlwaysTrueFalse(0, "true/false");
strPlusChar(0); strPlusChar(0);
sizeofsizeofError(0); sizeofsizeofError(0);
emptyCatchBlockError(0);
// optimisations // optimisations
postIncrementError(0, "varname", true); postIncrementError(0, "varname", true);
@ -262,6 +268,7 @@ public:
"* variable scope can be limited\n" "* variable scope can be limited\n"
"* condition that is always true/false\n" "* condition that is always true/false\n"
"* unusal pointer arithmetic. For example: \"abc\" + 'd'\n" "* unusal pointer arithmetic. For example: \"abc\" + 'd'\n"
"* empty catch() block\n"
// optimisations // optimisations
"* optimisation: detect post increment/decrement\n" "* optimisation: detect post increment/decrement\n"

View File

@ -98,6 +98,8 @@ private:
TEST_CASE(fflushOnInputStreamTest); TEST_CASE(fflushOnInputStreamTest);
TEST_CASE(sizeofsizeof); TEST_CASE(sizeofsizeof);
TEST_CASE(emptyCatchBlock);
} }
void check(const char code[]) void check(const char code[])
@ -116,6 +118,7 @@ private:
errout.str(""); errout.str("");
checkOther.sizeofsizeof(); checkOther.sizeofsizeof();
checkOther.checkEmptyCatchBlock();
// Simplify token list.. // Simplify token list..
tokenizer.simplifyTokenList(); tokenizer.simplifyTokenList();
@ -2631,6 +2634,70 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Suspicios code 'sizeof sizeof ..', most likely there should only be one sizeof. The current code is equivalent with 'sizeof(size_t)'.\n", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (style) Suspicios code 'sizeof sizeof ..', most likely there should only be one sizeof. The current code is equivalent with 'sizeof(size_t)'.\n", errout.str());
} }
void emptyCatchBlock()
{
check("void Open(String name)\n"
"{\n"
" try\n"
" {\n"
" foo();\n"
" }\n"
" catch (FileNotFoundException e)\n"
" {\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:7]: (style) Empty catch block\n", errout.str());
check("void Open(String name)\n"
"{\n"
" try\n"
" {\n"
" foo();\n"
" }\n"
" catch (char *errorMsg)\n"
" {\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:7]: (style) Empty catch block\n", errout.str());
check("void Open(String name)\n"
"{\n"
" try\n"
" {\n"
" foo();\n"
" }\n"
" catch (const FileNotFoundException& e)\n"
" {\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:7]: (style) Empty catch block\n", errout.str());
check("void Open(String name)\n"
"{\n"
" try\n"
" {\n"
" foo();\n"
" }\n"
" catch (...)\n"
" {\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:7]: (style) Empty catch block\n", errout.str());
check("void Open(String name)\n"
"{\n"
" try\n"
" {\n"
" foo();\n"
" }\n"
" catch (const FileNotFoundException& e)\n"
" {\n"
" prinf(\"File not found\");\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
}; };
REGISTER_TEST(TestOther) REGISTER_TEST(TestOther)