Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions lib/checkautovariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,8 @@ void CheckAutoVariables::checkVarLifetimeScope(const Token * start, const Token
if (tokvalue->exprId() == tok->exprId() && !(tok->variable() && tok->variable()->isArray()) &&
!astIsContainerView(tok->astParent()))
continue;
if (tokvalue->str() == "=" && Token::simpleMatch(tokvalue->astOperand1(), ".") && !tokvalue->astOperand1()->astOperand2())
tokvalue = tokvalue->astOperand2(); // designated initializer
if ((tokvalue->variable() && !isEscapedReference(tokvalue->variable()) &&
isInScope(tokvalue->variable()->nameToken(), scope)) ||
isDeadTemporary(tokvalue, nullptr, mSettings->library)) {
Expand Down
10 changes: 10 additions & 0 deletions test/testautovariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3937,6 +3937,16 @@ class TestAutoVariables : public TestFixture {
ASSERT_EQUALS(
"[test.cpp:6:30] -> [test.cpp:6:30] -> [test.cpp:6:21] -> [test.cpp:5:21] -> [test.cpp:8:12]: (error) Returning object that points to local variable 'a' that will be invalid when returning. [returnDanglingLifetime]\n",
errout_str());

check("struct A { int& x; };\n" // #14247
"A f() {\n"
" int x = 0;\n"
" A a{.x = x};\n"
" return a;\n"
"}\n");
ASSERT_EQUALS(
"[test.cpp:4:12] -> [test.cpp:5:12]: (error) Returning object that will be invalid when returning. [returnDanglingLifetime]\n",
errout_str());
}

void danglingLifetimeInitList() {
Expand Down