Refactoring CheckMemoryLeakNoVar::checkForUnusedReturnValue(). use continue

This commit is contained in:
Daniel Marjamäki 2016-01-25 10:33:11 +01:00
parent 7663b6ee75
commit 0fb9ab7b4a
2 changed files with 25 additions and 6 deletions

View File

@ -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());
}
}

View File

@ -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<typename... Ts> auto unary_right_comma (Ts... ts) { return (ts , ...); }\n"
"template<typename T, typename... Ts> auto binary_left_comma (T x, Ts... ts) { return (x , ... , ts); }\n"