From 88c61e8d1e8c5495ba3a3ecfa7afc8e989b4b182 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 4 Dec 2018 18:54:26 +0100 Subject: [PATCH] ValueFlow: Avoid FP when lambda is used --- lib/valueflow.cpp | 2 ++ test/testvalueflow.cpp | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 0b8deffc7..eba71bb64 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -1882,6 +1882,8 @@ static bool valueFlowForward(Token * const startToken, // TODO: handle lambda functions if (tok2->str() == "[" && findLambdaEndToken(tok2)) { Token *lambdaEndToken = const_cast(findLambdaEndToken(tok2)); + if (isVariableChanged(lambdaEndToken->link(), lambdaEndToken, varid, var->isGlobal(), settings, tokenlist->isCPP())) + return false; // Dont skip lambdas for lifetime values if (!std::all_of(values.begin(), values.end(), std::mem_fn(&ValueFlow::Value::isLifetimeValue))) { tok2 = lambdaEndToken; diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 37b3c6ff7..3a4bef722 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -1441,6 +1441,15 @@ private: "}\n"; 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" " int x = 8;\n"