diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 4fe32f45c..945448710 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -1367,6 +1367,18 @@ static bool valueFlowForward(Token * const startToken, it->inconclusive = true; } } + + // Lambda function + if (Token::simpleMatch(tok2, "= [") && + Token::simpleMatch(tok2->linkAt(1), "] (") && + Token::simpleMatch(tok2->linkAt(1)->linkAt(1), ") {")) { + const Token *bodyStart = tok2->linkAt(1)->linkAt(1)->next(); + if (isVariableChanged(bodyStart, bodyStart->link(), varid)) { + if (settings->debugwarnings) + bailout(tokenlist, errorLogger, tok2, "valueFlowForward, " + var->name() + " is changed in lambda function"); + return false; + } + } } return true; } diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 89620c9e0..aac2f7e1c 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -726,6 +726,17 @@ private: "}"; ASSERT_EQUALS(false, testValueOfX(code, 4U, 0)); + // lambda + code = "void f() {\n" + " int x = 0;\n" + " Q q = [&]() {\n" + " if (x > 0) {}\n" + " x++;\n" + " };\n" + " dosomething(q);\n" + "}\n"; + ASSERT_EQUALS(false, testValueOfX(code, 4U, 0)); + // if/else code = "void f() {\n" " int x = 123;\n"