From 29e5992e51ecf1ddba469c73a0eed0b28b131de5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 20 May 2019 21:30:20 +0200 Subject: [PATCH] Fixed #9045 (FP operatorEqRetRefThis - recent regression) --- lib/checkclass.cpp | 10 ++++++++-- test/testclass.cpp | 12 ++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) 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() {