This revision was automatically updated to reflect the committed changes. Closed by commit rGfa6aef442772: [clang-tidy] Added a case to UnconventionalAssignOperatorCheck. (authored by balazske).
Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D74529/new/ https://reviews.llvm.org/D74529 Files: clang-tools-extra/clang-tidy/misc/UnconventionalAssignOperatorCheck.cpp clang-tools-extra/test/clang-tidy/checkers/misc-unconventional-assign-operator.cpp Index: clang-tools-extra/test/clang-tidy/checkers/misc-unconventional-assign-operator.cpp =================================================================== --- clang-tools-extra/test/clang-tidy/checkers/misc-unconventional-assign-operator.cpp +++ clang-tools-extra/test/clang-tidy/checkers/misc-unconventional-assign-operator.cpp @@ -109,3 +109,21 @@ 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' + } +}; Index: clang-tools-extra/clang-tidy/misc/UnconventionalAssignOperatorCheck.cpp =================================================================== --- clang-tools-extra/clang-tidy/misc/UnconventionalAssignOperatorCheck.cpp +++ clang-tools-extra/clang-tidy/misc/UnconventionalAssignOperatorCheck.cpp @@ -60,7 +60,12 @@ 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))
Index: clang-tools-extra/test/clang-tidy/checkers/misc-unconventional-assign-operator.cpp =================================================================== --- clang-tools-extra/test/clang-tidy/checkers/misc-unconventional-assign-operator.cpp +++ clang-tools-extra/test/clang-tidy/checkers/misc-unconventional-assign-operator.cpp @@ -109,3 +109,21 @@ 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' + } +}; Index: clang-tools-extra/clang-tidy/misc/UnconventionalAssignOperatorCheck.cpp =================================================================== --- clang-tools-extra/clang-tidy/misc/UnconventionalAssignOperatorCheck.cpp +++ clang-tools-extra/clang-tidy/misc/UnconventionalAssignOperatorCheck.cpp @@ -60,7 +60,12 @@ 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))
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits