Skip to content

Commit 7303e80

Browse files
Fix #13816 FN uninitMemberVar with union (regression) (danmar#7496)
Re `uninitVarUnion2()`: I don't see why there should be a bailout like that for unions, or any other members. --------- Co-authored-by: chrchr-github <[email protected]>
1 parent 56be00d commit 7303e80

File tree

2 files changed

+16
-32
lines changed

2 files changed

+16
-32
lines changed

lib/checkclass.cpp

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -172,17 +172,6 @@ void CheckClass::constructors()
172172
if (!printWarnings)
173173
continue;
174174

175-
// #3196 => bailout if there are nested unions
176-
// TODO: handle union variables better
177-
{
178-
const bool bailout = std::any_of(scope->nestedList.cbegin(), scope->nestedList.cend(), [](const Scope* nestedScope) {
179-
return nestedScope->type == ScopeType::eUnion;
180-
});
181-
if (bailout)
182-
continue;
183-
}
184-
185-
186175
std::vector<Usage> usageList = createUsageList(scope);
187176

188177
for (const Function &func : scope->functionList) {
@@ -310,22 +299,20 @@ void CheckClass::constructors()
310299
// If constructor is not in scope then we maybe using a constructor from a different template specialization
311300
if (!precedes(scope->bodyStart, func.tokenDef))
312301
continue;
313-
const Scope *varType = var.typeScope();
314-
if (!varType || varType->type != ScopeType::eUnion) {
315-
const bool derived = scope != var.scope();
316-
if (func.type == FunctionType::eConstructor &&
317-
func.nestedIn && (func.nestedIn->numConstructors - func.nestedIn->numCopyOrMoveConstructors) > 1 &&
318-
func.argCount() == 0 && func.functionScope &&
319-
func.arg && func.arg->link()->next() == func.functionScope->bodyStart &&
320-
func.functionScope->bodyStart->link() == func.functionScope->bodyStart->next()) {
321-
// don't warn about user defined default constructor when there are other constructors
322-
if (printInconclusive)
323-
uninitVarError(func.token, func.access == AccessControl::Private, func.type, var.scope()->className, var.name(), derived, true);
324-
} else if (missingCopy)
325-
missingMemberCopyError(func.token, func.type, var.scope()->className, var.name());
326-
else
327-
uninitVarError(func.token, func.access == AccessControl::Private, func.type, var.scope()->className, var.name(), derived, false);
328-
}
302+
303+
const bool derived = scope != var.scope();
304+
if (func.type == FunctionType::eConstructor &&
305+
func.nestedIn && (func.nestedIn->numConstructors - func.nestedIn->numCopyOrMoveConstructors) > 1 &&
306+
func.argCount() == 0 && func.functionScope &&
307+
func.arg && func.arg->link()->next() == func.functionScope->bodyStart &&
308+
func.functionScope->bodyStart->link() == func.functionScope->bodyStart->next()) {
309+
// don't warn about user defined default constructor when there are other constructors
310+
if (printInconclusive)
311+
uninitVarError(func.token, func.access == AccessControl::Private, func.type, var.scope()->className, var.name(), derived, true);
312+
} else if (missingCopy)
313+
missingMemberCopyError(func.token, func.type, var.scope()->className, var.name());
314+
else
315+
uninitVarError(func.token, func.access == AccessControl::Private, func.type, var.scope()->className, var.name(), derived, false);
329316
}
330317
}
331318
}

test/testconstructors.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,7 +1303,7 @@ class TestConstructors : public TestFixture {
13031303
" {\n"
13041304
" }\n"
13051305
"};");
1306-
TODO_ASSERT_EQUALS("[test.cpp:9]: (warning) Member variable 'Fred::U' is not initialized in the constructor.\n", "", errout_str());
1306+
ASSERT_EQUALS("[test.cpp:9]: (warning) Member variable 'Fred::U' is not initialized in the constructor.\n", errout_str());
13071307
}
13081308

13091309

@@ -3429,9 +3429,6 @@ class TestConstructors : public TestFixture {
34293429
}
34303430

34313431
void uninitVarUnion2() {
3432-
// If the "data_type" is 0 it means union member "data" is invalid.
3433-
// So it's ok to not initialize "data".
3434-
// related forum: http://sourceforge.net/apps/phpbb/cppcheck/viewtopic.php?f=3&p=1806
34353432
check("union Data { int id; int *ptr; };\n"
34363433
"\n"
34373434
"class Fred {\n"
@@ -3442,7 +3439,7 @@ class TestConstructors : public TestFixture {
34423439
" Fred() : data_type(0)\n"
34433440
" { }\n"
34443441
"};");
3445-
ASSERT_EQUALS("", errout_str());
3442+
ASSERT_EQUALS("[test.cpp:8]: (warning) Member variable 'Fred::data' is not initialized in the constructor.\n", errout_str());
34463443
}
34473444

34483445
void uninitMissingFuncDef() {

0 commit comments

Comments
 (0)