diff --git a/lib/astutils.cpp b/lib/astutils.cpp index fe19a54a4..b7d841018 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -237,8 +237,14 @@ bool isTemporary(bool cpp, const Token* tok) return false; if (Token::Match(tok, "&|<<|>>") && isLikelyStream(cpp, tok->astOperand1())) return false; - if (Token::Match(tok->previous(), "%name% (")) - return tok->previous()->function() && !Function::returnsReference(tok->previous()->function(), true); + if (Token::Match(tok->previous(), ">|%name% (")) { + const Function * f = nullptr; + if (tok->previous()->function()) + f = tok->previous()->function(); + else if (tok->previous()->link()) + f = tok->previous()->link()->previous()->function(); + return f && !Function::returnsReference(tok->previous()->function(), true); + } return true; } diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index 8beb9b6ca..e6e09243f 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -107,6 +107,8 @@ private: TEST_CASE(returnReference12); TEST_CASE(returnReference13); TEST_CASE(returnReference14); + TEST_CASE(returnReference15); // #9432 + TEST_CASE(returnReference16); // #9433 TEST_CASE(returnReferenceFunction); TEST_CASE(returnReferenceContainer); TEST_CASE(returnReferenceLiteral); @@ -1244,6 +1246,39 @@ private: ASSERT_EQUALS("", errout.str()); } + void returnReference15() { + check("template \n" + "const int& f() {\n" + " static int s;\n" + " return s;\n" + "}\n" + "template \n" + "const int& f(const T&) {\n" + " return f();\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + + check("template \n" + "int g();\n" + "template \n" + "const int& f(const T&) {\n" + " return g();\n" + "}\n"); + TODO_ASSERT_EQUALS("error", "", errout.str()); + } + + void returnReference16() { + check("int& f(std::tuple& x) {\n" + " return std::get<0>(x);\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + + check("int& f(int x) {\n" + " return std::get<0>(std::make_tuple(x));\n" + "}\n"); + TODO_ASSERT_EQUALS("error", "", errout.str()); + } + void returnReferenceFunction() { check("int& f(int& a) {\n" " return a;\n"