Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions lib/checkmemoryleak.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions test/testmemleak.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2607,6 +2607,14 @@ class TestMemleakNoVar : public TestFixture {
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() {
Expand Down