ValueFlow: Avoid FP when lambda is used

This commit is contained in:
Daniel Marjamäki 2018-12-04 18:54:26 +01:00
parent 452318dfa4
commit 88c61e8d1e
2 changed files with 11 additions and 0 deletions

View File

@ -1882,6 +1882,8 @@ static bool valueFlowForward(Token * const startToken,
// TODO: handle lambda functions // TODO: handle lambda functions
if (tok2->str() == "[" && findLambdaEndToken(tok2)) { if (tok2->str() == "[" && findLambdaEndToken(tok2)) {
Token *lambdaEndToken = const_cast<Token *>(findLambdaEndToken(tok2)); Token *lambdaEndToken = const_cast<Token *>(findLambdaEndToken(tok2));
if (isVariableChanged(lambdaEndToken->link(), lambdaEndToken, varid, var->isGlobal(), settings, tokenlist->isCPP()))
return false;
// Dont skip lambdas for lifetime values // Dont skip lambdas for lifetime values
if (!std::all_of(values.begin(), values.end(), std::mem_fn(&ValueFlow::Value::isLifetimeValue))) { if (!std::all_of(values.begin(), values.end(), std::mem_fn(&ValueFlow::Value::isLifetimeValue))) {
tok2 = lambdaEndToken; tok2 = lambdaEndToken;

View File

@ -1441,6 +1441,15 @@ private:
"}\n"; "}\n";
ASSERT_EQUALS(false, testValueOfX(code, 4U, 0)); ASSERT_EQUALS(false, testValueOfX(code, 4U, 0));
code = "int f() {\n"
" int x = 1;\n"
" dostuff([&]() {\n"
" x = y;\n"
" });\n"
" return x;\n"
"}\n";
ASSERT_EQUALS(false, testValueOfX(code, 6U, 1));
// ?: // ?:
code = "void f() {\n" code = "void f() {\n"
" int x = 8;\n" " int x = 8;\n"