From 64f74bf72eb484aa32e1104050cb54745116decf Mon Sep 17 00:00:00 2001 From: Zachary Turner Date: Wed, 20 Nov 2019 11:27:14 -0800 Subject: [clang-tidy] Rewrite modernize-avoid-bind check. This represents largely a full re-write of modernize-avoid-bind, adding significant new functionality in the process. In particular: * Both boost::bind and std::bind are now supported * Function objects are supported in addition to functions * Member functions are supported * Nested calls are supported using capture-init syntax * std::ref() and boost::ref() are now recognized, and will capture by reference. * Rather than capturing with a global =, we now build up an individual capture list that is both necessary and sufficient for the call. * Fixits are supported in a much larger variety of scenarios than before. All previous tests pass under the re-write, but a large number of new tests have been added as well. Differential Revision: https://reviews.llvm.org/D70368 --- ...ernize-avoid-bind-permissive-parameter-list.cpp | 58 ++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 clang-tools-extra/test/clang-tidy/checkers/modernize-avoid-bind-permissive-parameter-list.cpp (limited to 'clang-tools-extra/test/clang-tidy/checkers/modernize-avoid-bind-permissive-parameter-list.cpp') diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize-avoid-bind-permissive-parameter-list.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize-avoid-bind-permissive-parameter-list.cpp new file mode 100644 index 00000000000..6c81a6e9ab9 --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize-avoid-bind-permissive-parameter-list.cpp @@ -0,0 +1,58 @@ +// RUN: %check_clang_tidy -std=c++14-or-later %s modernize-avoid-bind %t -- \ +// RUN: -config="{CheckOptions: [ \ +// RUN: {key: modernize-avoid-bind.PermissiveParameterList, value: 1}]}" -- + +namespace std { +inline namespace impl { +template +class bind_rt {}; + +template +bind_rt bind(Fp &&, Arguments &&...); +} // namespace impl + +template +T ref(T &t); +} // namespace std + +int add(int x, int y) { return x + y; } + +// Let's fake a minimal std::function-like facility. +namespace std { +template +_Tp declval(); + +template +struct __res { + template + static decltype(declval<_Functor>()(_Args()...)) _S_test(int); + + template + static void _S_test(...); + + using type = decltype(_S_test<_ArgTypes...>(0)); +}; + +template +struct function; + +template +struct function { + template ::type> + function(_Functor) {} +}; +} // namespace std + +struct placeholder {}; +placeholder _1; + +void testLiteralParameters() { + auto AAA = std::bind(add, 2, 2); + // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: prefer a lambda to std::bind [modernize-avoid-bind] + // CHECK-FIXES: auto AAA = [](auto && ...) { return add(2, 2); }; + + auto BBB = std::bind(add, _1, 2); + // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: prefer a lambda to std::bind [modernize-avoid-bind] + // CHECK-FIXES: auto BBB = [](auto && PH1, auto && ...) { return add(PH1, 2); }; +} -- cgit v1.2.3