Skip to content

Commit 8ce0faf

Browse files
Fix #10427 Regression: selfInitialization (#4070)
1 parent 5a7c998 commit 8ce0faf

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

lib/checkclass.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2504,8 +2504,16 @@ void CheckClass::checkSelfInitialization()
25042504
continue;
25052505

25062506
for (; tok != scope->bodyStart; tok = tok->next()) {
2507-
if (Token::Match(tok, "[:,] %var% (|{ %var% )|}") && tok->next()->varId() == tok->tokAt(3)->varId()) {
2508-
selfInitializationError(tok, tok->strAt(1));
2507+
if (Token::Match(tok, "[:,] %var% (|{")) {
2508+
const Token* varTok = tok->next();
2509+
if (Token::Match(varTok->astParent(), "(|{")) {
2510+
if (const Token* initTok = varTok->astParent()->astOperand2()) {
2511+
if (initTok->varId() == varTok->varId())
2512+
selfInitializationError(tok, varTok->str());
2513+
else if (initTok->isCast() && ((initTok->astOperand1() && initTok->astOperand1()->varId() == varTok->varId()) || (initTok->astOperand2() && initTok->astOperand2()->varId() == varTok->varId())))
2514+
selfInitializationError(tok, varTok->str());
2515+
}
2516+
}
25092517
}
25102518
}
25112519
}

test/testclass.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7186,6 +7186,22 @@ class TestClass : public TestFixture {
71867186
"}");
71877187
ASSERT_EQUALS("[test.cpp:5]: (error) Member variable 'i' is initialized by itself.\n", errout.str());
71887188

7189+
checkSelfInitialization("class A {\n" // #10427
7190+
"public:\n"
7191+
" explicit A(int x) : _x(static_cast<int>(_x)) {}\n"
7192+
"private:\n"
7193+
" int _x;\n"
7194+
"};\n");
7195+
ASSERT_EQUALS("[test.cpp:3]: (error) Member variable '_x' is initialized by itself.\n", errout.str());
7196+
7197+
checkSelfInitialization("class A {\n"
7198+
"public:\n"
7199+
" explicit A(int x) : _x((int)(_x)) {}\n"
7200+
"private:\n"
7201+
" int _x;\n"
7202+
"};\n");
7203+
ASSERT_EQUALS("[test.cpp:3]: (error) Member variable '_x' is initialized by itself.\n", errout.str());
7204+
71897205
checkSelfInitialization("class Fred {\n"
71907206
" std::string s;\n"
71917207
" Fred() : s(s) {\n"

0 commit comments

Comments
 (0)