[PATCH] D125949: [clang-tidy] modernize-avoid-bind: Fix crash when method name is not a simple identifier

Piotr Zegar via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Nov 11 02:00:42 PST 2023


This revision was automatically updated to reflect the committed changes.
Closed by commit rGd867f668672d: [clang-tidy] modernize-avoid-bind: Fix handling of operators (authored by jspam, committed by PiotrZSL).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D125949/new/

https://reviews.llvm.org/D125949

Files:
  clang-tools-extra/clang-tidy/modernize/AvoidBindCheck.cpp
  clang-tools-extra/test/clang-tidy/checkers/modernize/avoid-bind.cpp


Index: clang-tools-extra/test/clang-tidy/checkers/modernize/avoid-bind.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/modernize/avoid-bind.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/modernize/avoid-bind.cpp
@@ -43,6 +43,7 @@
 struct D {
   D() = default;
   void operator()(int x, int y) const {}
+  operator bool() const { return true; }
 
   void MemberFunction(int x) {}
   int MemberFunctionWithReturn(int x) {}
@@ -342,6 +343,7 @@
 struct E {
   void MemberFunction(int x) {}
   int MemberFunctionWithReturn(int x) {}
+  int operator()(int x, int y) const { return x + y; }
 
   void testMemberFunctions() {
     D *d;
@@ -379,6 +381,26 @@
     auto HHH = std::bind(&D::MemberFunctionWithReturn, _1, 1);
     // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: prefer a lambda to std::bind
     // CHECK-FIXES: auto HHH = [](auto && PH1) { return PH1->MemberFunctionWithReturn(1); };
+
+    auto III = std::bind(&D::operator(), d, 1, 2);
+    // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: prefer a lambda to std::bind
+    // CHECK-FIXES: auto III = [d] { (*d)(1, 2); }
+
+    auto JJJ = std::bind(&D::operator(), &dd, 1, 2);
+    // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: prefer a lambda to std::bind
+    // CHECK-FIXES: auto JJJ = [ObjectPtr = &dd] { (*ObjectPtr)(1, 2); }
+
+    auto KKK = std::bind(&D::operator(), _1, 1, 2);
+    // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: prefer a lambda to std::bind
+    // CHECK-FIXES: auto KKK = [](auto && PH1) { (*PH1)(1, 2); };
+
+    auto LLL = std::bind(&D::operator bool, d);
+    // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: prefer a lambda to std::bind
+    // CHECK-FIXES: auto LLL = [d] { return d->operator bool(); }
+
+    auto MMM = std::bind(&E::operator(), this, 1, 2);
+    // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: prefer a lambda to std::bind
+    // CHECK-FIXES: auto MMM = [this] { return (*this)(1, 2); }
   }
 };
 
Index: clang-tools-extra/clang-tidy/modernize/AvoidBindCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/modernize/AvoidBindCheck.cpp
+++ clang-tools-extra/clang-tidy/modernize/AvoidBindCheck.cpp
@@ -691,12 +691,15 @@
     const auto *MethodDecl = dyn_cast<CXXMethodDecl>(LP.Callable.Decl);
     const BindArgument &ObjPtr = FunctionCallArgs.front();
 
-    if (!isa<CXXThisExpr>(ignoreTemporariesAndPointers(ObjPtr.E))) {
-      Stream << ObjPtr.UsageIdentifier;
-      Stream << "->";
+    if (MethodDecl->getOverloadedOperator() == OO_Call) {
+      Stream << "(*" << ObjPtr.UsageIdentifier << ')';
+    } else {
+      if (!isa<CXXThisExpr>(ignoreTemporariesAndPointers(ObjPtr.E))) {
+        Stream << ObjPtr.UsageIdentifier;
+        Stream << "->";
+      }
+      Stream << MethodDecl->getNameAsString();
     }
-
-    Stream << MethodDecl->getName();
   } else {
     switch (LP.Callable.CE) {
     case CE_Var:


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D125949.558077.patch
Type: text/x-patch
Size: 2948 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20231111/ae9797de/attachment.bin>


More information about the cfe-commits mailing list