Fix issue 9900: False positive: Returning lambda that captures local variable 'x' that will be invalid when returning. (#2809)

This commit is contained in:
Paul Fultz II 2020-09-17 01:33:52 -05:00 committed by GitHub
parent 782684a7cc
commit e5d0ffdbe7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 0 deletions

View File

@ -3845,6 +3845,8 @@ static void valueFlowLifetime(TokenList *tokenlist, SymbolDatabase*, ErrorLogger
}
for (const Token * tok2 = lam.bodyTok; tok2 != lam.bodyTok->link(); tok2 = tok2->next()) {
if (!tok2->variable())
continue;
captureVariable(tok2, lam.implicitCapture, isImplicitCapturingVariable);
}

View File

@ -1914,6 +1914,15 @@ private:
" return [=, &i] { return j; };\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void f(int*);\n"
"auto g(int y) {\n"
" int x = y;\n"
" return [=] {\n"
" g(&x);\n"
" };\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void danglingLifetimeContainer() {