diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 18ce60a60..452075344 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -2745,10 +2745,9 @@ 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->strAt(-1) != "=") { - tok = tok->next(); - const int allocationId = _settings->library.alloc(tok); - if (allocationId > 0) + if (Token::Match(tok, "%name% (") && (!tok->next()->astParent() || tok->next()->astParent()->str() == "!" || tok->next()->astParent()->isComparisonOp())) { + const AllocType allocType = getAllocationType(tok, 0); + if (allocType != No) returnValueNotUsedError(tok, tok->str()); } } @@ -2807,7 +2806,7 @@ void CheckMemoryLeakNoVar::functionCallLeak(const Token *loc, const std::string void CheckMemoryLeakNoVar::returnValueNotUsedError(const Token *tok, const std::string &alloc) { - reportError(tok, Severity::error, "leakReturnValNotUsed", "Return value of allocation function " + alloc + " is not used."); + reportError(tok, Severity::error, "leakReturnValNotUsed", "Return value of allocation function " + alloc + " is not stored."); } void CheckMemoryLeakNoVar::unsafeArgAllocError(const Token *tok, const std::string &funcName, const std::string &ptrType, const std::string& objType) diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index cca945087..daf7a60c1 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -6448,25 +6448,25 @@ private: "{\n" " malloc(10);\n" "}"); - ASSERT_EQUALS("[test.cpp:3]: (error) Return value of allocation function malloc is not used.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:3]: (error) Return value of allocation function malloc is not stored.\n", errout.str()); check("void x()\n" "{\n" " calloc(10);\n" "}"); - ASSERT_EQUALS("[test.cpp:3]: (error) Return value of allocation function calloc is not used.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:3]: (error) Return value of allocation function calloc is not stored.\n", errout.str()); check("void x()\n" "{\n" " strdup(\"Test\");\n" "}"); - ASSERT_EQUALS("[test.cpp:3]: (error) Return value of allocation function strdup is not used.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:3]: (error) Return value of allocation function strdup is not stored.\n", errout.str()); check("void x()\n" "{\n" " (char*) malloc(10);\n" "}"); - ASSERT_EQUALS("[test.cpp:3]: (error) Return value of allocation function malloc is not used.\n", errout.str()); + ASSERT_EQUALS("[test.cpp:3]: (error) Return value of allocation function malloc is not stored.\n", errout.str()); check("void x()\n" "{\n" @@ -6486,7 +6486,7 @@ private: "{\n" " 42,malloc(42);\n" "}"); - TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Return value of allocation function malloc is not used.\n", "", errout.str()); + ASSERT_EQUALS("[test.cpp:3]: (error) Return value of allocation function malloc is not stored.\n", errout.str()); check("void *f()\n" "{\n" @@ -6496,7 +6496,13 @@ private: "{\n" " f();\n" "}"); - TODO_ASSERT_EQUALS("[test.cpp:7]: (error) Return value of allocation function f is not used.\n", "", errout.str()); + ASSERT_EQUALS("[test.cpp:7]: (error) Return value of allocation function f is not stored.\n", errout.str()); + + check("void x()\n" + "{\n" + " if(!malloc(5)) fail();\n" + "}"); + ASSERT_EQUALS("[test.cpp:3]: (error) Return value of allocation function malloc is not stored.\n", errout.str()); } void smartPointerFunctionParam() {