Fix #10427 Regression: selfInitialization (#4070)

This commit is contained in:
chrchr-github 2022-05-02 16:49:13 +02:00 committed by GitHub
parent 5a7c998a79
commit 8ce0faf723
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 2 deletions

View File

@ -2504,8 +2504,16 @@ void CheckClass::checkSelfInitialization()
continue; continue;
for (; tok != scope->bodyStart; tok = tok->next()) { for (; tok != scope->bodyStart; tok = tok->next()) {
if (Token::Match(tok, "[:,] %var% (|{ %var% )|}") && tok->next()->varId() == tok->tokAt(3)->varId()) { if (Token::Match(tok, "[:,] %var% (|{")) {
selfInitializationError(tok, tok->strAt(1)); const Token* varTok = tok->next();
if (Token::Match(varTok->astParent(), "(|{")) {
if (const Token* initTok = varTok->astParent()->astOperand2()) {
if (initTok->varId() == varTok->varId())
selfInitializationError(tok, varTok->str());
else if (initTok->isCast() && ((initTok->astOperand1() && initTok->astOperand1()->varId() == varTok->varId()) || (initTok->astOperand2() && initTok->astOperand2()->varId() == varTok->varId())))
selfInitializationError(tok, varTok->str());
}
}
} }
} }
} }

View File

@ -7186,6 +7186,22 @@ private:
"}"); "}");
ASSERT_EQUALS("[test.cpp:5]: (error) Member variable 'i' is initialized by itself.\n", errout.str()); ASSERT_EQUALS("[test.cpp:5]: (error) Member variable 'i' is initialized by itself.\n", errout.str());
checkSelfInitialization("class A {\n" // #10427
"public:\n"
" explicit A(int x) : _x(static_cast<int>(_x)) {}\n"
"private:\n"
" int _x;\n"
"};\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Member variable '_x' is initialized by itself.\n", errout.str());
checkSelfInitialization("class A {\n"
"public:\n"
" explicit A(int x) : _x((int)(_x)) {}\n"
"private:\n"
" int _x;\n"
"};\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Member variable '_x' is initialized by itself.\n", errout.str());
checkSelfInitialization("class Fred {\n" checkSelfInitialization("class Fred {\n"
" std::string s;\n" " std::string s;\n"
" Fred() : s(s) {\n" " Fred() : s(s) {\n"