Skip to content

Commit 62ebc00

Browse files
Fix #12944 FP uninitvar reported in lambda (#8237)
1 parent 0a3f445 commit 62ebc00

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

lib/valueflow.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5958,9 +5958,12 @@ static Token* findStartToken(const Variable* var, Token* start, const Library& l
59585958
}))
59595959
return first->previous();
59605960
// Compute the outer scope
5961-
while (scope && scope->nestedIn != var->scope())
5961+
while (scope && scope->nestedIn != var->scope()) {
5962+
if (scope->type == ScopeType::eLambda && !Token::simpleMatch(scope->bodyEnd, "} ("))
5963+
return start;
59625964
scope = scope->nestedIn;
5963-
if (!scope)
5965+
}
5966+
if (!scope || (scope->type == ScopeType::eLambda && !Token::simpleMatch(scope->bodyEnd, "} (")))
59645967
return start;
59655968
auto* tok = const_cast<Token*>(scope->bodyStart);
59665969
if (!tok)

test/testuninitvar.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6663,6 +6663,26 @@ class TestUninitVar : public TestFixture {
66636663
" return ret;\n"
66646664
"}\n");
66656665
ASSERT_EQUALS("", errout_str());
6666+
6667+
valueFlowUninit("int f() {\n" // #12944
6668+
" int i;\n"
6669+
" auto x = [&]() { return i; };\n"
6670+
" i = 5;\n"
6671+
" return x();\n"
6672+
"}\n"
6673+
"int g() {\n"
6674+
" int i;\n"
6675+
" {\n"
6676+
" auto x = [&]() { return i; };\n"
6677+
" i = 5;\n"
6678+
" return x();\n"
6679+
" }\n"
6680+
"}\n"
6681+
"int h() {\n"
6682+
" int j;\n"
6683+
" return [&]() { return j; }();\n"
6684+
"}\n");
6685+
ASSERT_EQUALS("[test.cpp:17:27]: (error) Uninitialized variable: j [uninitvar]\n", errout_str());
66666686
}
66676687

66686688
void valueFlowUninitBreak() { // Do not show duplicate warnings about the same uninitialized value

0 commit comments

Comments
 (0)