Fixed #8951 (false postive: (style) The class 'x' does not have a constructor although it has private member variables.)

This commit is contained in:
Daniel Marjamäki 2019-02-02 18:24:48 +01:00
parent fed8769687
commit 9d8f798aca
2 changed files with 10 additions and 0 deletions

View File

@ -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;

View File

@ -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() {