Skip to content

Commit 5d79fe2

Browse files
authored
Fix 13760: false negative: danglingTemporaryLifetime with temporary lambda (danmar#7460)
1 parent d3adf44 commit 5d79fe2

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

lib/astutils.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,8 @@ bool isTemporary(const Token* tok, const Library* library, bool unknown)
440440
return isTemporary(tok->astOperand2(), library);
441441
if (tok->isCast() || (tok->isCpp() && isCPPCast(tok)))
442442
return isTemporary(tok->astOperand2(), library);
443+
if (findLambdaEndToken(tok) != nullptr)
444+
return true;
443445
if (Token::Match(tok, ".|[|++|--|%name%|%assign%"))
444446
return false;
445447
if (tok->isUnaryOp("*"))

test/testautovariables.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4209,6 +4209,21 @@ class TestAutoVariables : public TestFixture {
42094209
" if (!p) {}\n"
42104210
"}\n");
42114211
ASSERT_EQUALS("", errout_str());
4212+
4213+
// #13760
4214+
check("template<class T>\n"
4215+
"auto f(T&& x) {\n"
4216+
" return [&] {\n"
4217+
" return x();\n"
4218+
" };\n"
4219+
"}\n"
4220+
"auto g() {\n"
4221+
" auto y = f([](auto x) { return 1; });\n"
4222+
" return y();\n"
4223+
"}\n");
4224+
ASSERT_EQUALS(
4225+
"[test.cpp:3:12] -> [test.cpp:2:13] -> [test.cpp:4:16] -> [test.cpp:8:16] -> [test.cpp:8:16] -> [test.cpp:9:12]: (error) Using object that is a temporary. [danglingTemporaryLifetime]\n",
4226+
errout_str());
42124227
}
42134228

42144229
void danglingLifetimeBorrowedMembers()

0 commit comments

Comments
 (0)