diff --git a/lib/checkexceptionsafety.cpp b/lib/checkexceptionsafety.cpp index 2de142f20..a768b5f6b 100644 --- a/lib/checkexceptionsafety.cpp +++ b/lib/checkexceptionsafety.cpp @@ -32,6 +32,9 @@ namespace { void CheckExceptionSafety::destructors() { + if (!_settings->isEnabled("warning")) + return; + const SymbolDatabase* const symbolDatabase = _tokenizer->getSymbolDatabase(); // Perform check.. @@ -57,7 +60,7 @@ void CheckExceptionSafety::destructors() // throw found within a destructor if (tok->str() == "throw") { - destructorsError(tok); + destructorsError(tok, scope->className); break; } } diff --git a/lib/checkexceptionsafety.h b/lib/checkexceptionsafety.h index 947f18dfe..704f4db24 100644 --- a/lib/checkexceptionsafety.h +++ b/lib/checkexceptionsafety.h @@ -82,8 +82,12 @@ public: private: /** Don't throw exceptions in destructors */ - void destructorsError(const Token * const tok) { - reportError(tok, Severity::error, "exceptThrowInDestructor", "Exception thrown in destructor."); + void destructorsError(const Token * const tok, const std::string &className) { + reportError(tok, Severity::warning, "exceptThrowInDestructor", + "Class " + className + " is not safe, destructor throws exception\n" + "The class " + className + " is not safe because its destructor " + "throws an exception. If " + className + " is used and an exception " + "is thrown that is caught in an outer scope the program will terminate."); } void deallocThrowError(const Token * const tok, const std::string &varname) { @@ -140,7 +144,7 @@ private: /** Generate all possible errors (for --errorlist) */ void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const { CheckExceptionSafety c(0, settings, errorLogger); - c.destructorsError(0); + c.destructorsError(0, "Class"); c.deallocThrowError(0, "p"); c.rethrowCopyError(0, "varname"); c.catchExceptionByValueError(0); diff --git a/test/testexceptionsafety.cpp b/test/testexceptionsafety.cpp index d519ea14e..b4f09d59f 100644 --- a/test/testexceptionsafety.cpp +++ b/test/testexceptionsafety.cpp @@ -75,7 +75,7 @@ private: " throw e;\n" " }\n" "};"); - ASSERT_EQUALS("[test.cpp:3]: (error) Exception thrown in destructor.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:3]: (warning) Class x is not safe, destructor throws exception\n", errout.str()); check("class x {\n" " ~x();\n" @@ -83,12 +83,12 @@ private: "x::~x() {\n" " throw e;\n" "}"); - ASSERT_EQUALS("[test.cpp:5]: (error) Exception thrown in destructor.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:5]: (warning) Class x is not safe, destructor throws exception\n", errout.str()); check("x::~x() {\n" " throw e;\n" "}"); - TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Exception thrown in destructor.\n", "", errout.str()); + TODO_ASSERT_EQUALS("[test.cpp:3]: (warning) Class x is not safe, destructor throws exception\n", "", errout.str()); // #3858 - throwing exception in try block in destructor. check("class x {\n"