From f95a53b0ca7cdc092ebe7fb87134cfbd9c911a0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 9 Sep 2020 18:04:21 +0200 Subject: [PATCH] Fixed #9821 (False positive: Delegating constructor and initialization list) --- lib/checkclass.cpp | 8 +++++++- test/testclass.cpp | 13 +++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) 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()); }