[clang-tidy] Added a case to UnconventionalAssignOperatorCheck.

Summary:
The check accepts now a `return (*this = something);` as return
statement too (beneath of `*this`).

Reviewers: alexfh, hokein, aaron.ballman, JonasToth

Reviewed By: aaron.ballman

Subscribers: xazax.hun, dkrupp, Szelethus, gamesh411, cfe-commits

Tags: #clang, #clang-tools-extra

Differential Revision: https://reviews.llvm.org/D74529
This commit is contained in:
Balázs Kéri 2020-02-19 10:06:58 +01:00
parent 7cbf710396
commit fa6aef4427
2 changed files with 24 additions and 1 deletions

View File

@ -60,7 +60,12 @@ void UnconventionalAssignOperatorCheck::registerMatchers(
anyOf(unaryOperator(hasOperatorName("*"), hasUnaryOperand(cxxThisExpr())),
cxxOperatorCallExpr(argumentCountIs(1),
callee(unresolvedLookupExpr()),
hasArgument(0, cxxThisExpr())))))));
hasArgument(0, cxxThisExpr())),
cxxOperatorCallExpr(
hasOverloadedOperatorName("="),
hasArgument(
0, unaryOperator(hasOperatorName("*"),
hasUnaryOperand(cxxThisExpr())))))))));
const auto IsGoodAssign = cxxMethodDecl(IsAssign, HasGoodReturnType);
Finder->addMatcher(returnStmt(IsBadReturnStatement, forFunction(IsGoodAssign))

View File

@ -109,3 +109,21 @@ struct Template {
Template<int> TemplateInt;
}
struct AssignmentCallAtReturn {
AssignmentCallAtReturn &returnThis() {
return *this;
}
AssignmentCallAtReturn &operator=(int rhs) {
return *this;
}
AssignmentCallAtReturn &operator=(char rhs) {
// Allow call to assignment from other type.
return (*this = static_cast<int>(rhs));
}
AssignmentCallAtReturn &operator=(float rhs) {
// Do not allow calls to other functions.
return returnThis();
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: operator=() should always return '*this'
}
};