Skip to content

Commit a38b814

Browse files
committed
Fix #14697 FN uninitMemberVarNoCtor when mixing (un)initialized member variables
1 parent 89da2d1 commit a38b814

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

lib/checkclass.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,16 +351,19 @@ void CheckClass::constructors()
351351

352352
// Variables with default initializers
353353
bool hasAnyDefaultInit = false;
354+
bool hasAnySelfInit = false;
354355
for (Usage& usage : usageList) {
355356
const Variable& var = *usage.var;
356357

357358
// check for C++11 initializer
358359
if (var.hasDefault()) {
359360
usage.init = true;
360361
hasAnyDefaultInit = true;
362+
} else if (!hasAnySelfInit && isInitialized(usage, FunctionType::eConstructor)) {
363+
hasAnySelfInit = true;
361364
}
362365
}
363-
if (!hasAnyDefaultInit)
366+
if (!hasAnyDefaultInit && !hasAnySelfInit)
364367
continue;
365368

366369
handleUnionMembers(usageList);

test/testconstructors.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,19 @@ class TestConstructors : public TestFixture {
769769
" int a, b;\n"
770770
"};\n");
771771
ASSERT_EQUALS("", errout_str());
772+
773+
check("struct S { int i = 0; };\n" // #14697
774+
"struct T {\n"
775+
" S s;\n"
776+
" int j;\n"
777+
"};\n"
778+
"struct U {\n"
779+
" std::string a;\n"
780+
" int k;\n"
781+
"};\n");
782+
ASSERT_EQUALS("[test.cpp:4:9]: (warning) Member variable 'T::j' has no initializer. [uninitMemberVarNoCtor]\n"
783+
"[test.cpp:8:9]: (warning) Member variable 'U::k' has no initializer. [uninitMemberVarNoCtor]\n",
784+
errout_str());
772785
}
773786

774787
// ticket #4290 "False Positive: style (noConstructor): The class 'foo' does not have a constructor."

0 commit comments

Comments
 (0)