diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index b9eb70d62..d074df2cb 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -1070,9 +1070,14 @@ void CheckMemoryLeakNoVar::checkForUnusedReturnValue(const Scope *scope) if (closingBrace->str() == "}" && Token::Match(closingBrace->link()->tokAt(-1), "%name%") && (!isNew && precedes(tok, closingBrace->link()))) continue; returnValueNotUsedError(tok, tok->str()); - } else if (Token::Match(parent, "%comp%|!|,|%oror%|&&")) { - if (!(parent->astParent() && parent->str() == ",")) - returnValueNotUsedError(tok, tok->str()); + } else if (Token::Match(parent, "%comp%|!|,|%oror%|&&|:")) { + if (parent->astParent() && parent->str() == ",") + continue; + if (parent->str() == ":") { + if (!(Token::simpleMatch(parent->astParent(), "?") && !parent->astParent()->astParent())) + continue; + } + returnValueNotUsedError(tok, tok->str()); } } } diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 717212db8..659596e58 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -2607,6 +2607,14 @@ private: ASSERT_EQUALS("[test.cpp:1]: (error) Return value of allocation function 'malloc' is not stored.\n" "[test.cpp:2]: (error) Return value of allocation function 'malloc' is not stored.\n", errout.str()); + + check("void f0(const bool b) { b ? new int : nullptr; }\n" // #11155 + "void f1(const bool b) { b ? nullptr : new int; }\n" + "int* g0(const bool b) { return b ? new int : nullptr; }\n" + "void g1(const bool b) { h(b, b ? nullptr : new int); }\n"); + ASSERT_EQUALS("[test.cpp:1]: (error) Return value of allocation function 'new' is not stored.\n" + "[test.cpp:2]: (error) Return value of allocation function 'new' is not stored.\n", + errout.str()); } void smartPointerFunctionParam() {