Skip to content

Commit 8043930

Browse files
Fix FN uninitMemberVar with std::array (#4935)
1 parent 1b00a0f commit 8043930

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

lib/checkclass.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,17 @@ void CheckClass::constructors()
252252
// Check if this is a class constructor
253253
if (!var.isPointer() && !var.isPointerArray() && var.isClass() && func.type == Function::eConstructor) {
254254
// Unknown type so assume it is initialized
255-
if (!var.type())
256-
continue;
255+
if (!var.type()) {
256+
if (var.isStlType() && var.valueType() && var.valueType()->containerTypeToken && var.getTypeName() == "std::array") {
257+
const Token* ctt = var.valueType()->containerTypeToken;
258+
if (!ctt->isStandardType() &&
259+
(!ctt->type() || ctt->type()->needInitialization != Type::NeedInitialization::True) &&
260+
!mSettings->library.podtype(ctt->str())) // TODO: handle complex type expression
261+
continue;
262+
}
263+
else
264+
continue;
265+
}
257266

258267
// Known type that doesn't need initialization or
259268
// known type that has member variables of an unknown type

test/testconstructors.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ class TestConstructors : public TestFixture {
194194
TEST_CASE(uninitVarArray7);
195195
TEST_CASE(uninitVarArray8);
196196
TEST_CASE(uninitVarArray9); // ticket #6957, #6959
197+
TEST_CASE(uninitVarArray10);
197198
TEST_CASE(uninitVarArray2D);
198199
TEST_CASE(uninitVarArray3D);
199200
TEST_CASE(uninitVarCpp11Init1);
@@ -3117,6 +3118,28 @@ class TestConstructors : public TestFixture {
31173118
ASSERT_EQUALS("[test.cpp:6]: (warning) Member variable 'sRAIUnitDef::List' is not initialized in the constructor.\n", errout.str());
31183119
}
31193120

3121+
void uninitVarArray10() { // #11650
3122+
Settings s(settings);
3123+
LOAD_LIB_2(s.library, "std.cfg");
3124+
check("struct T { int j; };\n"
3125+
"struct U { int k{}; };\n"
3126+
"struct S {\n"
3127+
" std::array<int, 2> a;\n"
3128+
" std::array<T, 2> b;\n"
3129+
" std::array<std::size_t, 2> c;\n"
3130+
" std::array<clock_t, 2> d;\n"
3131+
" std::array<std::string, 2> e;\n"
3132+
" std::array<U, 2> f;\n"
3133+
"S() {}\n"
3134+
"};\n", s);
3135+
3136+
ASSERT_EQUALS("[test.cpp:10]: (warning) Member variable 'S::a' is not initialized in the constructor.\n"
3137+
"[test.cpp:10]: (warning) Member variable 'S::b' is not initialized in the constructor.\n"
3138+
"[test.cpp:10]: (warning) Member variable 'S::c' is not initialized in the constructor.\n"
3139+
"[test.cpp:10]: (warning) Member variable 'S::d' is not initialized in the constructor.\n",
3140+
errout.str());
3141+
}
3142+
31203143
void uninitVarArray2D() {
31213144
check("class John\n"
31223145
"{\n"

0 commit comments

Comments
 (0)