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
3 changes: 2 additions & 1 deletion lib/checkautovariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,8 @@ void CheckAutoVariables::checkVarLifetimeScope(const Token * start, const Token
if ((tokvalue->variable() && !isEscapedReference(tokvalue->variable()) &&
isInScope(tokvalue->variable()->nameToken(), scope)) ||
isDeadTemporary(tokvalue, nullptr, mSettings->library)) {
errorReturnDanglingLifetime(tok, &val);
if (!diag(tokvalue))
errorReturnDanglingLifetime(tok, &val);
break;
}
} else if (tokvalue->variable() && isDeadScope(tokvalue->variable()->nameToken(), tok->scope())) {
Expand Down
11 changes: 9 additions & 2 deletions test/testautovariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2643,8 +2643,7 @@ class TestAutoVariables : public TestFixture {
" auto it = g().begin();\n"
" return it;\n"
"}");
ASSERT_EQUALS("[test.cpp:3:24] -> [test.cpp:3:16] -> [test.cpp:4:5]: (error) Using iterator that is a temporary. [danglingTemporaryLifetime]\n"
"[test.cpp:3:24] -> [test.cpp:4:12]: (error) Returning iterator that will be invalid when returning. [returnDanglingLifetime]\n",
ASSERT_EQUALS("[test.cpp:3:24] -> [test.cpp:3:16] -> [test.cpp:4:5]: (error) Using iterator that is a temporary. [danglingTemporaryLifetime]\n",
errout_str());

check("std::vector<int> g();\n"
Expand Down Expand Up @@ -3142,6 +3141,14 @@ class TestAutoVariables : public TestFixture {
ASSERT_EQUALS(
"[test.cpp:3:12] -> [test.cpp:2:10] -> [test.cpp:3:12]: (error) Returning pointer to local variable 'a' that will be invalid when returning. [returnDanglingLifetime]\n",
errout_str());

check("std::string_view f() {\n" // #14465
" std::vector<std::string> v;\n"
" return *v.begin();\n"
"}\n");
ASSERT_EQUALS(
"[test.cpp:3:12] -> [test.cpp:3:12]: (error) Returning object that will be invalid when returning. [returnDanglingLifetime]\n",
errout_str());
}

void danglingLifetimeUniquePtr()
Expand Down
Loading