diff --git a/lib/checkleakautovar.cpp b/lib/checkleakautovar.cpp index f35ab543d..d9209897f 100644 --- a/lib/checkleakautovar.cpp +++ b/lib/checkleakautovar.cpp @@ -388,8 +388,17 @@ void CheckLeakAutoVar::checkScope(const Token * const startToken, } // throw - // TODO: if the execution leave the function then treat it as return else if (tok->str() == "throw") { + bool tryFound = false; + const Scope* scope = tok->scope(); + while (scope && scope->isExecutable()) { + if (scope->type == Scope::eTry) + tryFound = true; + scope = scope->nestedIn; + } + // If the execution leaves the function then treat it as return + if (!tryFound) + ret(tok, *varInfo); varInfo->clear(); } } diff --git a/test/testleakautovar.cpp b/test/testleakautovar.cpp index b05f7fa5c..4060b4ebb 100644 --- a/test/testleakautovar.cpp +++ b/test/testleakautovar.cpp @@ -661,7 +661,7 @@ private: " char *p = malloc(10);\n" " throw 123;\n" "}"); - TODO_ASSERT_EQUALS("error", "", errout.str()); + ASSERT_EQUALS("[test.c:3]: (error) Memory leak: p\n", errout.str()); check("void f() {\n" " char *p;\n"