Fix #3462 FN missed class members in copy operator of derived class (#3837)

This commit is contained in:
chrchr-github 2022-02-16 21:30:19 +01:00 committed by GitHub
parent 6488cb6e76
commit b07814f329
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 5 deletions

View File

@ -819,9 +819,8 @@ void CheckClass::initializeVarList(const Function &func, std::list<const Functio
} }
// Calling member function? // Calling member function?
else if (Token::simpleMatch(ftok, "operator= (") && else if (Token::simpleMatch(ftok, "operator= (")) {
ftok->previous()->str() != "::") { if (ftok->function()) {
if (ftok->function() && ftok->function()->nestedIn == scope) {
const Function *member = ftok->function(); const Function *member = ftok->function();
// recursive call // recursive call
// assume that all variables are initialized // assume that all variables are initialized

View File

@ -120,7 +120,7 @@ private:
TEST_CASE(initvar_union); TEST_CASE(initvar_union);
TEST_CASE(initvar_delegate); // ticket #4302 TEST_CASE(initvar_delegate); // ticket #4302
TEST_CASE(initvar_delegate2); TEST_CASE(initvar_delegate2);
TEST_CASE(initvar_derived_class); // ticket #10161 TEST_CASE(initvar_derived_class);
TEST_CASE(initvar_private_constructor); // BUG 2354171 - private constructor TEST_CASE(initvar_private_constructor); // BUG 2354171 - private constructor
TEST_CASE(initvar_copy_constructor); // ticket #1611 TEST_CASE(initvar_copy_constructor); // ticket #1611
@ -1251,7 +1251,7 @@ private:
} }
void initvar_derived_class() { void initvar_derived_class() {
check("class Base {\n" check("class Base {\n" // #10161
"public:\n" "public:\n"
" virtual void foo() = 0;\n" " virtual void foo() = 0;\n"
" int x;\n" // <- uninitialized " int x;\n" // <- uninitialized
@ -1263,6 +1263,41 @@ private:
" void foo() override;\n" " void foo() override;\n"
"};"); "};");
ASSERT_EQUALS("[test.cpp:9]: (warning) Member variable 'Base::x' is not initialized in the constructor. Maybe it should be initialized directly in the class Base?\n", errout.str()); ASSERT_EQUALS("[test.cpp:9]: (warning) Member variable 'Base::x' is not initialized in the constructor. Maybe it should be initialized directly in the class Base?\n", errout.str());
check("struct A {\n" // #3462
" char ca;\n"
" A& operator=(const A& a) {\n"
" ca = a.ca;\n"
" return *this;\n"
" }\n"
"};\n"
"struct B : public A {\n"
" char cb;\n"
" B& operator=(const B& b) {\n"
" A::operator=(b);\n"
" return *this;\n"
" }\n"
"};\n");
ASSERT_EQUALS("[test.cpp:10]: (warning) Member variable 'B::cb' is not assigned a value in 'B::operator='.\n", errout.str());
check("struct A {\n"
" char ca;\n"
" A& operator=(const A& a) {\n"
" return *this;\n"
" }\n"
"};\n"
"struct B : public A {\n"
" char cb;\n"
" B& operator=(const B& b) {\n"
" A::operator=(b);\n"
" return *this;\n"
" }\n"
"};\n");
ASSERT_EQUALS("[test.cpp:3]: (warning) Member variable 'A::ca' is not assigned a value in 'A::operator='.\n"
"[test.cpp:9]: (warning) Member variable 'B::cb' is not assigned a value in 'B::operator='.\n"
"[test.cpp:9]: (warning) Member variable 'B::ca' is not assigned a value in 'B::operator='.\n",
errout.str());
} }
void initvar_private_constructor() { void initvar_private_constructor() {