Skip to content

Commit 78dd29a

Browse files
Fix #10770 FP noConstructor with function pointer member (cppcheck-opensource#3751)
1 parent 8a7992c commit 78dd29a

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

lib/checkclass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ void CheckClass::constructors()
164164
if (scope->numConstructors == 0 && printStyle && !usedInUnion) {
165165
// If there is a private variable, there should be a constructor..
166166
for (const Variable &var : scope->varlist) {
167-
if (var.isPrivate() && !var.isStatic() && !var.isInit() &&
167+
if (var.isPrivate() && !var.isStatic() && !var.isInit() && !var.hasDefault() &&
168168
(!var.isClass() || (var.type() && var.type()->needInitialization == Type::NeedInitialization::True))) {
169169
noConstructorError(scope->classDef, scope->className, scope->classDef->str() == "struct");
170170
break;

test/testconstructors.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ class TestConstructors : public TestFixture {
102102
TEST_CASE(noConstructor11); // ticket #3552
103103
TEST_CASE(noConstructor12); // #8951 - member initialization
104104
TEST_CASE(noConstructor13); // #9998
105+
TEST_CASE(noConstructor14); // #10770
105106

106107
TEST_CASE(forwardDeclaration); // ticket #4290/#3190
107108

@@ -699,6 +700,17 @@ class TestConstructors : public TestFixture {
699700
ASSERT_EQUALS("", errout.str());
700701
}
701702

703+
void noConstructor14() { // #10770
704+
check("typedef void (*Func)();\n"
705+
"class C {\n"
706+
"public:\n"
707+
" void f();\n"
708+
"private:\n"
709+
" Func fp = nullptr;\n"
710+
"};\n");
711+
ASSERT_EQUALS("", errout.str());
712+
}
713+
702714
// ticket #4290 "False Positive: style (noConstructor): The class 'foo' does not have a constructor."
703715
// ticket #3190 "SymbolDatabase: Parse of sub class constructor fails"
704716
void forwardDeclaration() {

0 commit comments

Comments
 (0)