diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index ec90fb3e302..94dde440368 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -5958,9 +5958,12 @@ static Token* findStartToken(const Variable* var, Token* start, const Library& l })) return first->previous(); // Compute the outer scope - while (scope && scope->nestedIn != var->scope()) + while (scope && scope->nestedIn != var->scope()) { + if (scope->type == ScopeType::eLambda && !Token::simpleMatch(scope->bodyEnd, "} (")) + return start; scope = scope->nestedIn; - if (!scope) + } + if (!scope || (scope->type == ScopeType::eLambda && !Token::simpleMatch(scope->bodyEnd, "} ("))) return start; auto* tok = const_cast(scope->bodyStart); if (!tok) diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 15467db7971..ad9dee0fcac 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -6663,6 +6663,26 @@ class TestUninitVar : public TestFixture { " return ret;\n" "}\n"); ASSERT_EQUALS("", errout_str()); + + valueFlowUninit("int f() {\n" // #12944 + " int i;\n" + " auto x = [&]() { return i; };\n" + " i = 5;\n" + " return x();\n" + "}\n" + "int g() {\n" + " int i;\n" + " {\n" + " auto x = [&]() { return i; };\n" + " i = 5;\n" + " return x();\n" + " }\n" + "}\n" + "int h() {\n" + " int j;\n" + " return [&]() { return j; }();\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:17:27]: (error) Uninitialized variable: j [uninitvar]\n", errout_str()); } void valueFlowUninitBreak() { // Do not show duplicate warnings about the same uninitialized value