Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/checkuninitvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1671,6 +1671,8 @@ void CheckUninitVar::valueFlowUninit()
const bool isleaf = isLeafDot(tok) || uninitderef;
if (!isleaf && Token::Match(tok->astParent(), ". %name%") && (tok->astParent()->next()->varId() || tok->astParent()->next()->isEnumerator()))
continue;
if (isWithinScope(tok, tok->variable(), Scope::ScopeType::eLambda))
continue;
}
const ExprUsage usage = getExprUsage(tok, v->indirect, *mSettings);
if (usage == ExprUsage::NotUsed || usage == ExprUsage::Inconclusive)
Expand Down
12 changes: 12 additions & 0 deletions test/testuninitvar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class TestUninitVar : public TestFixture {
TEST_CASE(valueFlowUninit2_value);
TEST_CASE(valueFlowUninit_uninitvar2);
TEST_CASE(valueFlowUninit_functioncall);
TEST_CASE(valueFlowUninit_lambda); // #12944
TEST_CASE(uninitStructMember); // struct members
TEST_CASE(uninitvar2_while);
TEST_CASE(uninitvar2_4494); // #4494
Expand Down Expand Up @@ -4389,6 +4390,17 @@ class TestUninitVar : public TestFixture {
ASSERT_EQUALS("[test.cpp:8] -> [test.cpp:4]: (warning) Uninitialized variable: i\n", errout_str());
}

void valueFlowUninit_lambda() { // #12944
valueFlowUninit( "void Fun()\n"
"{\n"
" int var;\n"
" auto lam = [&]() { int x = var; };\n"
" var = 5;\n"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume after your changes that no error will be written if var = 5; is removed?

It feels like a better fix would be in valueflow? I assume it "executes" the lambda when it is defined. It should probably skip the lambda first and then when lam() is called it can "execute" the lambda.

" lam();\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}

void uninitStructMember() { // struct members
checkUninitVar("struct AB { int a; int b; };\n"
"void f(void) {\n"
Expand Down