From fe5de60f3246f918859170b54439635a435e62bc Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Fri, 8 Feb 2013 06:55:45 +0100 Subject: [PATCH] Fixed #4567 (false negative: The class 'B' does not have a constructor.) --- lib/checkclass.cpp | 3 ++- test/testconstructors.cpp | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 547a8965f..94b0e4d7f 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -66,7 +66,8 @@ void CheckClass::constructors() // If there is a private variable, there should be a constructor.. std::list::const_iterator var; for (var = scope->varlist.begin(); var != scope->varlist.end(); ++var) { - if (var->isPrivate() && !var->isClass() && !var->isStatic()) { + if (var->isPrivate() && !var->isStatic() && + (!var->isClass() || (var->type() && var->type()->needInitialization == Scope::True))) { noConstructorError(scope->classDef, scope->className, scope->classDef->str() == "struct"); break; } diff --git a/test/testconstructors.cpp b/test/testconstructors.cpp index 7e0470935..27bb0203d 100644 --- a/test/testconstructors.cpp +++ b/test/testconstructors.cpp @@ -60,6 +60,7 @@ private: TEST_CASE(simple5); // ticket #2560 TEST_CASE(simple6); // ticket #4085 - uninstantiated template class TEST_CASE(simple7); // ticket #4531 + TEST_CASE(simple8); TEST_CASE(initvar_with_this); // BUG 2190300 TEST_CASE(initvar_if); // BUG 2190290 @@ -311,6 +312,14 @@ private: ASSERT_EQUALS("", errout.str()); } + void simple8() { + check("struct Fred { int x; };\n" + "class Barney { Fred fred; };\n" + "class Wilma { struct Betty { int x; } betty; };\n"); + ASSERT_EQUALS("[test.cpp:2]: (style) The class 'Barney' does not have a constructor.\n" + "[test.cpp:3]: (style) The class 'Wilma' does not have a constructor.\n", errout.str()); + } + void initvar_with_this() { check("struct Fred\n" "{\n"