From 9d8f798aca920b8d48d7560eae98155334755993 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 2 Feb 2019 18:24:48 +0100 Subject: [PATCH] Fixed #8951 (false postive: (style) The class 'x' does not have a constructor although it has private member variables.) --- lib/checkclass.cpp | 1 + test/testconstructors.cpp | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 13c6a6acd..462490f47 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -110,6 +110,7 @@ void CheckClass::constructors() // If there is a private variable, there should be a constructor.. for (const Variable &var : scope->varlist) { if (var.isPrivate() && !var.isStatic() && !Token::Match(var.nameToken(), "%varid% ; %varid% =", var.declarationId()) && + !Token::Match(var.nameToken(), "%var% {|=") && (!var.isClass() || (var.type() && var.type()->needInitialization == Type::True))) { noConstructorError(scope->classDef, scope->className, scope->classDef->str() == "struct"); break; diff --git a/test/testconstructors.cpp b/test/testconstructors.cpp index 37358797a..6e40a8b0e 100644 --- a/test/testconstructors.cpp +++ b/test/testconstructors.cpp @@ -78,6 +78,7 @@ private: TEST_CASE(noConstructor9); // ticket #4419 TEST_CASE(noConstructor10); // ticket #6614 TEST_CASE(noConstructor11); // ticket #3552 + TEST_CASE(noConstructor12); // #8951 - member initialization TEST_CASE(forwardDeclaration); // ticket #4290/#3190 @@ -578,6 +579,14 @@ private: ASSERT_EQUALS("", errout.str()); } + void noConstructor12() { // #8951 + check("class Fred { int x{0}; };"); + ASSERT_EQUALS("", errout.str()); + + check("class Fred { int x=0; };"); + 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() {