Fixed #9045 (FP operatorEqRetRefThis - recent regression)

This commit is contained in:
Daniel Marjamäki 2019-05-20 21:30:20 +02:00
parent 592ff56b90
commit 29e5992e51
2 changed files with 20 additions and 2 deletions

View File

@ -1416,6 +1416,13 @@ void CheckClass::checkReturnPtrThis(const Scope *scope, const Function *func, co
continue; continue;
foundReturn = true; 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 + " & )"); std::string cast("( " + scope->className + " & )");
if (Token::simpleMatch(tok->next(), cast.c_str())) if (Token::simpleMatch(tok->next(), cast.c_str()))
tok = tok->tokAt(4); tok = tok->tokAt(4);
@ -1450,8 +1457,7 @@ void CheckClass::checkReturnPtrThis(const Scope *scope, const Function *func, co
} }
// check if *this is returned // check if *this is returned
else if (!(Token::Match(tok->next(), "(| * this ;|=") || else if (!(Token::simpleMatch(tok->next(), "operator= (") ||
Token::simpleMatch(tok->next(), "operator= (") ||
Token::simpleMatch(tok->next(), "this . operator= (") || Token::simpleMatch(tok->next(), "this . operator= (") ||
(Token::Match(tok->next(), "%type% :: operator= (") && (Token::Match(tok->next(), "%type% :: operator= (") &&
tok->next()->str() == scope->className))) tok->next()->str() == scope->className)))

View File

@ -1479,6 +1479,18 @@ private:
"A &A::operator =(int *other) { return (*this); };\n" "A &A::operator =(int *other) { return (*this); };\n"
"A &A::operator =(long *other) { return this->operator = (*(int *)other); };"); "A &A::operator =(long *other) { return this->operator = (*(int *)other); };");
ASSERT_EQUALS("", errout.str()); 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() { void operatorEqRetRefThis4() {