[clang-tools-extra] fa6aef4 - [clang-tidy] Added a case to UnconventionalAssignOperatorCheck.

Balázs Kéri via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 19 01:08:17 PST 2020


Author: Balázs Kéri
Date: 2020-02-19T10:07:34+01:00
New Revision: fa6aef44277230adecb541c78a71978172accd5c

URL: https://github.com/llvm/llvm-project/commit/fa6aef44277230adecb541c78a71978172accd5c
DIFF: https://github.com/llvm/llvm-project/commit/fa6aef44277230adecb541c78a71978172accd5c.diff

LOG: [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

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/misc/UnconventionalAssignOperatorCheck.cpp
    clang-tools-extra/test/clang-tidy/checkers/misc-unconventional-assign-operator.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/misc/UnconventionalAssignOperatorCheck.cpp b/clang-tools-extra/clang-tidy/misc/UnconventionalAssignOperatorCheck.cpp
index 8c87dae90080..811b55bd12be 100644
--- a/clang-tools-extra/clang-tidy/misc/UnconventionalAssignOperatorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/UnconventionalAssignOperatorCheck.cpp
@@ -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))

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/misc-unconventional-assign-operator.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc-unconventional-assign-operator.cpp
index a0a37c0ff1c7..4f0713a01f6f 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/misc-unconventional-assign-operator.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/misc-unconventional-assign-operator.cpp
@@ -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'
+  }
+};


        


More information about the cfe-commits mailing list