diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 02916d682..109bd314b 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -948,12 +948,14 @@ void CheckClass::operatorEqRetRefThis() if (tok1 && tok1->next() && tok1->next()->str() == "{") { + bool foundReturn = false; const Token *last = tok1->next()->link(); for (tok1 = tok1->tokAt(2); tok1 && tok1 != last; tok1 = tok1->next()) { // check for return of reference to this if (tok1->str() == "return") { + foundReturn = true; std::string cast("( " + name->str() + " & )"); if (Token::Match(tok1->next(), cast.c_str())) tok1 = tok1->tokAt(4); @@ -964,6 +966,8 @@ void CheckClass::operatorEqRetRefThis() operatorEqRetRefThisError(tok); } } + if (!foundReturn) + operatorEqRetRefThisError(tok); } } } @@ -1005,12 +1009,14 @@ void CheckClass::operatorEqRetRefThis() if (tok1 && tok1->next() && tok1->next()->str() == "{") { + bool foundReturn = false; const Token *last = tok1->next()->link(); for (tok1 = tok1->tokAt(2); tok1 && tok1 != last; tok1 = tok1->next()) { // check for return of reference to this if (tok1->str() == "return") { + foundReturn = true; std::string cast("( " + name->str() + " & )"); if (Token::Match(tok1->next(), cast.c_str())) tok1 = tok1->tokAt(4); @@ -1021,6 +1027,8 @@ void CheckClass::operatorEqRetRefThis() operatorEqRetRefThisError(tok); } } + if (!foundReturn) + operatorEqRetRefThisError(tok); } } } diff --git a/test/testclass.cpp b/test/testclass.cpp index d418ff406..a8ca70792 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -79,6 +79,7 @@ private: TEST_CASE(operatorEqRetRefThis2); // ticket #1323 TEST_CASE(operatorEqRetRefThis3); // ticket #1405 TEST_CASE(operatorEqRetRefThis4); // ticket #1451 + TEST_CASE(operatorEqRetRefThis5); // ticket #1550 TEST_CASE(operatorEqToSelf1); // single class TEST_CASE(operatorEqToSelf2); // nested class TEST_CASE(operatorEqToSelf3); // multiple inheritance @@ -315,7 +316,7 @@ private: "{\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() @@ -341,6 +342,17 @@ private: 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 void checkOpertorEqToSelf(const char code[]) {