Skip to content

Commit af2b94e

Browse files
committed
Fixed cppcheck-opensource#5826 (Change error message for 'throw in destructor' check)
1 parent 851f89d commit af2b94e

3 files changed

Lines changed: 14 additions & 7 deletions

File tree

lib/checkexceptionsafety.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ namespace {
3232

3333
void CheckExceptionSafety::destructors()
3434
{
35+
if (!_settings->isEnabled("warning"))
36+
return;
37+
3538
const SymbolDatabase* const symbolDatabase = _tokenizer->getSymbolDatabase();
3639

3740
// Perform check..
@@ -57,7 +60,7 @@ void CheckExceptionSafety::destructors()
5760

5861
// throw found within a destructor
5962
if (tok->str() == "throw") {
60-
destructorsError(tok);
63+
destructorsError(tok, scope->className);
6164
break;
6265
}
6366
}

lib/checkexceptionsafety.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,12 @@ class CPPCHECKLIB CheckExceptionSafety : public Check {
8282

8383
private:
8484
/** Don't throw exceptions in destructors */
85-
void destructorsError(const Token * const tok) {
86-
reportError(tok, Severity::error, "exceptThrowInDestructor", "Exception thrown in destructor.");
85+
void destructorsError(const Token * const tok, const std::string &className) {
86+
reportError(tok, Severity::warning, "exceptThrowInDestructor",
87+
"Class " + className + " is not safe, destructor throws exception\n"
88+
"The class " + className + " is not safe because its destructor "
89+
"throws an exception. If " + className + " is used and an exception "
90+
"is thrown that is caught in an outer scope the program will terminate.");
8791
}
8892

8993
void deallocThrowError(const Token * const tok, const std::string &varname) {
@@ -140,7 +144,7 @@ class CPPCHECKLIB CheckExceptionSafety : public Check {
140144
/** Generate all possible errors (for --errorlist) */
141145
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const {
142146
CheckExceptionSafety c(0, settings, errorLogger);
143-
c.destructorsError(0);
147+
c.destructorsError(0, "Class");
144148
c.deallocThrowError(0, "p");
145149
c.rethrowCopyError(0, "varname");
146150
c.catchExceptionByValueError(0);

test/testexceptionsafety.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,20 @@ class TestExceptionSafety : public TestFixture {
7575
" throw e;\n"
7676
" }\n"
7777
"};");
78-
ASSERT_EQUALS("[test.cpp:3]: (error) Exception thrown in destructor.\n", errout.str());
78+
ASSERT_EQUALS("[test.cpp:3]: (warning) Class x is not safe, destructor throws exception\n", errout.str());
7979

8080
check("class x {\n"
8181
" ~x();\n"
8282
"};\n"
8383
"x::~x() {\n"
8484
" throw e;\n"
8585
"}");
86-
ASSERT_EQUALS("[test.cpp:5]: (error) Exception thrown in destructor.\n", errout.str());
86+
ASSERT_EQUALS("[test.cpp:5]: (warning) Class x is not safe, destructor throws exception\n", errout.str());
8787

8888
check("x::~x() {\n"
8989
" throw e;\n"
9090
"}");
91-
TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Exception thrown in destructor.\n", "", errout.str());
91+
TODO_ASSERT_EQUALS("[test.cpp:3]: (warning) Class x is not safe, destructor throws exception\n", "", errout.str());
9292

9393
// #3858 - throwing exception in try block in destructor.
9494
check("class x {\n"

0 commit comments

Comments
 (0)