forked from OSchip/llvm-project
[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:
parent
7cbf710396
commit
fa6aef4427
|
@ -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))
|
||||
|
|
|
@ -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'
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue