diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 04bfdf591..37eee7fbc 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -878,8 +878,8 @@ void CheckClass::operatorEqRetRefThis() if (tok1 && tok1->next() && tok1->next()->str() == "{") { - const Token *last = tok1->next()->link()->tokAt(-2); - for (tok1 = tok1->tokAt(2); tok1 != last; tok1 = tok1->next()) + 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") @@ -930,8 +930,8 @@ void CheckClass::operatorEqRetRefThis() if (tok1 && tok1->next() && tok1->next()->str() == "{") { - const Token *last = tok1->next()->link()->tokAt(-2); - for (tok1 = tok1->tokAt(2); tok1 != last; tok1 = tok1->next()) + 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") diff --git a/test/testclass.cpp b/test/testclass.cpp index d3b0167ba..58551c2d7 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -68,7 +68,8 @@ private: TEST_CASE(noConstructor4); TEST_CASE(operatorEq1); - TEST_CASE(operatorEqRetRefThis); + TEST_CASE(operatorEqRetRefThis1); + TEST_CASE(operatorEqRetRefThis2); // ticket #1323 TEST_CASE(operatorEqToSelf1); // single class TEST_CASE(operatorEqToSelf2); // nested class TEST_CASE(operatorEqToSelf3); // multiple inheritance @@ -175,7 +176,7 @@ private: checkClass.operatorEqRetRefThis(); } - void operatorEqRetRefThis() + void operatorEqRetRefThis1() { checkOpertorEqRetRefThis( "class A\n" @@ -280,6 +281,17 @@ private: ASSERT_EQUALS("[test.cpp:10]: (style) 'operator=' should return reference to self\n", errout.str()); } + void operatorEqRetRefThis2() + { + // ticket # 1323 + checkOpertorEqRetRefThis( + "class szp\n" + "{\n" + " szp &operator =(int *other) {};\n" + "};"); + ASSERT_EQUALS("", errout.str()); + } + // Check that operator Equal checks for assignment to self void checkOpertorEqToSelf(const char code[]) {