Skip to content

Commit 8ee2ccb

Browse files
Fix #12548 FN returnTempReference with braced initializer (regression) (#8409)
Co-authored-by: chrchr-github <noreply@github.com>
1 parent d17592e commit 8ee2ccb

2 files changed

Lines changed: 15 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: 14 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,19 @@ class TestAutoVariables : public TestFixture {
17571758
ASSERT_EQUALS("", errout_str());
17581759
}
17591760

1761+
void returnReference29()
1762+
{
1763+
check("const std::string& f() {\n" // #12548
1764+
" return std::string{};\n"
1765+
"}\n"
1766+
"const std::string& g() {\n"
1767+
" return {};\n"
1768+
"}\n");
1769+
ASSERT_EQUALS("[test.cpp:2:23]: (error) Reference to temporary returned. [returnTempReference]\n"
1770+
"[test.cpp:5:12]: (error) Reference to temporary returned. [returnTempReference]\n",
1771+
errout_str());
1772+
}
1773+
17601774
void returnReferenceFunction() {
17611775
check("int& f(int& a) {\n"
17621776
" return a;\n"

0 commit comments

Comments
 (0)