Fixed #8850 (Array in-class initialization for private member considered uninitialized)

This commit is contained in:
Daniel Marjamäki 2019-02-02 18:34:41 +01:00
parent 9d8f798aca
commit 4457faa26b
2 changed files with 10 additions and 1 deletions

View File

@ -109,8 +109,11 @@ void CheckClass::constructors()
if (scope->numConstructors == 0 && printStyle && !usedInUnion) { if (scope->numConstructors == 0 && printStyle && !usedInUnion) {
// If there is a private variable, there should be a constructor.. // If there is a private variable, there should be a constructor..
for (const Variable &var : scope->varlist) { for (const Variable &var : scope->varlist) {
const Token *initTok = var.nameToken();
while (Token::simpleMatch(initTok->next(), "["))
initTok = initTok->linkAt(1);
if (var.isPrivate() && !var.isStatic() && !Token::Match(var.nameToken(), "%varid% ; %varid% =", var.declarationId()) && if (var.isPrivate() && !var.isStatic() && !Token::Match(var.nameToken(), "%varid% ; %varid% =", var.declarationId()) &&
!Token::Match(var.nameToken(), "%var% {|=") && !Token::Match(initTok, "%var%|] {|=") &&
(!var.isClass() || (var.type() && var.type()->needInitialization == Type::True))) { (!var.isClass() || (var.type() && var.type()->needInitialization == Type::True))) {
noConstructorError(scope->classDef, scope->className, scope->classDef->str() == "struct"); noConstructorError(scope->classDef, scope->className, scope->classDef->str() == "struct");
break; break;

View File

@ -585,6 +585,12 @@ private:
check("class Fred { int x=0; };"); check("class Fred { int x=0; };");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
check("class Fred { int x[1]={0}; };"); // #8850
ASSERT_EQUALS("", errout.str());
check("class Fred { int x[1]{0}; };");
ASSERT_EQUALS("", errout.str());
} }
// ticket #4290 "False Positive: style (noConstructor): The class 'foo' does not have a constructor." // ticket #4290 "False Positive: style (noConstructor): The class 'foo' does not have a constructor."