diff --git a/lib/astutils.cpp b/lib/astutils.cpp index f14ef52c0..7ad774aef 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -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; } diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index 0e4848f2b..9d69c032e 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -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"