diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 6ed701737..f30942e04 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -1416,6 +1416,13 @@ void CheckClass::checkReturnPtrThis(const Scope *scope, const Function *func, co continue; foundReturn = true; + + const Token *retExpr = tok->astOperand1(); + if (retExpr && retExpr->str() == "=") + retExpr = retExpr->astOperand1(); + if (retExpr && retExpr->isUnaryOp("*") && Token::simpleMatch(retExpr->astOperand1(), "this")) + continue; + std::string cast("( " + scope->className + " & )"); if (Token::simpleMatch(tok->next(), cast.c_str())) tok = tok->tokAt(4); @@ -1450,8 +1457,7 @@ void CheckClass::checkReturnPtrThis(const Scope *scope, const Function *func, co } // check if *this is returned - else if (!(Token::Match(tok->next(), "(| * this ;|=") || - Token::simpleMatch(tok->next(), "operator= (") || + else if (!(Token::simpleMatch(tok->next(), "operator= (") || Token::simpleMatch(tok->next(), "this . operator= (") || (Token::Match(tok->next(), "%type% :: operator= (") && tok->next()->str() == scope->className))) diff --git a/test/testclass.cpp b/test/testclass.cpp index 65e0c4fb0..ed9109627 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -1479,6 +1479,18 @@ private: "A &A::operator =(int *other) { return (*this); };\n" "A &A::operator =(long *other) { return this->operator = (*(int *)other); };"); ASSERT_EQUALS("", errout.str()); + + checkOpertorEqRetRefThis( // #9045 + "class V {\n" + "public:\n" + " V& operator=(const V& r) {\n" + " if (this == &r) {\n" + " return ( *this );\n" + " }\n" + " return *this;\n" + " }\n" + "};\n"); + ASSERT_EQUALS("", errout.str()); } void operatorEqRetRefThis4() {