File: modernize-avoid-bind.cpp

package info (click to toggle)
llvm-toolchain-3.9 1%3A3.9.1-8
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 441,060 kB
  • ctags: 428,777
  • sloc: cpp: 2,546,577; ansic: 538,318; asm: 119,677; objc: 103,316; python: 102,148; sh: 27,847; pascal: 5,626; ml: 5,510; perl: 5,293; lisp: 4,801; makefile: 2,177; xml: 686; cs: 362; php: 212; csh: 117
file content (70 lines) | stat: -rw-r--r-- 1,965 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// RUN: %check_clang_tidy %s modernize-avoid-bind %t -- -- -std=c++14

namespace std {
inline namespace impl {
template <class Fp, class... Arguments>
class bind_rt {};

template <class Fp, class... Arguments>
bind_rt<Fp, Arguments...> bind(Fp &&, Arguments &&...);
}
}

int add(int x, int y) { return x + y; }

void f() {
  auto clj = std::bind(add, 2, 2);
  // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: prefer a lambda to std::bind [modernize-avoid-bind]
  // CHECK-FIXES: auto clj = [] { return add(2, 2); };
}

void g() {
  int x = 2;
  int y = 2;
  auto clj = std::bind(add, x, y);
  // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: prefer a lambda to std::bind
  // CHECK-FIXES: auto clj = [=] { return add(x, y); };
}

struct placeholder {};
placeholder _1;
placeholder _2;

void h() {
  int x = 2;
  auto clj = std::bind(add, x, _1);
  // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: prefer a lambda to std::bind
  // CHECK-FIXES: auto clj = [=](auto && arg1) { return add(x, arg1); };
}

struct A;
struct B;
bool ABTest(const A &, const B &);

void i() {
  auto BATest = std::bind(ABTest, _2, _1);
  // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: prefer a lambda to std::bind
  // CHECK-FIXES: auto BATest = [](auto && arg1, auto && arg2) { return ABTest(arg2, arg1); };
}

void j() {
  auto clj = std::bind(add, 2, 2, 2);
  // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: prefer a lambda to std::bind
  // No fix is applied for argument mismatches.
  // CHECK-FIXES: auto clj = std::bind(add, 2, 2, 2);
}

void k() {
  auto clj = std::bind(add, _1, _1);
  // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: prefer a lambda to std::bind
  // No fix is applied for reused placeholders.
  // CHECK-FIXES: auto clj = std::bind(add, _1, _1);
}

void m() {
  auto clj = std::bind(add, 1, add(2, 5));
  // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: prefer a lambda to std::bind
  // No fix is applied for nested calls.
  // CHECK-FIXES: auto clj = std::bind(add, 1, add(2, 5));
}