Skip to content

Commit f95a53b

Browse files
committed
Fixed #9821 (False positive: Delegating constructor and initialization list)
1 parent 687b44d commit f95a53b

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

lib/checkclass.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -943,9 +943,15 @@ void CheckClass::initializationListUsage()
943943

944944
for (const Scope *scope : mSymbolDatabase->functionScopes) {
945945
// Check every constructor
946-
if (!scope->function || (!scope->function->isConstructor()))
946+
if (!scope->function || !scope->function->isConstructor())
947947
continue;
948948

949+
// Do not warn when a delegate constructor is called
950+
if (const Token *initList = scope->function->constructorMemberInitialization()) {
951+
if (Token::Match(initList, ": %name% {|(") && initList->strAt(1) == scope->className)
952+
continue;
953+
}
954+
949955
const Scope* owner = scope->functionOf;
950956
for (const Token* tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) {
951957
if (Token::Match(tok, "%name% (")) // Assignments might depend on this function call or if/for/while/switch statement from now on.

test/testclass.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6567,6 +6567,19 @@ class TestClass : public TestFixture {
65676567
" Foo m_i;\n"
65686568
"};");
65696569
ASSERT_EQUALS("", errout.str());
6570+
6571+
checkInitializationListUsage("class A {\n" // #9821 - delegate constructor
6572+
"public:\n"
6573+
" A() : st{} {}\n"
6574+
"\n"
6575+
" explicit A(const std::string &input): A() {\n"
6576+
" st = input;\n"
6577+
" }\n"
6578+
"\n"
6579+
"private:\n"
6580+
" std::string st;\n"
6581+
"};");
6582+
ASSERT_EQUALS("", errout.str());
65706583
}
65716584

65726585

0 commit comments

Comments
 (0)