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

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


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


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


More information about the cfe-commits mailing list