[PATCH] D74529: [clang-tidy] Added a case to UnconventionalAssignOperatorCheck.

Balázs Kéri via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 13 00:50:44 PST 2020


balazske created this revision.
Herald added subscribers: cfe-commits, gamesh411, Szelethus, dkrupp, xazax.hun.
Herald added a project: clang.

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


Repository:
  rG LLVM Github Monorepo

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))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D74529.244350.patch
Type: text/x-patch
Size: 1991 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200213/562cefd1/attachment.bin>


More information about the cfe-commits mailing list