[clang-tools-extra] d867f66 - [clang-tidy] modernize-avoid-bind: Fix handling of operators
Piotr Zegar via cfe-commits
cfe-commits at lists.llvm.org
Sat Nov 11 02:00:38 PST 2023
Author: Joachim Priesner
Date: 2023-11-11T09:56:33Z
New Revision: d867f668672d634d52eaeae4cdcb7f9740890082
URL: https://github.com/llvm/llvm-project/commit/d867f668672d634d52eaeae4cdcb7f9740890082
DIFF: https://github.com/llvm/llvm-project/commit/d867f668672d634d52eaeae4cdcb7f9740890082.diff
LOG: [clang-tidy] modernize-avoid-bind: Fix handling of operators
When operators are used explicitly with std::bind
invalid fixes were generated, this change fixes it.
Reviewed By: PiotrZSL
Differential Revision: https://reviews.llvm.org/D125949
Added:
Modified:
clang-tools-extra/clang-tidy/modernize/AvoidBindCheck.cpp
clang-tools-extra/test/clang-tidy/checkers/modernize/avoid-bind.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/modernize/AvoidBindCheck.cpp b/clang-tools-extra/clang-tidy/modernize/AvoidBindCheck.cpp
index 2a54eaddccb15cf..51ee35da623f202 100644
--- a/clang-tools-extra/clang-tidy/modernize/AvoidBindCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/AvoidBindCheck.cpp
@@ -691,12 +691,15 @@ void AvoidBindCheck::check(const MatchFinder::MatchResult &Result) {
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:
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/avoid-bind.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/avoid-bind.cpp
index 336b3cb4ee08f94..22b24d45fe63f76 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/avoid-bind.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/avoid-bind.cpp
@@ -43,6 +43,7 @@ struct Foo {
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 @@ void testCapturedSubexpressions() {
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 @@ struct E {
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); }
}
};
More information about the cfe-commits
mailing list