fix #2592 (False positive: 'operator=' should return reference to self)

This commit is contained in:
Robert Reif 2011-02-19 20:02:16 -05:00
parent d20987c3da
commit fef1142997
2 changed files with 24 additions and 1 deletions

View File

@ -872,7 +872,9 @@ void CheckClass::checkReturnPtrThis(const Scope *scope, const Function *func, co
// check of *this is returned
else if (!(Token::Match(tok->tokAt(1), "(| * this ;|=") ||
Token::Match(tok->tokAt(1), "(| * this +=") ||
Token::simpleMatch(tok->tokAt(1), "operator= (")))
Token::simpleMatch(tok->tokAt(1), "operator= (") ||
(Token::Match(tok->tokAt(1), "%type% :: operator= (") &&
tok->next()->str() == scope->className)))
operatorEqRetRefThisError(func->token);
}
}

View File

@ -113,6 +113,7 @@ private:
TEST_CASE(operatorEqToSelf6); // ticket # 1550
TEST_CASE(operatorEqToSelf7);
TEST_CASE(operatorEqToSelf8); // ticket #2179
TEST_CASE(operatorEqToSelf9); // ticket #2592
TEST_CASE(memsetOnStruct);
TEST_CASE(memsetVector);
TEST_CASE(memsetOnClass);
@ -1299,6 +1300,26 @@ private:
ASSERT_EQUALS("", errout.str());
}
void operatorEqToSelf9()
{
checkOpertorEqToSelf(
"class Foo\n"
"{\n"
"public:\n"
" Foo& operator=(Foo* pOther);\n"
" Foo& operator=(Foo& other);\n"
"};\n"
"Foo& Foo::operator=(Foo* pOther)\n"
"{\n"
" return *this;\n"
"}\n"
"Foo& Foo::operator=(Foo& other)\n"
"{\n"
" return Foo::operator=(&other);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
// Check that base classes have virtual destructors
void checkVirtualDestructor(const char code[])
{