[clang-tools-extra] r284719 - [Clang-tidy]: Fix modernize-avoid-bind erroneous scope resolution.
Haojian Wu via cfe-commits
cfe-commits at lists.llvm.org
Thu Oct 20 04:32:48 PDT 2016
Author: hokein
Date: Thu Oct 20 06:32:47 2016
New Revision: 284719
URL: http://llvm.org/viewvc/llvm-project?rev=284719&view=rev
Log:
[Clang-tidy]: Fix modernize-avoid-bind erroneous scope resolution.
Hello, i would like to suggest a fix for one of the checks in clang-tidy and i should hope this one is the correct mailing list.
The check is modernize-avoid-bind.
Consider the following:
void bar(int x, int y);
namespace N {
void bar(int x, int y);
}
void foo(){
auto Test = std::bind(N::bar,1,1);
}
clang-tidy’s modernize-avoid-bind check suggests writing:
void foo(){
auto Test =[] {return bar(1,1);};
}
instead of:
void foo(){
auto Test = [] {return N::bar(1,1);};
}
So clang-tidy has proposed an incorrect Fix.
Patch by IdrissRio!
Reviewers: alexfh, hokein, aaron.ballman
Subscriber: cfe-commits
Modified:
clang-tools-extra/trunk/clang-tidy/modernize/AvoidBindCheck.cpp
clang-tools-extra/trunk/test/clang-tidy/modernize-avoid-bind.cpp
Modified: clang-tools-extra/trunk/clang-tidy/modernize/AvoidBindCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/modernize/AvoidBindCheck.cpp?rev=284719&r1=284718&r2=284719&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/modernize/AvoidBindCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/modernize/AvoidBindCheck.cpp Thu Oct 20 06:32:47 2016
@@ -108,8 +108,9 @@ void AvoidBindCheck::registerMatchers(Ma
return;
Finder->addMatcher(
- callExpr(callee(namedDecl(hasName("::std::bind"))),
- hasArgument(0, declRefExpr(to(functionDecl().bind("f")))))
+ callExpr(
+ callee(namedDecl(hasName("::std::bind"))),
+ hasArgument(0, declRefExpr(to(functionDecl().bind("f"))).bind("ref")))
.bind("bind"),
this);
}
@@ -148,14 +149,17 @@ void AvoidBindCheck::check(const MatchFi
bool HasCapturedArgument = llvm::any_of(
Args, [](const BindArgument &B) { return B.Kind == BK_Other; });
-
+ const auto *Ref = Result.Nodes.getNodeAs<DeclRefExpr>("ref");
Stream << "[" << (HasCapturedArgument ? "=" : "") << "]";
addPlaceholderArgs(Args, Stream);
- Stream << " { return " << F->getName() << "(";
+ Stream << " { return ";
+ Ref->printPretty(Stream, nullptr, Result.Context->getPrintingPolicy());
+ Stream<< "(";
addFunctionCallArgs(Args, Stream);
Stream << "); };";
- Diag << FixItHint::CreateReplacement(MatchedDecl->getSourceRange(), Stream.str());
+ Diag << FixItHint::CreateReplacement(MatchedDecl->getSourceRange(),
+ Stream.str());
}
} // namespace modernize
Modified: clang-tools-extra/trunk/test/clang-tidy/modernize-avoid-bind.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/modernize-avoid-bind.cpp?rev=284719&r1=284718&r2=284719&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/modernize-avoid-bind.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/modernize-avoid-bind.cpp Thu Oct 20 06:32:47 2016
@@ -68,3 +68,12 @@ void m() {
// CHECK-FIXES: auto clj = std::bind(add, 1, add(2, 5));
}
+namespace C {
+ int add(int x, int y){ return x + y; }
+}
+
+void n() {
+ auto clj = std::bind(C::add, 1, 1);
+ // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: prefer a lambda to std::bind
+ // CHECK-FIXES: auto clj = [] { return C::add(1, 1); };
+}
More information about the cfe-commits
mailing list