Fix issue 9530: False positive: Reference to temporary returned when using initializer lists (#2796)

This commit is contained in:
Paul Fultz II 2020-09-17 01:33:16 -05:00 committed by GitHub
parent 11c99d7387
commit 782684a7cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 0 deletions

View File

@ -273,6 +273,9 @@ bool isTemporary(bool cpp, const Token* tok, const Library* library, bool unknow
// Currying a function is unknown in cppcheck
if (Token::simpleMatch(tok, "(") && Token::simpleMatch(tok->astOperand1(), "("))
return unknown;
if (Token::simpleMatch(tok, "{") && Token::simpleMatch(tok->astParent(), "return") && tok->astOperand1() &&
!tok->astOperand2())
return isTemporary(cpp, tok->astOperand1(), library);
return true;
}

View File

@ -115,6 +115,7 @@ private:
TEST_CASE(returnReference18); // #9482
TEST_CASE(returnReference19); // #9597
TEST_CASE(returnReference20); // #9536
TEST_CASE(returnReference21); // #9530
TEST_CASE(returnReferenceFunction);
TEST_CASE(returnReferenceContainer);
TEST_CASE(returnReferenceLiteral);
@ -1352,6 +1353,14 @@ private:
ASSERT_EQUALS("", errout.str());
}
// #9530
void returnReference21() {
check("int& f(int& x) {\n"
" return {x};\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void returnReferenceFunction() {
check("int& f(int& a) {\n"
" return a;\n"