Fixed #1451 (false positive: operator = should return reference to self)

This commit is contained in:
Robert Reif 2010-02-25 07:26:59 +01:00 committed by Daniel Marjamäki
parent f9d444ed52
commit 0da133f8a0
2 changed files with 22 additions and 0 deletions

View File

@ -924,7 +924,12 @@ void CheckClass::operatorEqRetRefThis()
// check for return of reference to this
if (tok1->str() == "return")
{
std::string cast("( " + name->str() + " & )");
if (Token::Match(tok1->next(), cast.c_str()))
tok1 = tok1->tokAt(4);
if (!(Token::Match(tok1->tokAt(1), "(| * this ;|=") ||
Token::Match(tok1->tokAt(1), "(| * this +=") ||
Token::Match(tok1->tokAt(1), "operator = (")))
operatorEqRetRefThisError(tok);
}
@ -976,7 +981,12 @@ void CheckClass::operatorEqRetRefThis()
// check for return of reference to this
if (tok1->str() == "return")
{
std::string cast("( " + name->str() + " & )");
if (Token::Match(tok1->next(), cast.c_str()))
tok1 = tok1->tokAt(4);
if (!(Token::Match(tok1->tokAt(1), "(| * this ;|=") ||
Token::Match(tok1->tokAt(1), "(| * this +=") ||
Token::Match(tok1->tokAt(1), "operator = (")))
operatorEqRetRefThisError(tok);
}

View File

@ -72,6 +72,7 @@ private:
TEST_CASE(operatorEqRetRefThis1);
TEST_CASE(operatorEqRetRefThis2); // ticket #1323
TEST_CASE(operatorEqRetRefThis3); // ticket #1405
TEST_CASE(operatorEqRetRefThis4); // ticket #1451
TEST_CASE(operatorEqToSelf1); // single class
TEST_CASE(operatorEqToSelf2); // nested class
TEST_CASE(operatorEqToSelf3); // multiple inheritance
@ -310,6 +311,17 @@ private:
ASSERT_EQUALS("", errout.str());
}
void operatorEqRetRefThis4()
{
// ticket # 1451
checkOpertorEqRetRefThis(
"P& P::operator = (const P& pc)\n"
"{\n"
" return (P&)(*this += pc);\n"
"}");
ASSERT_EQUALS("", errout.str());
}
// Check that operator Equal checks for assignment to self
void checkOpertorEqToSelf(const char code[])
{