diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index f893e2ede..2c45f8e9a 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -943,9 +943,15 @@ void CheckClass::initializationListUsage() for (const Scope *scope : mSymbolDatabase->functionScopes) { // Check every constructor - if (!scope->function || (!scope->function->isConstructor())) + if (!scope->function || !scope->function->isConstructor()) continue; + // Do not warn when a delegate constructor is called + if (const Token *initList = scope->function->constructorMemberInitialization()) { + if (Token::Match(initList, ": %name% {|(") && initList->strAt(1) == scope->className) + continue; + } + const Scope* owner = scope->functionOf; for (const Token* tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) { if (Token::Match(tok, "%name% (")) // Assignments might depend on this function call or if/for/while/switch statement from now on. diff --git a/test/testclass.cpp b/test/testclass.cpp index eaa197725..34a1d6104 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -6567,6 +6567,19 @@ private: " Foo m_i;\n" "};"); ASSERT_EQUALS("", errout.str()); + + checkInitializationListUsage("class A {\n" // #9821 - delegate constructor + "public:\n" + " A() : st{} {}\n" + "\n" + " explicit A(const std::string &input): A() {\n" + " st = input;\n" + " }\n" + "\n" + "private:\n" + " std::string st;\n" + "};"); + ASSERT_EQUALS("", errout.str()); }