parent
5a7c998a79
commit
8ce0faf723
|
@ -2504,8 +2504,16 @@ void CheckClass::checkSelfInitialization()
|
|||
continue;
|
||||
|
||||
for (; tok != scope->bodyStart; tok = tok->next()) {
|
||||
if (Token::Match(tok, "[:,] %var% (|{ %var% )|}") && tok->next()->varId() == tok->tokAt(3)->varId()) {
|
||||
selfInitializationError(tok, tok->strAt(1));
|
||||
if (Token::Match(tok, "[:,] %var% (|{")) {
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7186,6 +7186,22 @@ private:
|
|||
"}");
|
||||
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"
|
||||
" std::string s;\n"
|
||||
" Fred() : s(s) {\n"
|
||||
|
|
Loading…
Reference in New Issue