diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 452075344..06c6dc63e 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -2745,7 +2745,7 @@ void CheckMemoryLeakNoVar::check() void CheckMemoryLeakNoVar::checkForUnusedReturnValue(const Scope *scope) { for (const Token *tok = scope->classStart; tok != scope->classEnd; tok = tok->next()) { - if (Token::Match(tok, "%name% (") && (!tok->next()->astParent() || tok->next()->astParent()->str() == "!" || tok->next()->astParent()->isComparisonOp())) { + if (Token::Match(tok, "%name% (") && (!tok->next()->astParent() || tok->next()->astParent()->str() == "!" || tok->next()->astParent()->isComparisonOp()) && tok->next()->astOperand1() == tok) { const AllocType allocType = getAllocationType(tok, 0); if (allocType != No) returnValueNotUsedError(tok, tok->str()); diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index daf7a60c1..61099fbde 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -6503,6 +6503,12 @@ private: " if(!malloc(5)) fail();\n" "}"); ASSERT_EQUALS("[test.cpp:3]: (error) Return value of allocation function malloc is not stored.\n", errout.str()); + + check("FOO* factory() {\n" + " FOO* foo = new (std::nothrow) FOO;\n" + " return foo;\n" + "}"); + ASSERT_EQUALS("", errout.str()); } void smartPointerFunctionParam() {