Fixed #9821 (False positive: Delegating constructor and initialization list)

This commit is contained in:
Daniel Marjamäki 2020-09-09 18:04:21 +02:00
parent 687b44dbb7
commit f95a53b0ca
2 changed files with 20 additions and 1 deletions

View File

@ -943,9 +943,15 @@ void CheckClass::initializationListUsage()
for (const Scope *scope : mSymbolDatabase->functionScopes) { for (const Scope *scope : mSymbolDatabase->functionScopes) {
// Check every constructor // Check every constructor
if (!scope->function || (!scope->function->isConstructor())) if (!scope->function || !scope->function->isConstructor())
continue; 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; const Scope* owner = scope->functionOf;
for (const Token* tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) { 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. if (Token::Match(tok, "%name% (")) // Assignments might depend on this function call or if/for/while/switch statement from now on.

View File

@ -6567,6 +6567,19 @@ private:
" Foo m_i;\n" " Foo m_i;\n"
"};"); "};");
ASSERT_EQUALS("", errout.str()); 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());
} }