diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 462490f47..0ca7cacc5 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -109,8 +109,11 @@ 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) { + 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()) && - !Token::Match(var.nameToken(), "%var% {|=") && + !Token::Match(initTok, "%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 6e40a8b0e..d41fe9034 100644 --- a/test/testconstructors.cpp +++ b/test/testconstructors.cpp @@ -585,6 +585,12 @@ private: check("class Fred { int x=0; };"); 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."