Better checking for assignment to self. Ticket: #1550

This commit is contained in:
Robert Reif 2010-03-31 17:09:59 +02:00 committed by Daniel Marjamäki
parent 32e597e343
commit 7fb5b9b67b
2 changed files with 23 additions and 1 deletions

View File

@ -1089,7 +1089,7 @@ static bool hasDeallocation(const Token * first, const Token * last)
while (tok1 && (tok1 != last))
{
if (Token::Match(tok1, "%var% = new ["))
if (Token::Match(tok1, "%var% = new %type% ["))
{
if (tok1->str() == var->str())
return true;

View File

@ -83,6 +83,7 @@ private:
TEST_CASE(operatorEqToSelf3); // multiple inheritance
TEST_CASE(operatorEqToSelf4); // nested class with multiple inheritance
TEST_CASE(operatorEqToSelf5); // ticket # 1233
TEST_CASE(operatorEqToSelf6); // ticket # 1550
TEST_CASE(memsetOnStruct);
TEST_CASE(memsetOnClass);
@ -820,6 +821,27 @@ private:
ASSERT_EQUALS("", errout.str());
}
void operatorEqToSelf6()
{
// ticket # 1550
checkOpertorEqToSelf(
"class A\n"
"{\n"
"public:\n"
" char *s;\n"
" A & operator=(const A &a)\n"
" {\n"
" delete [] data;\n"
" data = new char[strlen(a.data) + 1];\n"
" strcpy(data, a.data);\n"
" return *this;\n"
" }\n"
"private:\n"
" char * data;\n"
"};");
ASSERT_EQUALS("[test.cpp:5]: (possible style) 'operator=' should check for assignment to self\n", errout.str());
}
// Check that base classes have virtual destructors
void checkVirtualDestructor(const char code[])
{