From 78dd29ada3e6b0eb0b5a54ff081d0cb34f0728f4 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 3 Mar 2022 13:56:24 +0100 Subject: [PATCH] Fix #10770 FP noConstructor with function pointer member (#3751) --- lib/checkclass.cpp | 2 +- test/testconstructors.cpp | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index ff4f87252..0bcfe25e5 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -164,7 +164,7 @@ void CheckClass::constructors() if (scope->numConstructors == 0 && printStyle && !usedInUnion) { // If there is a private variable, there should be a constructor.. for (const Variable &var : scope->varlist) { - if (var.isPrivate() && !var.isStatic() && !var.isInit() && + if (var.isPrivate() && !var.isStatic() && !var.isInit() && !var.hasDefault() && (!var.isClass() || (var.type() && var.type()->needInitialization == Type::NeedInitialization::True))) { noConstructorError(scope->classDef, scope->className, scope->classDef->str() == "struct"); break; diff --git a/test/testconstructors.cpp b/test/testconstructors.cpp index 0dcf7b4d0..271b6a518 100644 --- a/test/testconstructors.cpp +++ b/test/testconstructors.cpp @@ -102,6 +102,7 @@ private: TEST_CASE(noConstructor11); // ticket #3552 TEST_CASE(noConstructor12); // #8951 - member initialization TEST_CASE(noConstructor13); // #9998 + TEST_CASE(noConstructor14); // #10770 TEST_CASE(forwardDeclaration); // ticket #4290/#3190 @@ -699,6 +700,17 @@ private: ASSERT_EQUALS("", errout.str()); } + void noConstructor14() { // #10770 + check("typedef void (*Func)();\n" + "class C {\n" + "public:\n" + " void f();\n" + "private:\n" + " Func fp = nullptr;\n" + "};\n"); + ASSERT_EQUALS("", errout.str()); + } + // ticket #4290 "False Positive: style (noConstructor): The class 'foo' does not have a constructor." // ticket #3190 "SymbolDatabase: Parse of sub class constructor fails" void forwardDeclaration() {