1// RUN: %check_clang_tidy %s performance-move-const-arg %t
2
3namespace std {
4template <typename>
5struct remove_reference;
6
7template <typename _Tp>
8struct remove_reference {
9 typedef _Tp type;
10};
11
12template <typename _Tp>
13struct remove_reference<_Tp &> {
14 typedef _Tp type;
15};
16
17template <typename _Tp>
18struct remove_reference<_Tp &&> {
19 typedef _Tp type;
20};
21
22template <typename _Tp>
23constexpr typename std::remove_reference<_Tp>::type &&move(_Tp &&__t) {
24 return static_cast<typename std::remove_reference<_Tp>::type &&>(__t);
25}
26
27template <typename _Tp>
28constexpr _Tp &&
29forward(typename remove_reference<_Tp>::type &__t) noexcept {
30 return static_cast<_Tp &&>(__t);
31}
32
33} // namespace std
34
35class A {
36public:
37 A() {}
38 A(const A &rhs) {}
39 A(A &&rhs) {}
40};
41
42using AlsoA = A;
43
44struct TriviallyCopyable {
45 int i;
46};
47
48using TrivialAlias = TriviallyCopyable;
49
50void f(TriviallyCopyable) {}
51
52void g() {
53 TriviallyCopyable obj;
54 f(std::move(obj));
55 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: std::move of the variable 'obj' of the trivially-copyable type 'TriviallyCopyable' has no effect; remove std::move() [performance-move-const-arg]
56 // CHECK-FIXES: f(obj);
57
58 TrivialAlias obj2;
59 f(std::move(obj2));
60 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: std::move of the variable 'obj2' of the trivially-copyable type 'TrivialAlias' (aka 'TriviallyCopyable') has no effect; remove std::move() [performance-move-const-arg]
61 // CHECK-FIXES: f(obj2);
62}
63
64int f1() {
65 return std::move(42);
66 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: std::move of the expression of the trivially-copyable type 'int' has no effect; remove std::move() [performance-move-const-arg]
67 // CHECK-FIXES: return 42;
68}
69
70int f2(int x2) {
71 return std::move(x2);
72 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: std::move of the variable 'x2' of the trivially-copyable type 'int'
73 // CHECK-FIXES: return x2;
74}
75
76int *f3(int *x3) {
77 return std::move(x3);
78 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: std::move of the variable 'x3' of the trivially-copyable type 'int *'
79 // CHECK-FIXES: return x3;
80}
81
82A f4(A x4) { return std::move(x4); }
83
84AlsoA f4_a(AlsoA x4) { return std::move(x4); }
85
86A f5(const A x5) {
87 return std::move(x5);
88 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: std::move of the const variable 'x5' has no effect; remove std::move() or make the variable non-const [performance-move-const-arg]
89 // CHECK-FIXES: return x5;
90}
91
92AlsoA f5_a(const AlsoA x5) {
93 return std::move(x5);
94 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: std::move of the const variable 'x5' has no effect; remove std::move() or make the variable non-const [performance-move-const-arg]
95 // CHECK-FIXES: return x5;
96}
97
98template <typename T>
99T f6(const T x6) {
100 return std::move(x6);
101}
102
103void f7() { int a = f6(x6: 10); }
104
105#define M1(x) x
106void f8() {
107 const A a;
108 M1(A b = std::move(a);)
109 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: std::move of the const variable 'a' has no effect; remove std::move() or make the variable non-const
110 // CHECK-FIXES: M1(A b = a;)
111}
112
113#define M2(x) std::move(x)
114int f9() { return M2(1); }
115
116template <typename T>
117T f10(const int x10) {
118 return std::move(x10);
119 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: std::move of the const variable 'x10' of the trivially-copyable type 'const int' has no effect; remove std::move() [performance-move-const-arg]
120 // CHECK-FIXES: return x10;
121}
122void f11() {
123 f10<int>(x10: 1);
124 f10<double>(x10: 1);
125}
126
127class NoMoveSemantics {
128public:
129 NoMoveSemantics();
130 NoMoveSemantics(const NoMoveSemantics &);
131
132 NoMoveSemantics &operator=(const NoMoveSemantics &);
133};
134
135using NoMoveSemanticsAlias = NoMoveSemantics;
136
137void callByConstRef(const NoMoveSemantics &);
138void callByConstRef(int i, const NoMoveSemantics &);
139
140void moveToConstReferencePositives() {
141 NoMoveSemantics obj;
142
143 // Basic case.
144 callByConstRef(std::move(obj));
145 // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: passing result of std::move() as
146 // CHECK-FIXES: callByConstRef(obj);
147
148 // Also works for second argument.
149 callByConstRef(i: 1, std::move(obj));
150 // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: passing result of std::move() as
151 // CHECK-FIXES: callByConstRef(1, obj);
152
153 // Works if std::move() applied to a temporary.
154 callByConstRef(std::move(NoMoveSemantics()));
155 // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: passing result of std::move() as
156 // CHECK-FIXES: callByConstRef(NoMoveSemantics());
157
158 // Works if calling a copy constructor.
159 NoMoveSemantics other(std::move(obj));
160 // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: passing result of std::move() as
161 // CHECK-FIXES: NoMoveSemantics other(obj);
162
163 // Works if calling assignment operator.
164 other = std::move(obj);
165 // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: passing result of std::move() as
166 // CHECK-FIXES: other = obj;
167}
168
169void moveToConstReferencePositivesAlias() {
170 NoMoveSemanticsAlias obj;
171
172 // Basic case.
173 callByConstRef(std::move(obj));
174 // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: passing result of std::move() as a const reference argument; no move will actually happen [performance-move-const-arg]
175 // CHECK-FIXES: callByConstRef(obj);
176
177 // Also works for second argument.
178 callByConstRef(i: 1, std::move(obj));
179 // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: passing result of std::move() as a const reference argument; no move will actually happen [performance-move-const-arg]
180 // CHECK-FIXES: callByConstRef(1, obj);
181
182 // Works if std::move() applied to a temporary.
183 callByConstRef(std::move(NoMoveSemanticsAlias()));
184 // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: passing result of std::move() as a const reference argument; no move will actually happen [performance-move-const-arg]
185 // CHECK-FIXES: callByConstRef(NoMoveSemanticsAlias());
186
187 // Works if calling a copy constructor.
188 NoMoveSemanticsAlias other(std::move(obj));
189 // CHECK-MESSAGES: :[[@LINE-1]]:30: warning: passing result of std::move() as a const reference argument; no move will actually happen [performance-move-const-arg]
190 // CHECK-FIXES: NoMoveSemanticsAlias other(obj);
191
192 // Works if calling assignment operator.
193 other = std::move(obj);
194 // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: passing result of std::move() as a const reference argument; no move will actually happen [performance-move-const-arg]
195 // CHECK-FIXES: other = obj;
196}
197
198class MoveSemantics {
199public:
200 MoveSemantics();
201 MoveSemantics(MoveSemantics &&);
202
203 MoveSemantics &operator=(MoveSemantics &&);
204};
205
206using MoveSemanticsAlias = MoveSemantics;
207
208void callByValue(MoveSemantics);
209
210void callByRValueRef(MoveSemantics &&);
211
212template <class T>
213void templateFunction(T obj) {
214 T other = std::move(obj);
215}
216
217#define M3(T, obj) \
218 do { \
219 T other = std::move(obj); \
220 } while (true)
221
222#define CALL(func) (func)()
223
224void moveToConstReferenceNegatives() {
225 // No warning when actual move takes place.
226 MoveSemantics move_semantics;
227 callByValue(std::move(move_semantics));
228 callByRValueRef(std::move(move_semantics));
229 MoveSemantics other(std::move(move_semantics));
230 other = std::move(move_semantics);
231
232 // No warning if std::move() not used.
233 NoMoveSemantics no_move_semantics;
234 callByConstRef(no_move_semantics);
235
236 // No warning if instantiating a template.
237 templateFunction(obj: no_move_semantics);
238
239 // No warning inside of macro expansions.
240 M3(NoMoveSemantics, no_move_semantics);
241
242 // No warning inside of macro expansion, even if the macro expansion is inside
243 // a lambda that is, in turn, an argument to a macro.
244 CALL([no_move_semantics] { M3(NoMoveSemantics, no_move_semantics); });
245
246 auto lambda = [] {};
247 auto lambda2 = std::move(lambda);
248}
249
250void moveToConstReferenceNegativesAlias() {
251 // No warning when actual move takes place.
252 MoveSemanticsAlias move_semantics;
253 callByValue(std::move(move_semantics));
254 callByRValueRef(std::move(move_semantics));
255 MoveSemanticsAlias other(std::move(move_semantics));
256 other = std::move(move_semantics);
257
258 // No warning if std::move() not used.
259 NoMoveSemanticsAlias no_move_semantics;
260 callByConstRef(no_move_semantics);
261
262 // No warning if instantiating a template.
263 templateFunction(obj: no_move_semantics);
264
265 // No warning inside of macro expansions.
266 M3(NoMoveSemanticsAlias, no_move_semantics);
267
268 // No warning inside of macro expansion, even if the macro expansion is inside
269 // a lambda that is, in turn, an argument to a macro.
270 CALL([no_move_semantics] { M3(NoMoveSemanticsAlias, no_move_semantics); });
271
272 auto lambda = [] {};
273 auto lambda2 = std::move(lambda);
274}
275
276class MoveOnly {
277public:
278 MoveOnly(const MoveOnly &other) = delete;
279 MoveOnly &operator=(const MoveOnly &other) = delete;
280 MoveOnly(MoveOnly &&other) = default;
281 MoveOnly &operator=(MoveOnly &&other) = default;
282};
283template <class T>
284void Q(T);
285void moveOnlyNegatives(MoveOnly val) {
286 Q(std::move(val));
287}
288
289using MoveOnlyAlias = MoveOnly;
290
291void fmovable(MoveSemantics);
292
293void lambda1() {
294 auto f = [](MoveSemantics m) {
295 fmovable(std::move(m));
296 };
297 f(MoveSemantics());
298}
299
300template<class T> struct function {};
301
302template<typename Result, typename... Args>
303class function<Result(Args...)> {
304public:
305 function() = default;
306 void operator()(Args... args) const {
307 fmovable(std::forward<Args>(args)...);
308 }
309};
310
311void functionInvocation() {
312 function<void(MoveSemantics)> callback;
313 MoveSemantics m;
314 callback(std::move(m));
315}
316
317void functionInvocationAlias() {
318 function<void(MoveSemanticsAlias)> callback;
319 MoveSemanticsAlias m;
320 callback(std::move(m));
321}
322
323void lambda2() {
324 function<void(MoveSemantics)> callback;
325
326 auto f = [callback = std::move(callback)](MoveSemantics m) mutable {
327 // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: std::move of the variable 'callback' of the trivially-copyable type 'function<void (MoveSemantics)>' has no effect; remove std::move()
328 // CHECK-FIXES: auto f = [callback = callback](MoveSemantics m) mutable {
329 callback(std::move(m));
330 };
331 f(MoveSemantics());
332}
333
334void lambda2Alias() {
335 function<void(MoveSemanticsAlias)> callback;
336
337 auto f = [callback = std::move(callback)](MoveSemanticsAlias m) mutable {
338 // CHECK-MESSAGES: :[[@LINE-1]]:24: warning: std::move of the variable 'callback' of the trivially-copyable type 'function<void (MoveSemanticsAlias)>' (aka 'function<void (MoveSemantics)>') has no effect; remove std::move() [performance-move-const-arg]
339 // CHECK-FIXES: auto f = [callback = callback](MoveSemanticsAlias m) mutable {
340 callback(std::move(m));
341 };
342 f(MoveSemanticsAlias());
343}
344
345void showInt(int &&v);
346void showInt(int v1, int &&v2);
347void showPointer(const char *&&s);
348void showPointer2(const char *const &&s);
349void showTriviallyCopyable(TriviallyCopyable &&obj);
350void showTriviallyCopyablePointer(const TriviallyCopyable *&&obj);
351void testFunctions() {
352 int a = 10;
353 showInt(v: std::move(a));
354 // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: std::move of the variable 'a' of the trivially-copyable type 'int' has no effect [performance-move-const-arg]
355 // CHECK-MESSAGES: :[[@LINE-10]]:20: note: consider changing the 1st parameter of 'showInt' from 'int &&' to 'const int &'
356 showInt(v: int());
357 showInt(v1: a, v2: std::move(a));
358 // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: std::move of the variable 'a' of the trivially-copyable type 'int' has no effect [performance-move-const-arg]
359 // CHECK-MESSAGES: :[[@LINE-13]]:28: note: consider changing the 2nd parameter of 'showInt' from 'int &&' to 'const int &'
360 const char* s = "";
361 showPointer(s: std::move(s));
362 // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: std::move of the variable 's' of the trivially-copyable type 'const char *' has no effect [performance-move-const-arg]
363 // CHECK-MESSAGES: :[[@LINE-16]]:32: note: consider changing the 1st parameter of 'showPointer' from 'const char *&&' to 'const char *'
364 showPointer2(s: std::move(s));
365 // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: std::move of the variable 's' of the trivially-copyable type 'const char *' has no effect [performance-move-const-arg]
366 // CHECK-MESSAGES: :[[@LINE-18]]:39: note: consider changing the 1st parameter of 'showPointer2' from 'const char *const &&' to 'const char *const'
367 TriviallyCopyable *obj = new TriviallyCopyable();
368 showTriviallyCopyable(obj: std::move(*obj));
369 // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: std::move of the expression of the trivially-copyable type 'TriviallyCopyable' has no effect [performance-move-const-arg]
370 // CHECK-MESSAGES: :[[@LINE-21]]:48: note: consider changing the 1st parameter of 'showTriviallyCopyable' from 'TriviallyCopyable &&' to 'const TriviallyCopyable &'
371 showTriviallyCopyablePointer(obj: std::move(obj));
372 // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: std::move of the variable 'obj' of the trivially-copyable type 'TriviallyCopyable *' has no effect [performance-move-const-arg]
373 // CHECK-MESSAGES: :[[@LINE-23]]:62: note: consider changing the 1st parameter of 'showTriviallyCopyablePointer' from 'const TriviallyCopyable *&&' to 'const TriviallyCopyable *'
374 TrivialAlias* obj2 = new TrivialAlias();
375 showTriviallyCopyable(obj: std::move(*obj));
376 // CHECK-MESSAGES: :[[@LINE-1]]:25: warning: std::move of the expression of the trivially-copyable type 'TriviallyCopyable' has no effect [performance-move-const-arg]
377 // CHECK-MESSAGES: :[[@LINE-28]]:48: note: consider changing the 1st parameter of 'showTriviallyCopyable' from 'TriviallyCopyable &&' to 'const TriviallyCopyable &'
378 showTriviallyCopyablePointer(obj: std::move(obj));
379 // CHECK-MESSAGES: :[[@LINE-1]]:32: warning: std::move of the variable 'obj' of the trivially-copyable type 'TriviallyCopyable *' has no effect [performance-move-const-arg]
380 // CHECK-MESSAGES: :[[@LINE-30]]:62: note: consider changing the 1st parameter of 'showTriviallyCopyablePointer' from 'const TriviallyCopyable *&&' to 'const TriviallyCopyable *'
381}
382template <class T>
383void forwardToShowInt(T && t) {
384 showInt(static_cast<T &&>(t));
385}
386void testTemplate() {
387 int a = 10;
388 forwardToShowInt(t: std::move(a));
389 // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: std::move of the variable 'a' of the trivially-copyable type 'int' has no effect [performance-move-const-arg]
390}
391
392struct Tmp {
393 Tmp();
394 Tmp(int &&a);
395 Tmp(int v1, int &&a);
396 Tmp(const char *&&s);
397 Tmp(TriviallyCopyable&& obj);
398 Tmp(const TriviallyCopyable *&&obj);
399 void showTmp(TriviallyCopyable&& t);
400 static void showTmpStatic(TriviallyCopyable&& t);
401};
402using TmpAlias = Tmp;
403
404void testMethods() {
405 Tmp t;
406 int a = 10;
407 Tmp t1(std::move(a));
408 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: std::move of the variable 'a' of the trivially-copyable type 'int' has no effect [performance-move-const-arg]
409 // CHECK-MESSAGES: :[[@LINE-15]]:13: note: consider changing the 1st parameter of 'Tmp' from 'int &&' to 'const int &'
410 Tmp t2(a, std::move(a));
411 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: std::move of the variable 'a' of the trivially-copyable type 'int' has no effect [performance-move-const-arg]
412 // CHECK-MESSAGES: :[[@LINE-17]]:21: note: consider changing the 2nd parameter of 'Tmp' from 'int &&' to 'const int &'
413 const char* s = "";
414 Tmp t3(std::move(s));
415 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: std::move of the variable 's' of the trivially-copyable type 'const char *' has no effect [performance-move-const-arg]
416 // CHECK-MESSAGES: :[[@LINE-20]]:21: note: consider changing the 1st parameter of 'Tmp' from 'const char *&&' to 'const char *'
417 TriviallyCopyable *obj = new TriviallyCopyable();
418 Tmp t4(std::move(*obj));
419 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: std::move of the expression of the trivially-copyable type 'TriviallyCopyable' has no effect [performance-move-const-arg]
420 // CHECK-MESSAGES: :[[@LINE-23]]:27: note: consider changing the 1st parameter of 'Tmp' from 'TriviallyCopyable &&' to 'const TriviallyCopyable &'
421 Tmp t5(std::move(obj));
422 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: std::move of the variable 'obj' of the trivially-copyable type 'TriviallyCopyable *' has no effect [performance-move-const-arg]
423 // CHECK-MESSAGES: :[[@LINE-25]]:34: note: consider changing the 1st parameter of 'Tmp' from 'const TriviallyCopyable *&&' to 'const TriviallyCopyable *'
424 t.showTmp(t: std::move(*obj));
425 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: std::move of the expression of the trivially-copyable type 'TriviallyCopyable' has no effect [performance-move-const-arg]
426 // CHECK-MESSAGES: :[[@LINE-27]]:36: note: consider changing the 1st parameter of 'showTmp' from 'TriviallyCopyable &&' to 'const TriviallyCopyable &'
427 Tmp::showTmpStatic(t: std::move(*obj));
428 // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: std::move of the expression of the trivially-copyable type 'TriviallyCopyable' has no effect [performance-move-const-arg]
429 // CHECK-MESSAGES: :[[@LINE-29]]:49: note: consider changing the 1st parameter of 'showTmpStatic' from 'TriviallyCopyable &&' to 'const TriviallyCopyable &'
430}
431
432void testMethodsAlias() {
433 TmpAlias t;
434 int a = 10;
435 TmpAlias t1(std::move(a));
436 // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: std::move of the variable 'a' of the trivially-copyable type 'int' has no effect [performance-move-const-arg]
437 // CHECK-MESSAGES: :[[@LINE-43]]:13: note: consider changing the 1st parameter of 'Tmp' from 'int &&' to 'const int &'
438 TmpAlias t2(a, std::move(a));
439 // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: std::move of the variable 'a' of the trivially-copyable type 'int' has no effect [performance-move-const-arg]
440 // CHECK-MESSAGES: :[[@LINE-45]]:21: note: consider changing the 2nd parameter of 'Tmp' from 'int &&' to 'const int &'
441 const char* s = "";
442 TmpAlias t3(std::move(s));
443 // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: std::move of the variable 's' of the trivially-copyable type 'const char *' has no effect [performance-move-const-arg]
444 // CHECK-MESSAGES: :[[@LINE-48]]:21: note: consider changing the 1st parameter of 'Tmp' from 'const char *&&' to 'const char *'
445 TrivialAlias *obj = new TrivialAlias();
446 TmpAlias t4(std::move(*obj));
447 // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: std::move of the expression of the trivially-copyable type 'TrivialAlias' (aka 'TriviallyCopyable') has no effect [performance-move-const-arg]
448 // CHECK-MESSAGES: :[[@LINE-51]]:27: note: consider changing the 1st parameter of 'Tmp' from 'TriviallyCopyable &&' to 'const TriviallyCopyable &'
449 TmpAlias t5(std::move(obj));
450 // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: std::move of the variable 'obj' of the trivially-copyable type 'TrivialAlias *' (aka 'TriviallyCopyable *') has no effect [performance-move-const-arg]
451 // CHECK-MESSAGES: :[[@LINE-53]]:34: note: consider changing the 1st parameter of 'Tmp' from 'const TriviallyCopyable *&&' to 'const TriviallyCopyable *'
452 t.showTmp(t: std::move(*obj));
453 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: std::move of the expression of the trivially-copyable type 'TrivialAlias' (aka 'TriviallyCopyable') has no effect [performance-move-const-arg]
454 // CHECK-MESSAGES: :[[@LINE-55]]:36: note: consider changing the 1st parameter of 'showTmp' from 'TriviallyCopyable &&' to 'const TriviallyCopyable &'
455 TmpAlias::showTmpStatic(t: std::move(*obj));
456 // CHECK-MESSAGES: :[[@LINE-1]]:27: warning: std::move of the expression of the trivially-copyable type 'TrivialAlias' (aka 'TriviallyCopyable') has no effect [performance-move-const-arg]
457 // CHECK-MESSAGES: :[[@LINE-57]]:49: note: consider changing the 1st parameter of 'showTmpStatic' from 'TriviallyCopyable &&' to 'const TriviallyCopyable &'
458}
459
460void showA(A &&v) {}
461void testA() {
462 A a;
463 showA(v: std::move(a));
464}
465
466void testAAlias() {
467 AlsoA a;
468 showA(v: std::move(a));
469}
470
471void testFuncPointer() {
472 int a = 10;
473 void (*choice)(int, int &&);
474 choice = showInt;
475 choice(std::move(a), std::move(a));
476 // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: std::move of the variable 'a' of the trivially-copyable type 'int' has no effect; remove std::move() [performance-move-const-arg]
477 // CHECK-FIXES: choice(a, std::move(a));
478 // CHECK-MESSAGES: :[[@LINE-3]]:24: warning: std::move of the variable 'a' of the trivially-copyable type 'int' has no effect [performance-move-const-arg]
479}
480
481namespace issue_62550 {
482
483struct NonMoveConstructable {
484 NonMoveConstructable();
485 NonMoveConstructable(const NonMoveConstructable&);
486 NonMoveConstructable& operator=(const NonMoveConstructable&);
487 NonMoveConstructable& operator=(NonMoveConstructable&&);
488};
489
490void testNonMoveConstructible() {
491 NonMoveConstructable t1;
492 NonMoveConstructable t2{std::move(t1)};
493 // CHECK-MESSAGES: :[[@LINE-1]]:27: warning: passing result of std::move() as a const reference argument; no move will actually happen [performance-move-const-arg]
494 // CHECK-MESSAGES: :[[@LINE-11]]:8: note: 'NonMoveConstructable' is not move constructible
495}
496
497struct NonMoveAssignable {
498 NonMoveAssignable();
499 NonMoveAssignable(const NonMoveAssignable&);
500 NonMoveAssignable(NonMoveAssignable&&);
501
502 NonMoveAssignable& operator=(const NonMoveAssignable&);
503};
504
505void testNonMoveAssignable() {
506 NonMoveAssignable t1;
507 NonMoveAssignable t2;
508
509 t2 = std::move(t1);
510 // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: passing result of std::move() as a const reference argument; no move will actually happen [performance-move-const-arg]
511 // CHECK-MESSAGES: :[[@LINE-14]]:8: note: 'NonMoveAssignable' is not move assignable
512}
513
514struct NonMoveable {
515 NonMoveable();
516 NonMoveable(const NonMoveable&);
517 NonMoveable& operator=(const NonMoveable&);
518};
519
520void testNonMoveable() {
521 NonMoveable t1;
522 NonMoveable t2{std::move(t1)};
523 // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: passing result of std::move() as a const reference argument; no move will actually happen [performance-move-const-arg]
524 // CHECK-MESSAGES: :[[@LINE-10]]:8: note: 'NonMoveable' is not move assignable/constructible
525
526 t1 = std::move(t2);
527 // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: passing result of std::move() as a const reference argument; no move will actually happen [performance-move-const-arg]
528 // CHECK-MESSAGES: :[[@LINE-14]]:8: note: 'NonMoveable' is not move assignable/constructible
529}
530
531using AlsoNonMoveable = NonMoveable;
532
533void testAlsoNonMoveable() {
534 AlsoNonMoveable t1;
535 AlsoNonMoveable t2{std::move(t1)};
536 // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: passing result of std::move() as a const reference argument; no move will actually happen [performance-move-const-arg]
537 // CHECK-MESSAGES: :[[@LINE-23]]:8: note: 'NonMoveable' is not move assignable/constructible
538
539 t1 = std::move(t2);
540 // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: passing result of std::move() as a const reference argument; no move will actually happen [performance-move-const-arg]
541 // CHECK-MESSAGES: :[[@LINE-27]]:8: note: 'NonMoveable' is not move assignable/constructible
542}
543
544} // namespace issue_62550
545

source code of clang-tools-extra/test/clang-tidy/checkers/performance/move-const-arg.cpp