File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -104,6 +104,16 @@ static bool isVclTypeInit(const Type *type)
104104 return false ;
105105 });
106106}
107+
108+ // Returns true if the class has constructors but none of them can be called without arguments
109+ static bool hasNoDefaultConstructor (const Scope& scope)
110+ {
111+ return scope.numConstructors > 0 && std::none_of (scope.functionList .cbegin (), scope.functionList .cend (), [](const Function& func) {
112+ // a variadic constructor can be called without arguments if only the parameter pack or ellipsis is left
113+ const size_t requiredArgs = func.isVariadic () ? 1 : 0 ;
114+ return func.isConstructor () && func.minArgCount () == requiredArgs && !func.isDelete ();
115+ });
116+ }
107117// ---------------------------------------------------------------------------
108118
109119CheckClassImpl::CheckClassImpl (const Tokenizer *tokenizer, const Settings &settings, ErrorLogger &errorLogger)
@@ -447,7 +457,8 @@ void CheckClassImpl::constructors()
447457 if (var.hasDefault ()) {
448458 usage.init = true ;
449459 hasAnyDefaultInit = true ;
450- } else if (!var.isStatic () && (var.isReference () || (var.isConst () && !var.isClass ()))) {
460+ } else if (!var.isStatic () && (var.isReference () || (var.isConst () && !var.isClass ()) ||
461+ (!var.isPointer () && !var.isPointerArray () && var.typeScope () && hasNoDefaultConstructor (*var.typeScope ())))) {
451462 // the default constructor is deleted, so all members are initialized by every instance
452463 isDefaultConstructible = false ;
453464 } else if (cpp14OrLater && !hasAnySelfInit && checkInitialization (usage, FunctionType::eConstructor) == Initialization::Initialized) {
Original file line number Diff line number Diff line change @@ -940,6 +940,60 @@ class TestConstructors : public TestFixture {
940940 ASSERT_EQUALS (" [test.cpp:3:9]: (warning) Member variable 'S::x' has no initializer. [uninitMemberVarNoCtor]\n "
941941 " [test.cpp:7:9]: (warning) Member variable 'T::x' has no initializer. [uninitMemberVarNoCtor]\n " ,
942942 errout_str ());
943+
944+ check (" struct N {\n " // #15197
945+ " explicit N(int);\n "
946+ " };\n "
947+ " struct D {\n "
948+ " D() = delete;\n "
949+ " };\n "
950+ " struct S {\n "
951+ " N n;\n "
952+ " int i;\n "
953+ " bool b = true;\n "
954+ " };\n "
955+ " struct W {\n "
956+ " W(int, ...);\n "
957+ " };\n "
958+ " struct T {\n "
959+ " D d;\n "
960+ " int i;\n "
961+ " bool b = true;\n "
962+ " };\n "
963+ " struct U {\n "
964+ " W w;\n "
965+ " int i;\n "
966+ " bool b = true;\n "
967+ " };\n "
968+ " struct A {\n "
969+ " N a[2];\n "
970+ " int i;\n "
971+ " bool b = true;\n "
972+ " };\n " );
973+ ASSERT_EQUALS (" " , errout_str ());
974+
975+ check (" struct N {\n "
976+ " explicit N(int = 0);\n "
977+ " };\n "
978+ " struct V {\n "
979+ " template<class... A> explicit V(A&&... a);\n "
980+ " };\n "
981+ " struct C {\n "
982+ " C(...);\n "
983+ " };\n "
984+ " struct M {\n "
985+ " explicit M(int);\n "
986+ " };\n "
987+ " struct S {\n "
988+ " N n;\n "
989+ " V v;\n "
990+ " C c;\n "
991+ " M* p;\n "
992+ " M* q[2];\n "
993+ " int i;\n "
994+ " bool b = true;\n "
995+ " };\n " );
996+ ASSERT_EQUALS (" [test.cpp:19:9]: (warning) Member variable 'S::i' has no initializer. [uninitMemberVarNoCtor]\n " , errout_str ());
943997 }
944998
945999 // ticket #4290 "False Positive: style (noConstructor): The class 'foo' does not have a constructor."
You can’t perform that action at this time.
0 commit comments