Skip to content

Commit 792a761

Browse files
Fix #14465 duplicated returnDanglingLifetime (#8184)
Co-authored-by: chrchr-github <noreply@github.com>
1 parent d2c363f commit 792a761

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

lib/checkautovariables.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,8 @@ void CheckAutoVariables::checkVarLifetimeScope(const Token * start, const Token
617617
if ((tokvalue->variable() && !isEscapedReference(tokvalue->variable()) &&
618618
isInScope(tokvalue->variable()->nameToken(), scope)) ||
619619
isDeadTemporary(tokvalue, nullptr, mSettings->library)) {
620-
errorReturnDanglingLifetime(tok, &val);
620+
if (!diag(tokvalue))
621+
errorReturnDanglingLifetime(tok, &val);
621622
break;
622623
}
623624
} else if (tokvalue->variable() && isDeadScope(tokvalue->variable()->nameToken(), tok->scope())) {

test/testautovariables.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2643,8 +2643,7 @@ class TestAutoVariables : public TestFixture {
26432643
" auto it = g().begin();\n"
26442644
" return it;\n"
26452645
"}");
2646-
ASSERT_EQUALS("[test.cpp:3:24] -> [test.cpp:3:16] -> [test.cpp:4:5]: (error) Using iterator that is a temporary. [danglingTemporaryLifetime]\n"
2647-
"[test.cpp:3:24] -> [test.cpp:4:12]: (error) Returning iterator that will be invalid when returning. [returnDanglingLifetime]\n",
2646+
ASSERT_EQUALS("[test.cpp:3:24] -> [test.cpp:3:16] -> [test.cpp:4:5]: (error) Using iterator that is a temporary. [danglingTemporaryLifetime]\n",
26482647
errout_str());
26492648

26502649
check("std::vector<int> g();\n"
@@ -3142,6 +3141,14 @@ class TestAutoVariables : public TestFixture {
31423141
ASSERT_EQUALS(
31433142
"[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",
31443143
errout_str());
3144+
3145+
check("std::string_view f() {\n" // #14465
3146+
" std::vector<std::string> v;\n"
3147+
" return *v.begin();\n"
3148+
"}\n");
3149+
ASSERT_EQUALS(
3150+
"[test.cpp:3:12] -> [test.cpp:3:12]: (error) Returning object that will be invalid when returning. [returnDanglingLifetime]\n",
3151+
errout_str());
31453152
}
31463153

31473154
void danglingLifetimeUniquePtr()

0 commit comments

Comments
 (0)