Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 0 additions & 51 deletions test/testclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ class TestClass : public TestFixture {
TEST_CASE(operatorEqToSelf8); // ticket #2179
TEST_CASE(operatorEqToSelf9); // ticket #2592

TEST_CASE(operatorEqPtrAssign); // ticket #13203

TEST_CASE(memsetOnStruct);
TEST_CASE(memsetVector);
TEST_CASE(memsetOnClass);
Expand Down Expand Up @@ -2591,55 +2589,6 @@ class TestClass : public TestFixture {
ASSERT_EQUALS("", errout_str());
}

#define checkConstructors(code) checkConstructors_(code, __FILE__, __LINE__)
template<size_t size>
void checkConstructors_(const char (&code)[size], const char* file, int line) {
const Settings settings = settingsBuilder().severity(Severity::warning).build();

// Tokenize..
SimpleTokenizer tokenizer(settings, *this);
ASSERT_LOC(tokenizer.tokenize(code), file, line);

// Check..
CheckClass checkClass(&tokenizer, &settings, this);
(checkClass.constructors)();
}

void operatorEqPtrAssign() { // ticket #13203
checkConstructors("struct S {\n"
" int* m_data;\n"
" S() : m_data(new int()) {}\n"
" S& operator=(const S& s) {\n"
" if (&s != this) {\n"
" *(m_data) = *(s.m_data);\n"
" }\n"
" return *this;\n"
" }\n"
"};\n");
ASSERT_EQUALS("", errout_str());

checkConstructors("struct S {\n"
" int* m_data;\n"
" S() : m_data(new int()) {}\n"
" S& operator=(const S& s) {\n"
" if (&s != this) {\n"
" (*m_data) = *(s.m_data);\n"
" }\n"
" return *this;\n"
" }\n"
"};\n");
ASSERT_EQUALS("", errout_str());

checkConstructors("struct S {\n"
" int* m_data;\n"
" S() : m_data(new int()) {}\n"
" S& operator=(const S& s) {\n"
" return *this;\n"
" }\n"
"};\n");
ASSERT_EQUALS("[test.cpp:4]: (warning) Member variable 'S::m_data' is not assigned a value in 'S::operator='.\n", errout_str());
}

// Check that base classes have virtual destructors
#define checkVirtualDestructor(...) checkVirtualDestructor_(__FILE__, __LINE__, __VA_ARGS__)
template<size_t size>
Expand Down
43 changes: 43 additions & 0 deletions test/testconstructors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ class TestConstructors : public TestFixture {
TEST_CASE(initvar_operator_eq6);
TEST_CASE(initvar_operator_eq7);
TEST_CASE(initvar_operator_eq8);
TEST_CASE(initvar_operator_eq9);
TEST_CASE(initvar_operator_eq10);
TEST_CASE(initvar_operator_eq11);
TEST_CASE(initvar_same_classname); // BUG 2208157
TEST_CASE(initvar_chained_assign); // BUG 2270433
TEST_CASE(initvar_2constructors); // BUG 2270353
Expand Down Expand Up @@ -997,6 +1000,46 @@ class TestConstructors : public TestFixture {
"[test.cpp:13]: (warning) Member variable 'D3::d2' is not assigned a value in 'D3::operator='.\n", errout_str());
}

void initvar_operator_eq9() { // ticket #13203
check("struct S {\n"
" int* m_data;\n"
" S() : m_data(new int()) {}\n"
" S& operator=(const S& s) {\n"
" if (&s != this) {\n"
" *(m_data) = *(s.m_data);\n"
" }\n"
" return *this;\n"
" }\n"
"};\n");
ASSERT_EQUALS("", errout_str());
}

void initvar_operator_eq10() {
check("struct S {\n"
" int* m_data;\n"
" S() : m_data(new int()) {}\n"
" S& operator=(const S& s) {\n"
" if (&s != this) {\n"
" (*m_data) = *(s.m_data);\n"
" }\n"
" return *this;\n"
" }\n"
"};\n");
ASSERT_EQUALS("", errout_str());
}

void initvar_operator_eq11() {
check("struct S {\n"
" int* m_data;\n"
" S() : m_data(new int()) {}\n"
" S& operator=(const S& s) {\n"
" return *this;\n"
" }\n"
"};\n");
ASSERT_EQUALS("[test.cpp:4]: (warning) Member variable 'S::m_data' is not assigned a value in 'S::operator='.\n", errout_str());
}


void initvar_same_classname() {
// Bug 2208157 - False positive: Uninitialized variable, same class name

Expand Down