Skip to content

Commit 3b12959

Browse files
Fix #14189 FP useInitializationList when members are assigned (danmar#7908)
1 parent a423574 commit 3b12959

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

lib/checkclass.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,6 +1226,10 @@ void CheckClass::initializationListUsage()
12261226
allowed = false;
12271227
return ChildrenToVisit::done;
12281228
}
1229+
if (var2->isLocal() && isVariableChanged(var2->nameToken(), previousBeforeAstLeftmostLeaf(tok), var2->declarationId(), /*globalvar*/ false, *mSettings)) {
1230+
allowed = false;
1231+
return ChildrenToVisit::done;
1232+
}
12291233
} else if (tok2->str() == "this") { // 'this' instance is not completely constructed in initialization list
12301234
allowed = false;
12311235
return ChildrenToVisit::done;

test/testclass.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8119,6 +8119,21 @@ class TestClass : public TestFixture {
81198119
" std::string st;\n"
81208120
"};");
81218121
ASSERT_EQUALS("", errout_str());
8122+
8123+
checkInitializationListUsage("struct S {\n" // #14189
8124+
" S() {}\n"
8125+
" int i{};\n"
8126+
"};\n"
8127+
"struct T { explicit T(const S&); };\n"
8128+
"class C {\n"
8129+
" C() {\n"
8130+
" S s;\n"
8131+
" s.i = 1;\n"
8132+
" p = std::make_unique<T>(s);\n"
8133+
" }\n"
8134+
" std::unique_ptr<T> p;\n"
8135+
"};");
8136+
ASSERT_EQUALS("", errout_str());
81228137
}
81238138

81248139

0 commit comments

Comments
 (0)