Skip to content
Merged
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
41 changes: 14 additions & 27 deletions lib/checkclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,17 +172,6 @@ void CheckClass::constructors()
if (!printWarnings)
continue;

// #3196 => bailout if there are nested unions
// TODO: handle union variables better
{
const bool bailout = std::any_of(scope->nestedList.cbegin(), scope->nestedList.cend(), [](const Scope* nestedScope) {
return nestedScope->type == ScopeType::eUnion;
});
if (bailout)
continue;
}


std::vector<Usage> usageList = createUsageList(scope);

for (const Function &func : scope->functionList) {
Expand Down Expand Up @@ -310,22 +299,20 @@ void CheckClass::constructors()
// If constructor is not in scope then we maybe using a constructor from a different template specialization
if (!precedes(scope->bodyStart, func.tokenDef))
continue;
const Scope *varType = var.typeScope();
if (!varType || varType->type != ScopeType::eUnion) {
const bool derived = scope != var.scope();
if (func.type == FunctionType::eConstructor &&
func.nestedIn && (func.nestedIn->numConstructors - func.nestedIn->numCopyOrMoveConstructors) > 1 &&
func.argCount() == 0 && func.functionScope &&
func.arg && func.arg->link()->next() == func.functionScope->bodyStart &&
func.functionScope->bodyStart->link() == func.functionScope->bodyStart->next()) {
// don't warn about user defined default constructor when there are other constructors
if (printInconclusive)
uninitVarError(func.token, func.access == AccessControl::Private, func.type, var.scope()->className, var.name(), derived, true);
} else if (missingCopy)
missingMemberCopyError(func.token, func.type, var.scope()->className, var.name());
else
uninitVarError(func.token, func.access == AccessControl::Private, func.type, var.scope()->className, var.name(), derived, false);
}

const bool derived = scope != var.scope();
if (func.type == FunctionType::eConstructor &&
func.nestedIn && (func.nestedIn->numConstructors - func.nestedIn->numCopyOrMoveConstructors) > 1 &&
func.argCount() == 0 && func.functionScope &&
func.arg && func.arg->link()->next() == func.functionScope->bodyStart &&
func.functionScope->bodyStart->link() == func.functionScope->bodyStart->next()) {
// don't warn about user defined default constructor when there are other constructors
if (printInconclusive)
uninitVarError(func.token, func.access == AccessControl::Private, func.type, var.scope()->className, var.name(), derived, true);
} else if (missingCopy)
missingMemberCopyError(func.token, func.type, var.scope()->className, var.name());
else
uninitVarError(func.token, func.access == AccessControl::Private, func.type, var.scope()->className, var.name(), derived, false);
}
}
}
Expand Down
7 changes: 2 additions & 5 deletions test/testconstructors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1302,7 +1302,7 @@ class TestConstructors : public TestFixture {
" {\n"
" }\n"
"};");
TODO_ASSERT_EQUALS("[test.cpp:9]: (warning) Member variable 'Fred::U' is not initialized in the constructor.\n", "", errout_str());
ASSERT_EQUALS("[test.cpp:9]: (warning) Member variable 'Fred::U' is not initialized in the constructor.\n", errout_str());
}


Expand Down Expand Up @@ -3428,9 +3428,6 @@ class TestConstructors : public TestFixture {
}

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

void uninitMissingFuncDef() {
Expand Down
Loading