diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 87c8badd6..bb76d6cb4 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -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) @@ -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"); } +void CheckOther::emptyCatchBlockError(const Token *tok) +{ + reportError(tok, Severity::style, "emptyCatchBlock", "Empty catch block"); +} void CheckOther::sizeofsizeof() { diff --git a/lib/checkother.h b/lib/checkother.h index 83ef03981..784307e70 100644 --- a/lib/checkother.h +++ b/lib/checkother.h @@ -61,6 +61,7 @@ public: checkOther.checkStructMemberUsage(); checkOther.strPlusChar(); checkOther.sizeofsizeof(); + checkOther.checkEmptyCatchBlock(); } /** @brief Run checks against the simplified token list */ @@ -170,6 +171,9 @@ public: /** @brief %Check for using fflush() on an input stream*/ void checkFflushOnInputStream(); + /** @brief %Check for empty catch() blocks*/ + void checkEmptyCatchBlock(); + /** @brief %Check for 'sizeof sizeof ..' */ void sizeofsizeof(); void sizeofsizeofError(const Token *tok); @@ -200,6 +204,7 @@ public: 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 fflushOnInputStreamError(const Token *tok, const std::string &varname); + void emptyCatchBlockError(const Token *tok); void getErrorMessages() { @@ -228,6 +233,7 @@ public: conditionAlwaysTrueFalse(0, "true/false"); strPlusChar(0); sizeofsizeofError(0); + emptyCatchBlockError(0); // optimisations postIncrementError(0, "varname", true); @@ -262,6 +268,7 @@ public: "* variable scope can be limited\n" "* condition that is always true/false\n" "* unusal pointer arithmetic. For example: \"abc\" + 'd'\n" + "* empty catch() block\n" // optimisations "* optimisation: detect post increment/decrement\n" diff --git a/test/testother.cpp b/test/testother.cpp index 8ebadec79..5da1aed1f 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -98,6 +98,8 @@ private: TEST_CASE(fflushOnInputStreamTest); TEST_CASE(sizeofsizeof); + + TEST_CASE(emptyCatchBlock); } void check(const char code[]) @@ -116,6 +118,7 @@ private: errout.str(""); checkOther.sizeofsizeof(); + checkOther.checkEmptyCatchBlock(); // Simplify token list.. tokenizer.simplifyTokenList(); @@ -2631,6 +2634,70 @@ private: "}\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()); } + + 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)