From 0fb9ab7b4aec74428ad7099104378b74f1b34a4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 25 Jan 2016 10:33:11 +0100 Subject: [PATCH] Refactoring CheckMemoryLeakNoVar::checkForUnusedReturnValue(). use continue --- lib/checkmemoryleak.cpp | 26 ++++++++++++++++++++------ test/testmemleak.cpp | 5 +++++ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 552470bbf..cf383280a 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -2634,12 +2634,26 @@ void CheckMemoryLeakNoVar::check() void CheckMemoryLeakNoVar::checkForUnusedReturnValue(const Scope *scope) { for (const Token *tok = scope->classStart; tok != scope->classEnd; tok = tok->next()) { - if (!tok->varId() && Token::Match(tok, "%name% (") && (!tok->function() || !Token::Match(tok->function()->retDef, "void %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()); - } + if (!Token::Match(tok, "%name% (")) + continue; + + if (tok->varId()) + continue; + + const AllocType allocType = getAllocationType(tok, 0); + if (allocType == No) + continue; + + if (tok != tok->next()->astOperand1()) + continue; + + // get ast parent, skip casts + const Token *parent = tok->next()->astParent(); + while (parent && parent->str() == "(" && !parent->astOperand2()) + parent = parent->astParent(); + + if (!parent || Token::Match(parent, "%comp%|!")) + returnValueNotUsedError(tok, tok->str()); } } diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 8e0beebb5..289d6bd3a 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -5714,6 +5714,11 @@ private: "}"); ASSERT_EQUALS("", errout.str()); + check("void foo() {\n" // #7348 - cast + " p = (::X*)malloc(42);\n" + "}"); + ASSERT_EQUALS("", errout.str()); + // #7182 "crash: CheckMemoryLeak::functionReturnType()" check("template auto unary_right_comma (Ts... ts) { return (ts , ...); }\n" "template auto binary_left_comma (T x, Ts... ts) { return (x , ... , ts); }\n"