diff --git a/lib/astutils.cpp b/lib/astutils.cpp index fe761b9c42e..9c869bbcffd 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -440,6 +440,8 @@ bool isTemporary(const Token* tok, const Library* library, bool unknown) return isTemporary(tok->astOperand2(), library); if (tok->isCast() || (tok->isCpp() && isCPPCast(tok))) return isTemporary(tok->astOperand2(), library); + if (findLambdaEndToken(tok) != nullptr) + return true; if (Token::Match(tok, ".|[|++|--|%name%|%assign%")) return false; if (tok->isUnaryOp("*")) diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index 217f6c1a606..0c37b7505e1 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -4209,6 +4209,21 @@ class TestAutoVariables : public TestFixture { " if (!p) {}\n" "}\n"); ASSERT_EQUALS("", errout_str()); + + // #13760 + check("template\n" + "auto f(T&& x) {\n" + " return [&] {\n" + " return x();\n" + " };\n" + "}\n" + "auto g() {\n" + " auto y = f([](auto x) { return 1; });\n" + " return y();\n" + "}\n"); + ASSERT_EQUALS( + "[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", + errout_str()); } void danglingLifetimeBorrowedMembers()