Skip to content
Open
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
13 changes: 12 additions & 1 deletion lib/checkclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ static bool isVclTypeInit(const Type *type)
return false;
});
}

// Returns true if the class has constructors but none of them can be called without arguments
static bool hasNoDefaultConstructor(const Scope& scope)
{
return scope.numConstructors > 0 && std::none_of(scope.functionList.cbegin(), scope.functionList.cend(), [](const Function& func) {
// a variadic constructor can be called without arguments if only the parameter pack or ellipsis is left
const size_t requiredArgs = func.isVariadic() ? 1 : 0;
return func.isConstructor() && func.minArgCount() == requiredArgs && !func.isDelete();
});
}
//---------------------------------------------------------------------------

CheckClassImpl::CheckClassImpl(const Tokenizer *tokenizer, const Settings &settings, ErrorLogger &errorLogger)
Expand Down Expand Up @@ -447,7 +457,8 @@ void CheckClassImpl::constructors()
if (var.hasDefault()) {
usage.init = true;
hasAnyDefaultInit = true;
} else if (!var.isStatic() && (var.isReference() || (var.isConst() && !var.isClass()))) {
} else if (!var.isStatic() && (var.isReference() || (var.isConst() && !var.isClass()) ||
(!var.isPointer() && !var.isPointerArray() && var.typeScope() && hasNoDefaultConstructor(*var.typeScope())))) {
// the default constructor is deleted, so all members are initialized by every instance
isDefaultConstructible = false;
} else if (cpp14OrLater && !hasAnySelfInit && checkInitialization(usage, FunctionType::eConstructor) == Initialization::Initialized) {
Expand Down
54 changes: 54 additions & 0 deletions test/testconstructors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,60 @@ class TestConstructors : public TestFixture {
ASSERT_EQUALS("[test.cpp:3:9]: (warning) Member variable 'S::x' has no initializer. [uninitMemberVarNoCtor]\n"
"[test.cpp:7:9]: (warning) Member variable 'T::x' has no initializer. [uninitMemberVarNoCtor]\n",
errout_str());

check("struct N {\n" // #15197
" explicit N(int);\n"
"};\n"
"struct D {\n"
" D() = delete;\n"
"};\n"
"struct S {\n"
" N n;\n"
" int i;\n"
" bool b = true;\n"
"};\n"
"struct W {\n"
" W(int, ...);\n"
"};\n"
"struct T {\n"
" D d;\n"
" int i;\n"
" bool b = true;\n"
"};\n"
"struct U {\n"
" W w;\n"
" int i;\n"
" bool b = true;\n"
"};\n"
"struct A {\n"
" N a[2];\n"
" int i;\n"
" bool b = true;\n"
"};\n");
ASSERT_EQUALS("", errout_str());

check("struct N {\n"
" explicit N(int = 0);\n"
"};\n"
"struct V {\n"
" template<class... A> explicit V(A&&... a);\n"
"};\n"
"struct C {\n"
" C(...);\n"
"};\n"
"struct M {\n"
" explicit M(int);\n"
"};\n"
"struct S {\n"
" N n;\n"
" V v;\n"
" C c;\n"
" M* p;\n"
" M* q[2];\n"
" int i;\n"
" bool b = true;\n"
"};\n");
ASSERT_EQUALS("[test.cpp:19:9]: (warning) Member variable 'S::i' has no initializer. [uninitMemberVarNoCtor]\n", errout_str());
}

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