Skip to content

Commit a37c314

Browse files
committed
Fixed cppcheck-opensource#3858 (Throw exception in destructor BUT inside a try-catch shouldn't be reported)
1 parent 62f92fe commit a37c314

2 files changed

Lines changed: 16 additions & 0 deletions

File tree

lib/checkexceptionsafety.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ void CheckExceptionSafety::destructors()
4242
if (j->type == Function::eDestructor && j->functionScope) {
4343
// Inspect this destructor..
4444
for (const Token *tok = j->functionScope->classStart->next(); tok != j->functionScope->classEnd; tok = tok->next()) {
45+
// Skip try blocks
46+
if (Token::simpleMatch(tok, "try {")) {
47+
tok = tok->next()->link();
48+
}
49+
4550
// throw found within a destructor
4651
if (tok->str() == "throw") {
4752
destructorsError(tok);

test/testexceptionsafety.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@ class TestExceptionSafety : public TestFixture {
8282
" throw e;\n"
8383
"}");
8484
TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Throwing exception in destructor\n", "", errout.str());
85+
86+
// #3858 - throwing exception in try block in destructor
87+
check("class x {\n"
88+
" ~x() {\n"
89+
" try {\n"
90+
" throw e;\n"
91+
" } catch (...) {\n"
92+
" }\n"
93+
" }\n"
94+
"}");
95+
ASSERT_EQUALS("", errout.str());
8596
}
8697

8798
void deallocThrow1() {

0 commit comments

Comments
 (0)