Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ bool isTemporary(const Token* tok, const Library* library, bool unknown)
return false;
return !branchTok->astOperand1()->valueType()->isTypeEqual(branchTok->astOperand2()->valueType());
}
if (Token::simpleMatch(tok, "(") && tok->astOperand1() &&
if (Token::Match(tok, "(|{") && tok->astOperand1() &&
(tok->astOperand2() || Token::simpleMatch(tok->next(), ")"))) {
if (Token::simpleMatch(tok->astOperand1(), "typeid"))
return false;
Expand Down Expand Up @@ -498,9 +498,6 @@ bool isTemporary(const Token* tok, const Library* library, bool unknown)
// 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(tok->astOperand1(), library);
return true;
}

Expand Down
14 changes: 14 additions & 0 deletions test/testautovariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ class TestAutoVariables : public TestFixture {
TEST_CASE(returnReference26);
TEST_CASE(returnReference27);
TEST_CASE(returnReference28);
TEST_CASE(returnReference29);
TEST_CASE(returnReferenceFunction);
TEST_CASE(returnReferenceContainer);
TEST_CASE(returnReferenceLiteral);
Expand Down Expand Up @@ -1757,6 +1758,19 @@ class TestAutoVariables : public TestFixture {
ASSERT_EQUALS("", errout_str());
}

void returnReference29()
{
check("const std::string& f() {\n" // #12548
" return std::string{};\n"
"}\n"
"const std::string& g() {\n"
" return {};\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2:23]: (error) Reference to temporary returned. [returnTempReference]\n"
"[test.cpp:5:12]: (error) Reference to temporary returned. [returnTempReference]\n",
errout_str());
}

void returnReferenceFunction() {
check("int& f(int& a) {\n"
" return a;\n"
Expand Down