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
8 changes: 7 additions & 1 deletion lib/checkclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,9 @@ void CheckClass::initializeVarList(const Function &func, std::list<const Functio
if (!Token::Match(ftok->next(), "::| %name%") &&
!Token::Match(ftok->next(), "*| this . %name%") &&
!Token::Match(ftok->next(), "* %name% =") &&
!Token::Match(ftok->next(), "( * this ) . %name%"))
!Token::Match(ftok->next(), "( * this ) . %name%") &&
!Token::Match(ftok->next(), "( * %name% ) =") &&
!Token::Match(ftok->next(), "* ( %name% ) ="))
continue;

// Goto the first token in this statement..
Expand Down Expand Up @@ -1060,6 +1062,10 @@ void CheckClass::initializeVarList(const Function &func, std::list<const Functio
// Assignment of array item of member variable?
else if (Token::Match(ftok, "* %name% =")) {
assignVar(usage, ftok->next()->varId());
} else if (Token::Match(ftok, "( * %name% ) =")) {
assignVar(usage, ftok->tokAt(2)->varId());
} else if (Token::Match(ftok, "* ( %name% ) =")) {
assignVar(usage, ftok->tokAt(2)->varId());
} else if (Token::Match(ftok, "* this . %name% =")) {
assignVar(usage, ftok->tokAt(3)->varId());
} else if (astIsRangeBasedForDecl(ftok)) {
Expand Down
51 changes: 51 additions & 0 deletions test/testclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ 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 @@ -2589,6 +2591,55 @@ class TestClass : public TestFixture {
ASSERT_EQUALS("", errout_str());
}

#define checkConstructors(code) checkConstructors_(code, __FILE__, __LINE__)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be in testconstructors.cpp?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes...
#6913

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