Fixed #1550 (Improve check: Assignment to self)

This commit is contained in:
Robert Reif 2010-04-01 16:40:12 +02:00 committed by Daniel Marjamäki
parent 3dbd719bee
commit a8c9aa1f8d
2 changed files with 21 additions and 1 deletions

View File

@ -948,12 +948,14 @@ void CheckClass::operatorEqRetRefThis()
if (tok1 && tok1->next() && tok1->next()->str() == "{") if (tok1 && tok1->next() && tok1->next()->str() == "{")
{ {
bool foundReturn = false;
const Token *last = tok1->next()->link(); const Token *last = tok1->next()->link();
for (tok1 = tok1->tokAt(2); tok1 && tok1 != last; tok1 = tok1->next()) for (tok1 = tok1->tokAt(2); tok1 && tok1 != last; tok1 = tok1->next())
{ {
// check for return of reference to this // check for return of reference to this
if (tok1->str() == "return") if (tok1->str() == "return")
{ {
foundReturn = true;
std::string cast("( " + name->str() + " & )"); std::string cast("( " + name->str() + " & )");
if (Token::Match(tok1->next(), cast.c_str())) if (Token::Match(tok1->next(), cast.c_str()))
tok1 = tok1->tokAt(4); tok1 = tok1->tokAt(4);
@ -964,6 +966,8 @@ void CheckClass::operatorEqRetRefThis()
operatorEqRetRefThisError(tok); operatorEqRetRefThisError(tok);
} }
} }
if (!foundReturn)
operatorEqRetRefThisError(tok);
} }
} }
} }
@ -1005,12 +1009,14 @@ void CheckClass::operatorEqRetRefThis()
if (tok1 && tok1->next() && tok1->next()->str() == "{") if (tok1 && tok1->next() && tok1->next()->str() == "{")
{ {
bool foundReturn = false;
const Token *last = tok1->next()->link(); const Token *last = tok1->next()->link();
for (tok1 = tok1->tokAt(2); tok1 && tok1 != last; tok1 = tok1->next()) for (tok1 = tok1->tokAt(2); tok1 && tok1 != last; tok1 = tok1->next())
{ {
// check for return of reference to this // check for return of reference to this
if (tok1->str() == "return") if (tok1->str() == "return")
{ {
foundReturn = true;
std::string cast("( " + name->str() + " & )"); std::string cast("( " + name->str() + " & )");
if (Token::Match(tok1->next(), cast.c_str())) if (Token::Match(tok1->next(), cast.c_str()))
tok1 = tok1->tokAt(4); tok1 = tok1->tokAt(4);
@ -1021,6 +1027,8 @@ void CheckClass::operatorEqRetRefThis()
operatorEqRetRefThisError(tok); operatorEqRetRefThisError(tok);
} }
} }
if (!foundReturn)
operatorEqRetRefThisError(tok);
} }
} }
} }

View File

@ -79,6 +79,7 @@ private:
TEST_CASE(operatorEqRetRefThis2); // ticket #1323 TEST_CASE(operatorEqRetRefThis2); // ticket #1323
TEST_CASE(operatorEqRetRefThis3); // ticket #1405 TEST_CASE(operatorEqRetRefThis3); // ticket #1405
TEST_CASE(operatorEqRetRefThis4); // ticket #1451 TEST_CASE(operatorEqRetRefThis4); // ticket #1451
TEST_CASE(operatorEqRetRefThis5); // ticket #1550
TEST_CASE(operatorEqToSelf1); // single class TEST_CASE(operatorEqToSelf1); // single class
TEST_CASE(operatorEqToSelf2); // nested class TEST_CASE(operatorEqToSelf2); // nested class
TEST_CASE(operatorEqToSelf3); // multiple inheritance TEST_CASE(operatorEqToSelf3); // multiple inheritance
@ -315,7 +316,7 @@ private:
"{\n" "{\n"
" szp &operator =(int *other) {};\n" " szp &operator =(int *other) {};\n"
"};"); "};");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (style) 'operator=' should return reference to self\n", errout.str());
} }
void operatorEqRetRefThis3() void operatorEqRetRefThis3()
@ -341,6 +342,17 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void operatorEqRetRefThis5()
{
// ticket # 1550
checkOpertorEqRetRefThis(
"class A {\n"
"public:\n"
" A & operator=(const A &a) { }\n"
"};");
ASSERT_EQUALS("[test.cpp:3]: (style) 'operator=' should return reference to self\n", errout.str());
}
// Check that operator Equal checks for assignment to self // Check that operator Equal checks for assignment to self
void checkOpertorEqToSelf(const char code[]) void checkOpertorEqToSelf(const char code[])
{ {