From 782684a7cc4a071312319d628d85364f532c4adf Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Thu, 17 Sep 2020 01:33:16 -0500 Subject: [PATCH] Fix issue 9530: False positive: Reference to temporary returned when using initializer lists (#2796) --- lib/astutils.cpp | 3 +++ test/testautovariables.cpp | 9 +++++++++ 2 files changed, 12 insertions(+) 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"