Skip to content

Commit d9c55e9

Browse files
committed
Fix #12548 FN returnTempReference with braced initializer (regression)
1 parent f6b31b7 commit d9c55e9

2 files changed

Lines changed: 10 additions & 4 deletions

File tree

lib/astutils.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ bool isTemporary(const Token* tok, const Library* library, bool unknown)
461461
return false;
462462
return !branchTok->astOperand1()->valueType()->isTypeEqual(branchTok->astOperand2()->valueType());
463463
}
464-
if (Token::simpleMatch(tok, "(") && tok->astOperand1() &&
464+
if (Token::Match(tok, "(|{") && tok->astOperand1() &&
465465
(tok->astOperand2() || Token::simpleMatch(tok->next(), ")"))) {
466466
if (Token::simpleMatch(tok->astOperand1(), "typeid"))
467467
return false;
@@ -498,9 +498,6 @@ bool isTemporary(const Token* tok, const Library* library, bool unknown)
498498
// Currying a function is unknown in cppcheck
499499
if (Token::simpleMatch(tok, "(") && Token::simpleMatch(tok->astOperand1(), "("))
500500
return unknown;
501-
if (Token::simpleMatch(tok, "{") && Token::simpleMatch(tok->astParent(), "return") && tok->astOperand1() &&
502-
!tok->astOperand2())
503-
return isTemporary(tok->astOperand1(), library);
504501
return true;
505502
}
506503

test/testautovariables.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ class TestAutoVariables : public TestFixture {
131131
TEST_CASE(returnReference26);
132132
TEST_CASE(returnReference27);
133133
TEST_CASE(returnReference28);
134+
TEST_CASE(returnReference29);
134135
TEST_CASE(returnReferenceFunction);
135136
TEST_CASE(returnReferenceContainer);
136137
TEST_CASE(returnReferenceLiteral);
@@ -1757,6 +1758,14 @@ class TestAutoVariables : public TestFixture {
17571758
ASSERT_EQUALS("", errout_str());
17581759
}
17591760

1761+
void returnReference29()
1762+
{
1763+
check("const std::string& s() {\n" // #12548
1764+
" return std::string{};\n"
1765+
"}\n");
1766+
ASSERT_EQUALS("[test.cpp:2:23]: (error) Reference to temporary returned. [returnTempReference]\n", errout_str());
1767+
}
1768+
17601769
void returnReferenceFunction() {
17611770
check("int& f(int& a) {\n"
17621771
" return a;\n"

0 commit comments

Comments
 (0)