aboutsummaryrefslogtreecommitdiff
path: root/clang-tools-extra/test/clang-tidy/checkers/modernize/pass-by-value.cpp
blob: cc237f4efcd96cc44a5fe8b7ed155ccbf4c9e279 (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// RUN: %check_clang_tidy %s modernize-pass-by-value %t -- -- -fno-delayed-template-parsing

namespace {
// POD types are trivially move constructible.
struct POD {
  int a, b, c;
};

struct Movable {
  int a, b, c;
  Movable() = default;
  Movable(const Movable &) {}
  Movable(Movable &&) {}
};

struct NotMovable {
  NotMovable() = default;
  NotMovable(const NotMovable &) = default;
  NotMovable(NotMovable &&) = delete;
  int a, b, c;
};
}

struct A {
  A(const Movable &M) : M(M) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: pass by value and use std::move [modernize-pass-by-value]
  // CHECK-FIXES: A(Movable M) : M(std::move(M)) {}
  Movable M;
};

// Test that we aren't modifying other things than a parameter.
Movable GlobalObj;
struct B {
  B(const Movable &M) : M(GlobalObj) {}
  Movable M;
};

// Test that a parameter with more than one reference to it won't be changed.
struct C {
  // Tests extra-reference in body.
  C(const Movable &M) : M(M) { this->i = M.a; }

  // Tests extra-reference in init-list.
  C(const Movable &M, int) : M(M), i(M.a) {}
  Movable M;
  int i;
};

// Test that both declaration and definition are updated.
struct D {
  D(const Movable &M);
  // CHECK-FIXES: D(Movable M);
  Movable M;
};
D::D(const Movable &M) : M(M) {}
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: pass by value and use std::move
// CHECK-FIXES: D::D(Movable M) : M(std::move(M)) {}

// Test with default parameter.
struct E {
  E(const Movable &M = Movable()) : M(M) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: pass by value and use std::move
  // CHECK-FIXES: E(Movable M = Movable()) : M(std::move(M)) {}
  Movable M;
};

// Test with object that can't be moved.
struct F {
  F(const NotMovable &NM) : NM(NM) {}
  NotMovable NM;
};

// Test unnamed parameter in declaration.
struct G {
  G(const Movable &);
  // CHECK-FIXES: G(Movable );
  Movable M;
};
G::G(const Movable &M) : M(M) {}
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: pass by value and use std::move
// CHECK-FIXES: G::G(Movable M) : M(std::move(M)) {}

// Test parameter with and without qualifier.
namespace ns_H {
typedef ::Movable HMovable;
}
struct H {
  H(const ns_H::HMovable &M);
  // CHECK-FIXES: H(ns_H::HMovable M);
  ns_H::HMovable M;
};
using namespace ns_H;
H::H(const HMovable &M) : M(M) {}
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: pass by value and use std::move
// CHECK-FIXES: H::H(HMovable M) : M(std::move(M)) {}

// Try messing up with macros.
#define MOVABLE_PARAM(Name) const Movable & Name
// CHECK-FIXES: #define MOVABLE_PARAM(Name) const Movable & Name
struct I {
  I(MOVABLE_PARAM(M)) : M(M) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: pass by value and use std::move
  // CHECK-FIXES: I(MOVABLE_PARAM(M)) : M(M) {}
  Movable M;
};
#undef MOVABLE_PARAM

// Test that templates aren't modified.
template <typename T> struct J {
  J(const T &M) : M(M) {}
  T M;
};
J<Movable> j1(Movable());
J<NotMovable> j2(NotMovable());

template<class T>
struct MovableTemplateT
{
  MovableTemplateT() {}
  MovableTemplateT(const MovableTemplateT& o) { }
  MovableTemplateT(MovableTemplateT&& o) { }
};

template <class T>
struct J2 {
  J2(const MovableTemplateT<T>& A);
  MovableTemplateT<T> M;
};

template <class T>
J2<T>::J2(const MovableTemplateT<T>& A) : M(A) {}
J2<int> j3(MovableTemplateT<int>{});

struct K_Movable {
  K_Movable() = default;
  K_Movable(const K_Movable &) = default;
  K_Movable(K_Movable &&o) { dummy = o.dummy; }
  int dummy;
};

// Test with movable type with an user defined move constructor.
struct K {
  K(const K_Movable &M) : M(M) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: pass by value and use std::move
  // CHECK-FIXES: K(K_Movable M) : M(std::move(M)) {}
  K_Movable M;
};

template <typename T> struct L {
  L(const Movable &M) : M(M) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: pass by value and use std::move
  // CHECK-FIXES: L(Movable M) : M(std::move(M)) {}
  Movable M;
};
L<int> l(Movable());

// Test with a non-instantiated template class.
template <typename T> struct N {
  N(const Movable &M) : M(M) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: pass by value and use std::move
  // CHECK-FIXES: N(Movable M) : M(std::move(M)) {}

  Movable M;
  T A;
};

// Test with value parameter.
struct O {
  O(Movable M) : M(M) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: pass by value and use std::move
  // CHECK-FIXES: O(Movable M) : M(std::move(M)) {}
  Movable M;
};

// Test with a const-value parameter.
struct P {
  P(const Movable M) : M(M) {}
  Movable M;
};

// Test with multiples parameters where some need to be changed and some don't.
// need to.
struct Q {
  Q(const Movable &A, const Movable &B, const Movable &C, double D)
      : A(A), B(B), C(C), D(D) {}
  // CHECK-MESSAGES: :[[@LINE-2]]:23: warning: pass by value and use std::move
  // CHECK-MESSAGES: :[[@LINE-3]]:41: warning: pass by value and use std::move
  // CHECK-FIXES:      Q(const Movable &A, Movable B, Movable C, double D)
  // CHECK-FIXES:     : A(A), B(std::move(B)), C(std::move(C)), D(D) {}
  const Movable &A;
  Movable B;
  Movable C;
  double D;
};

// Test that value-parameters with a nested name specifier are left as-is.
namespace ns_R {
typedef ::Movable RMovable;
}
struct R {
  R(ns_R::RMovable M) : M(M) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: pass by value and use std::move
  // CHECK-FIXES: R(ns_R::RMovable M) : M(std::move(M)) {}
  ns_R::RMovable M;
};

// Test with rvalue parameter.
struct S {
  S(Movable &&M) : M(M) {}
  Movable M;
};

template <typename T, int N> struct array { T A[N]; };

// Test that types that are trivially copyable will not use std::move. This will
// cause problems with performance-move-const-arg, as it will revert it.
struct T {
  T(array<int, 10> a) : a_(a) {}
  array<int, 10> a_;
};

struct U {
  U(const POD &M) : M(M) {}
  POD M;
};

// The rewrite can't look through `typedefs` and `using`.
// Test that we don't partially rewrite one decl without rewriting the other.
using MovableConstRef = const Movable &;
struct V {
  V(MovableConstRef M);
  // CHECK-FIXES: V(MovableConstRef M);
  Movable M;
};
V::V(const Movable &M) : M(M) {}
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: pass by value and use std::move
// CHECK-FIXES: V::V(const Movable &M) : M(M) {}

// Test with paired lvalue/rvalue overloads.
struct W1 {
  W1(const Movable &M) : M(M) {}
  W1(Movable &&M);
  Movable M;
};
struct W2 {
  W2(const Movable &M, int) : M(M) {}
  W2(Movable &&M, int);
  Movable M;
};
struct W3 {
  W3(const W1 &, const Movable &M) : M(M) {}
  W3(W1 &&, Movable &&M);
  Movable M;
};

struct ProtectedMovable {
  ProtectedMovable() = default;
  ProtectedMovable(const ProtectedMovable &) {}
protected:
  ProtectedMovable(ProtectedMovable &&) {}
};

struct PrivateMovable {
  PrivateMovable() = default;
  PrivateMovable(const PrivateMovable &) {}
private:
  PrivateMovable(PrivateMovable &&) {}

  friend struct X5;
};

struct InheritedProtectedMovable : ProtectedMovable {
  InheritedProtectedMovable() = default;
  InheritedProtectedMovable(const InheritedProtectedMovable &) {}
  InheritedProtectedMovable(InheritedProtectedMovable &&) {}
};

struct InheritedPrivateMovable : PrivateMovable {
  InheritedPrivateMovable() = default;
  InheritedPrivateMovable(const InheritedPrivateMovable &) {}
  InheritedPrivateMovable(InheritedPrivateMovable &&) {}
};

struct X1 {
  X1(const ProtectedMovable &M) : M(M) {}
  ProtectedMovable M;
};

struct X2 {
  X2(const PrivateMovable &M) : M(M) {}
  PrivateMovable M;
};

struct X3 {
  X3(const InheritedProtectedMovable &M) : M(M) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: pass by value and use std::move
  // CHECK-FIXES: X3(InheritedProtectedMovable M) : M(std::move(M)) {}
  InheritedProtectedMovable M;
};

struct X4 {
  X4(const InheritedPrivateMovable &M) : M(M) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: pass by value and use std::move
  // CHECK-FIXES: X4(InheritedPrivateMovable M) : M(std::move(M)) {}
  InheritedPrivateMovable M;
};

struct X5 {
  X5(const PrivateMovable &M) : M(M) {}
  // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: pass by value and use std::move
  // CHECK-FIXES: X5(PrivateMovable M) : M(std::move(M)) {}
  PrivateMovable M;
};