Fixed #1405 (false positive: operator = should return reference t

This commit is contained in:
Robert Reif 2010-02-17 22:46:03 +01:00 committed by Daniel Marjamäki
parent deb71fc566
commit 9e61e7dda8
2 changed files with 15 additions and 2 deletions

View File

@ -924,7 +924,7 @@ void CheckClass::operatorEqRetRefThis()
// check for return of reference to this
if (tok1->str() == "return")
{
if (!(Token::Match(tok1->tokAt(1), "* this ;") ||
if (!(Token::Match(tok1->tokAt(1), "(| * this ;|=") ||
Token::Match(tok1->tokAt(1), "operator = (")))
operatorEqRetRefThisError(tok);
}
@ -976,7 +976,7 @@ void CheckClass::operatorEqRetRefThis()
// check for return of reference to this
if (tok1->str() == "return")
{
if (!(Token::Match(tok1->tokAt(1), "* this ;") ||
if (!(Token::Match(tok1->tokAt(1), "(| * this ;|=") ||
Token::Match(tok1->tokAt(1), "operator = (")))
operatorEqRetRefThisError(tok);
}

View File

@ -71,6 +71,7 @@ private:
TEST_CASE(operatorEq1);
TEST_CASE(operatorEqRetRefThis1);
TEST_CASE(operatorEqRetRefThis2); // ticket #1323
TEST_CASE(operatorEqRetRefThis3); // ticket #1405
TEST_CASE(operatorEqToSelf1); // single class
TEST_CASE(operatorEqToSelf2); // nested class
TEST_CASE(operatorEqToSelf3); // multiple inheritance
@ -295,6 +296,18 @@ private:
ASSERT_EQUALS("", errout.str());
}
void operatorEqRetRefThis3()
{
// ticket # 1405
checkOpertorEqRetRefThis(
"class A {\n"
"public:\n"
" inline A &operator =(int *other) { return (*this;) };\n"
" inline A &operator =(long *other) { return (*this = 0;) };\n"
"};");
ASSERT_EQUALS("", errout.str());
}
// Check that operator Equal checks for assignment to self
void checkOpertorEqToSelf(const char code[])
{