Debian Codegraph @

clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-copy-initialization.cpp

raw
1 // RUN: %check_clang_tidy -std=c++17-or-later %s performance-unnecessary-copy-initialization %t
2
3 template <typename T>
4 struct Iterator {
5 void operator++();
6 T &operator*() const;
7 bool operator!=(const Iterator &) const;
8 typedef const T &const_reference;
9 };
10
11 template <typename T>
12 struct ConstIterator {
13 void operator++();
14 const T &operator*() const;
15 bool operator!=(const ConstIterator &) const;
16 typedef const T &const_reference;
17 };
18
19 struct ExpensiveToCopyType {
20 ExpensiveToCopyType();
21 virtual ~ExpensiveToCopyType();
22 const ExpensiveToCopyType &reference() const;
23 using ConstRef = const ExpensiveToCopyType &;
24 ConstRef referenceWithAlias() const;
25 const ExpensiveToCopyType *pointer() const;
26 void nonConstMethod();
27 bool constMethod() const;
28 template <typename A>
29 const A &templatedAccessor() const;
30 operator int() const; // Implicit conversion to int.
31 };
32
33 template <typename T>
34 struct Container {
35 using reference = T&;
36 using const_reference = const T&;
37
38 bool empty() const;
39 const T& operator[](int) const;
40 const T& operator[](int);
41 Iterator<T> begin();
42 Iterator<T> end();
43 ConstIterator<T> begin() const;
44 ConstIterator<T> end() const;
45 void nonConstMethod();
46 bool constMethod() const;
47
48 reference at(int) const;
49 const_reference at(int);
50
51 };
52
53 using ExpensiveToCopyContainerAlias = Container<ExpensiveToCopyType>;
54
55 struct TrivialToCopyType {
56 const TrivialToCopyType &reference() const;
57 };
58
59 struct WeirdCopyCtorType {
60 WeirdCopyCtorType();
61 WeirdCopyCtorType(const WeirdCopyCtorType &w, bool oh_yes = true);
62
63 void nonConstMethod();
64 bool constMethod() const;
65 };
66
67 ExpensiveToCopyType global_expensive_to_copy_type;
68
69 const ExpensiveToCopyType &ExpensiveTypeReference();
70 const ExpensiveToCopyType &freeFunctionWithArg(const ExpensiveToCopyType &);
71 const ExpensiveToCopyType &freeFunctionWithDefaultArg(
72 const ExpensiveToCopyType *arg = nullptr);
73 const TrivialToCopyType &TrivialTypeReference();
74
75 void mutate(ExpensiveToCopyType &);
76 void mutate(ExpensiveToCopyType *);
77 void useAsConstPointer(const ExpensiveToCopyType *);
78 void useAsConstReference(const ExpensiveToCopyType &);
79 void useByValue(ExpensiveToCopyType);
80
81 void PositiveFunctionCall() {
82 const auto AutoAssigned = ExpensiveTypeReference();
83 // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoAssigned' is copy-constructed from a const reference; consider making it a const reference [performance-unnecessary-copy-initialization]
84 // CHECK-FIXES: const auto& AutoAssigned = ExpensiveTypeReference();
85 AutoAssigned.constMethod();
86
87 const auto AutoCopyConstructed(ExpensiveTypeReference());
88 // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoCopyConstructed'
89 // CHECK-FIXES: const auto& AutoCopyConstructed(ExpensiveTypeReference());
90 AutoCopyConstructed.constMethod();
91
92 const ExpensiveToCopyType VarAssigned = ExpensiveTypeReference();
93 // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable 'VarAssigned'
94 // CHECK-FIXES: const ExpensiveToCopyType& VarAssigned = ExpensiveTypeReference();
95 VarAssigned.constMethod();
96
97 const ExpensiveToCopyType VarCopyConstructed(ExpensiveTypeReference());
98 // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable 'VarCopyConstructed'
99 // CHECK-FIXES: const ExpensiveToCopyType& VarCopyConstructed(ExpensiveTypeReference());
100 VarCopyConstructed.constMethod();
101 }
102
103 void PositiveMethodCallConstReferenceParam(const ExpensiveToCopyType &Obj) {
104 const auto AutoAssigned = Obj.reference();
105 // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoAssigned'
106 // CHECK-FIXES: const auto& AutoAssigned = Obj.reference();
107 AutoAssigned.constMethod();
108
109 const auto AutoCopyConstructed(Obj.reference());
110 // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoCopyConstructed'
111 // CHECK-FIXES: const auto& AutoCopyConstructed(Obj.reference());
112 AutoCopyConstructed.constMethod();
113
114 const ExpensiveToCopyType VarAssigned = Obj.reference();
115 // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable 'VarAssigned'
116 // CHECK-FIXES: const ExpensiveToCopyType& VarAssigned = Obj.reference();
117 VarAssigned.constMethod();
118
119 const ExpensiveToCopyType VarCopyConstructed(Obj.reference());
120 // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable 'VarCopyConstructed'
121 // CHECK-FIXES: const ExpensiveToCopyType& VarCopyConstructed(Obj.reference());
122 VarCopyConstructed.constMethod();
123 }
124
125 void PositiveMethodCallConstParam(const ExpensiveToCopyType Obj) {
126 const auto AutoAssigned = Obj.reference();
127 // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoAssigned'
128 // CHECK-FIXES: const auto& AutoAssigned = Obj.reference();
129 AutoAssigned.constMethod();
130
131 const auto AutoCopyConstructed(Obj.reference());
132 // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoCopyConstructed'
133 // CHECK-FIXES: const auto& AutoCopyConstructed(Obj.reference());
134 AutoCopyConstructed.constMethod();
135
136 const ExpensiveToCopyType VarAssigned = Obj.reference();
137 // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable 'VarAssigned'
138 // CHECK-FIXES: const ExpensiveToCopyType& VarAssigned = Obj.reference();
139 VarAssigned.constMethod();
140
141 const ExpensiveToCopyType VarCopyConstructed(Obj.reference());
142 // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable 'VarCopyConstructed'
143 // CHECK-FIXES: const ExpensiveToCopyType& VarCopyConstructed(Obj.reference());
144 VarCopyConstructed.constMethod();
145 }
146
147 void PositiveMethodCallConstPointerParam(const ExpensiveToCopyType *const Obj) {
148 const auto AutoAssigned = Obj->reference();
149 // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoAssigned'
150 // CHECK-FIXES: const auto& AutoAssigned = Obj->reference();
151 AutoAssigned.constMethod();
152
153 const auto AutoCopyConstructed(Obj->reference());
154 // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoCopyConstructed'
155 // CHECK-FIXES: const auto& AutoCopyConstructed(Obj->reference());
156 AutoCopyConstructed.constMethod();
157
158 const ExpensiveToCopyType VarAssigned = Obj->reference();
159 // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable 'VarAssigned'
160 // CHECK-FIXES: const ExpensiveToCopyType& VarAssigned = Obj->reference();
161 VarAssigned.constMethod();
162
163 const ExpensiveToCopyType VarCopyConstructed(Obj->reference());
164 // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable 'VarCopyConstructed'
165 // CHECK-FIXES: const ExpensiveToCopyType& VarCopyConstructed(Obj->reference());
166 VarCopyConstructed.constMethod();
167 }
168
169 void PositiveOperatorCallConstReferenceParam(const Container<ExpensiveToCopyType> &C) {
170 const auto AutoAssigned = C[42];
171 // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoAssigned'
172 // CHECK-FIXES: const auto& AutoAssigned = C[42];
173 AutoAssigned.constMethod();
174
175 const auto AutoCopyConstructed(C[42]);
176 // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoCopyConstructed'
177 // CHECK-FIXES: const auto& AutoCopyConstructed(C[42]);
178 AutoCopyConstructed.constMethod();
179
180 const ExpensiveToCopyType VarAssigned = C[42];
181 // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable 'VarAssigned'
182 // CHECK-FIXES: const ExpensiveToCopyType& VarAssigned = C[42];
183 VarAssigned.constMethod();
184
185 const ExpensiveToCopyType VarCopyConstructed(C[42]);
186 // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable 'VarCopyConstructed'
187 // CHECK-FIXES: const ExpensiveToCopyType& VarCopyConstructed(C[42]);
188 VarCopyConstructed.constMethod();
189 }
190
191 void PositiveOperatorCallConstValueParam(const Container<ExpensiveToCopyType> C) {
192 const auto AutoAssigned = C[42];
193 // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoAssigned'
194 // CHECK-FIXES: const auto& AutoAssigned = C[42];
195 AutoAssigned.constMethod();
196
197 const auto AutoCopyConstructed(C[42]);
198 // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoCopyConstructed'
199 // CHECK-FIXES: const auto& AutoCopyConstructed(C[42]);
200 AutoCopyConstructed.constMethod();
201
202 const ExpensiveToCopyType VarAssigned = C[42];
203 // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable 'VarAssigned'
204 // CHECK-FIXES: const ExpensiveToCopyType& VarAssigned = C[42];
205 VarAssigned.constMethod();
206
207 const ExpensiveToCopyType VarCopyConstructed(C[42]);
208 // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable 'VarCopyConstructed'
209 // CHECK-FIXES: const ExpensiveToCopyType& VarCopyConstructed(C[42]);
210 VarCopyConstructed.constMethod();
211 }
212
213 void PositiveOperatorValueParam(Container<ExpensiveToCopyType> C) {
214 const auto AutoAssigned = C[42];
215 // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoAssigned'
216 // CHECK-FIXES: const auto& AutoAssigned = C[42];
217 AutoAssigned.constMethod();
218
219 const auto AutoCopyConstructed(C[42]);
220 // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoCopyConstructed'
221 // CHECK-FIXES: const auto& AutoCopyConstructed(C[42]);
222 AutoCopyConstructed.constMethod();
223
224 const ExpensiveToCopyType VarAssigned = C.at(42);
225 // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable 'VarAssigned'
226 // CHECK-FIXES: const ExpensiveToCopyType& VarAssigned = C.at(42);
227 VarAssigned.constMethod();
228
229 const ExpensiveToCopyType VarCopyConstructed(C.at(42));
230 // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable 'VarCopyConstructed'
231 // CHECK-FIXES: const ExpensiveToCopyType& VarCopyConstructed(C.at(42));
232 VarCopyConstructed.constMethod();
233 }
234
235 void PositiveOperatorCallConstValueParamAlias(const ExpensiveToCopyContainerAlias C) {
236 const auto AutoAssigned = C[42];
237 // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoAssigned'
238 // CHECK-FIXES: const auto& AutoAssigned = C[42];
239 AutoAssigned.constMethod();
240
241 const auto AutoCopyConstructed(C[42]);
242 // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoCopyConstructed'
243 // CHECK-FIXES: const auto& AutoCopyConstructed(C[42]);
244 AutoCopyConstructed.constMethod();
245
246 const ExpensiveToCopyType VarAssigned = C[42];
247 // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable 'VarAssigned'
248 // CHECK-FIXES: const ExpensiveToCopyType& VarAssigned = C[42];
249 VarAssigned.constMethod();
250
251 const ExpensiveToCopyType VarCopyConstructed(C[42]);
252 // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable 'VarCopyConstructed'
253 // CHECK-FIXES: const ExpensiveToCopyType& VarCopyConstructed(C[42]);
254 VarCopyConstructed.constMethod();
255 }
256
257 void PositiveOperatorCallConstPtrParam(const Container<ExpensiveToCopyType>* C) {
258 const auto AutoAssigned = (*C)[42];
259 // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoAssigned'
260 // CHECK-FIXES: const auto& AutoAssigned = (*C)[42];
261 AutoAssigned.constMethod();
262
263 const auto AutoCopyConstructed((*C)[42]);
264 // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoCopyConstructed'
265 // CHECK-FIXES: const auto& AutoCopyConstructed((*C)[42]);
266 AutoCopyConstructed.constMethod();
267
268 const ExpensiveToCopyType VarAssigned = C->at(42);
269 // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable 'VarAssigned'
270 // CHECK-FIXES: const ExpensiveToCopyType& VarAssigned = C->at(42);
271 VarAssigned.constMethod();
272
273 const ExpensiveToCopyType VarCopyConstructed(C->at(42));
274 // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable 'VarCopyConstructed'
275 // CHECK-FIXES: const ExpensiveToCopyType& VarCopyConstructed(C->at(42));
276 VarCopyConstructed.constMethod();
277 }
278
279 void PositiveLocalConstValue() {
280 const ExpensiveToCopyType Obj;
281 const auto UnnecessaryCopy = Obj.reference();
282 // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'UnnecessaryCopy'
283 // CHECK-FIXES: const auto& UnnecessaryCopy = Obj.reference();
284 UnnecessaryCopy.constMethod();
285 }
286
287 void PositiveLocalConstRef() {
288 const ExpensiveToCopyType Obj;
289 const ExpensiveToCopyType &ConstReference = Obj.reference();
290 const auto UnnecessaryCopy = ConstReference.reference();
291 // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'UnnecessaryCopy'
292 // CHECK-FIXES: const auto& UnnecessaryCopy = ConstReference.reference();
293 UnnecessaryCopy.constMethod();
294 }
295
296 void PositiveLocalConstPointer() {
297 const ExpensiveToCopyType Obj;
298 const ExpensiveToCopyType *const ConstPointer = &Obj;
299 const auto UnnecessaryCopy = ConstPointer->reference();
300 // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'UnnecessaryCopy'
301 // CHECK-FIXES: const auto& UnnecessaryCopy = ConstPointer->reference();
302 UnnecessaryCopy.constMethod();
303 }
304
305 void NegativeFunctionCallTrivialType() {
306 const auto AutoAssigned = TrivialTypeReference();
307 const auto AutoCopyConstructed(TrivialTypeReference());
308 const TrivialToCopyType VarAssigned = TrivialTypeReference();
309 const TrivialToCopyType VarCopyConstructed(TrivialTypeReference());
310 }
311
312 void NegativeStaticLocalVar(const ExpensiveToCopyType &Obj) {
313 static const auto StaticVar = Obj.reference();
314 }
315
316 void PositiveFunctionCallExpensiveTypeNonConstVariable() {
317 auto AutoAssigned = ExpensiveTypeReference();
318 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: the variable 'AutoAssigned' is copy-constructed from a const reference but is only used as const reference; consider making it a const reference [performance-unnecessary-copy-initialization]
319 // CHECK-FIXES: const auto& AutoAssigned = ExpensiveTypeReference();
320 AutoAssigned.constMethod();
321
322 auto AutoCopyConstructed(ExpensiveTypeReference());
323 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: the variable 'AutoCopyConstructed'
324 // CHECK-FIXES: const auto& AutoCopyConstructed(ExpensiveTypeReference());
325 AutoCopyConstructed.constMethod();
326
327 ExpensiveToCopyType VarAssigned = ExpensiveTypeReference();
328 // CHECK-MESSAGES: [[@LINE-1]]:23: warning: the variable 'VarAssigned'
329 // CHECK-FIXES: const ExpensiveToCopyType& VarAssigned = ExpensiveTypeReference();
330 VarAssigned.constMethod();
331
332 ExpensiveToCopyType VarCopyConstructed(ExpensiveTypeReference());
333 // CHECK-MESSAGES: [[@LINE-1]]:23: warning: the variable 'VarCopyConstructed'
334 // CHECK-FIXES: const ExpensiveToCopyType& VarCopyConstructed(ExpensiveTypeReference());
335 VarCopyConstructed.constMethod();
336 }
337
338 void positiveNonConstVarInCodeBlock(const ExpensiveToCopyType &Obj) {
339 {
340 auto Assigned = Obj.reference();
341 // CHECK-MESSAGES: [[@LINE-1]]:10: warning: the variable 'Assigned'
342 // CHECK-FIXES: const auto& Assigned = Obj.reference();
343 Assigned.reference();
344 useAsConstReference(Assigned);
345 useByValue(Assigned);
346 }
347 }
348
349 void positiveNonConstVarInCodeBlockWithAlias(const ExpensiveToCopyType &Obj) {
350 {
351 const ExpensiveToCopyType Assigned = Obj.referenceWithAlias();
352 // CHECK-MESSAGES: [[@LINE-1]]:31: warning: the const qualified variable
353 // CHECK-FIXES: const ExpensiveToCopyType& Assigned = Obj.referenceWithAlias();
354 useAsConstReference(Assigned);
355 }
356 }
357
358 void negativeNonConstVarWithNonConstUse(const ExpensiveToCopyType &Obj) {
359 {
360 auto NonConstInvoked = Obj.reference();
361 // CHECK-FIXES: auto NonConstInvoked = Obj.reference();
362 NonConstInvoked.nonConstMethod();
363 }
364 {
365 auto Reassigned = Obj.reference();
366 // CHECK-FIXES: auto Reassigned = Obj.reference();
367 Reassigned = ExpensiveToCopyType();
368 }
369 {
370 auto MutatedByReference = Obj.reference();
371 // CHECK-FIXES: auto MutatedByReference = Obj.reference();
372 mutate(MutatedByReference);
373 }
374 {
375 auto MutatedByPointer = Obj.reference();
376 // CHECK-FIXES: auto MutatedByPointer = Obj.reference();
377 mutate(&MutatedByPointer);
378 }
379 }
380
381 void PositiveMethodCallNonConstRefNotModified(ExpensiveToCopyType &Obj) {
382 const auto AutoAssigned = Obj.reference();
383 // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoAssigned'
384 // CHECK-FIXES: const auto& AutoAssigned = Obj.reference();
385 AutoAssigned.constMethod();
386 }
387
388 void NegativeMethodCallNonConstRefIsModified(ExpensiveToCopyType &Obj) {
389 const auto AutoAssigned = Obj.reference();
390 const auto AutoCopyConstructed(Obj.reference());
391 const ExpensiveToCopyType VarAssigned = Obj.reference();
392 const ExpensiveToCopyType VarCopyConstructed(Obj.reference());
393 mutate(&Obj);
394 }
395
396 void PositiveMethodCallNonConstNotModified(ExpensiveToCopyType Obj) {
397 const auto AutoAssigned = Obj.reference();
398 // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoAssigned'
399 // CHECK-FIXES: const auto& AutoAssigned = Obj.reference();
400 AutoAssigned.constMethod();
401 }
402
403 void NegativeMethodCallNonConstValueArgumentIsModified(ExpensiveToCopyType Obj) {
404 Obj.nonConstMethod();
405 const auto AutoAssigned = Obj.reference();
406 }
407
408 void PositiveMethodCallNonConstPointerNotModified(ExpensiveToCopyType *const Obj) {
409 const auto AutoAssigned = Obj->reference();
410 // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoAssigned'
411 // CHECK-FIXES: const auto& AutoAssigned = Obj->reference();
412 Obj->constMethod();
413 AutoAssigned.constMethod();
414 }
415
416 void NegativeMethodCallNonConstPointerIsModified(ExpensiveToCopyType *const Obj) {
417 const auto AutoAssigned = Obj->reference();
418 const auto AutoCopyConstructed(Obj->reference());
419 const ExpensiveToCopyType VarAssigned = Obj->reference();
420 const ExpensiveToCopyType VarCopyConstructed(Obj->reference());
421 mutate(Obj);
422 }
423
424 void PositiveLocalVarIsNotModified() {
425 ExpensiveToCopyType LocalVar;
426 const auto AutoAssigned = LocalVar.reference();
427 // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoAssigned'
428 // CHECK-FIXES: const auto& AutoAssigned = LocalVar.reference();
429 AutoAssigned.constMethod();
430 }
431
432 void NegativeLocalVarIsModified() {
433 ExpensiveToCopyType Obj;
434 const auto AutoAssigned = Obj.reference();
435 Obj = AutoAssigned;
436 }
437
438 struct NegativeConstructor {
439 NegativeConstructor(const ExpensiveToCopyType &Obj) : Obj(Obj) {}
440 ExpensiveToCopyType Obj;
441 };
442
443 #define UNNECESSARY_COPY_INIT_IN_MACRO_BODY(TYPE) \
444 void functionWith##TYPE(const TYPE &T) { \
445 auto AssignedInMacro = T.reference(); \
446 } \
447 // Ensure fix is not applied.
448 // CHECK-FIXES: auto AssignedInMacro = T.reference();
449
450 UNNECESSARY_COPY_INIT_IN_MACRO_BODY(ExpensiveToCopyType)
451 // CHECK-MESSAGES: [[@LINE-1]]:1: warning: the variable 'AssignedInMacro' is copy-constructed
452
453 #define UNNECESSARY_COPY_INIT_IN_MACRO_ARGUMENT(ARGUMENT) ARGUMENT
454
455 void PositiveMacroArgument(const ExpensiveToCopyType &Obj) {
456 UNNECESSARY_COPY_INIT_IN_MACRO_ARGUMENT(auto CopyInMacroArg = Obj.reference());
457 // CHECK-MESSAGES: [[@LINE-1]]:48: warning: the variable 'CopyInMacroArg' is copy-constructed
458 // Ensure fix is not applied.
459 // CHECK-FIXES: auto CopyInMacroArg = Obj.reference()
460 CopyInMacroArg.constMethod();
461 }
462
463 void PositiveLocalCopyConstMethodInvoked() {
464 ExpensiveToCopyType orig;
465 ExpensiveToCopyType copy_1 = orig;
466 // CHECK-MESSAGES: [[@LINE-1]]:23: warning: local copy 'copy_1' of the variable 'orig' is never modified; consider avoiding the copy [performance-unnecessary-copy-initialization]
467 // CHECK-FIXES: const ExpensiveToCopyType& copy_1 = orig;
468 copy_1.constMethod();
469 orig.constMethod();
470 }
471
472 void PositiveLocalCopyUsingExplicitCopyCtor() {
473 ExpensiveToCopyType orig;
474 ExpensiveToCopyType copy_2(orig);
475 // CHECK-MESSAGES: [[@LINE-1]]:23: warning: local copy 'copy_2'
476 // CHECK-FIXES: const ExpensiveToCopyType& copy_2(orig);
477 copy_2.constMethod();
478 orig.constMethod();
479 }
480
481 void PositiveLocalCopyCopyIsArgument(const ExpensiveToCopyType &orig) {
482 ExpensiveToCopyType copy_3 = orig;
483 // CHECK-MESSAGES: [[@LINE-1]]:23: warning: local copy 'copy_3'
484 // CHECK-FIXES: const ExpensiveToCopyType& copy_3 = orig;
485 copy_3.constMethod();
486 }
487
488 void PositiveLocalCopyUsedAsConstRef() {
489 ExpensiveToCopyType orig;
490 ExpensiveToCopyType copy_4 = orig;
491 // CHECK-MESSAGES: [[@LINE-1]]:23: warning: local copy 'copy_4'
492 // CHECK-FIXES: const ExpensiveToCopyType& copy_4 = orig;
493 useAsConstReference(orig);
494 copy_4.constMethod();
495 }
496
497 void PositiveLocalCopyTwice() {
498 ExpensiveToCopyType orig;
499 ExpensiveToCopyType copy_5 = orig;
500 // CHECK-MESSAGES: [[@LINE-1]]:23: warning: local copy 'copy_5'
501 // CHECK-FIXES: const ExpensiveToCopyType& copy_5 = orig;
502 ExpensiveToCopyType copy_6 = copy_5;
503 // CHECK-MESSAGES: [[@LINE-1]]:23: warning: local copy 'copy_6'
504 // CHECK-FIXES: const ExpensiveToCopyType& copy_6 = copy_5;
505 copy_5.constMethod();
506 copy_6.constMethod();
507 orig.constMethod();
508 }
509
510
511 void PositiveLocalCopyWeirdCopy() {
512 WeirdCopyCtorType orig;
513 WeirdCopyCtorType weird_1(orig);
514 // CHECK-MESSAGES: [[@LINE-1]]:21: warning: local copy 'weird_1'
515 // CHECK-FIXES: const WeirdCopyCtorType& weird_1(orig);
516 weird_1.constMethod();
517
518 WeirdCopyCtorType weird_2 = orig;
519 // CHECK-MESSAGES: [[@LINE-1]]:21: warning: local copy 'weird_2'
520 // CHECK-FIXES: const WeirdCopyCtorType& weird_2 = orig;
521 weird_2.constMethod();
522 }
523
524 void NegativeLocalCopySimpleTypes() {
525 int i1 = 0;
526 int i2 = i1;
527 }
528
529 void NegativeLocalCopyCopyIsModified() {
530 ExpensiveToCopyType orig;
531 ExpensiveToCopyType neg_copy_1 = orig;
532 neg_copy_1.nonConstMethod();
533 }
534
535 void NegativeLocalCopyOriginalIsModified() {
536 ExpensiveToCopyType orig;
537 ExpensiveToCopyType neg_copy_2 = orig;
538 orig.nonConstMethod();
539 }
540
541 void NegativeLocalCopyUsedAsRefArg() {
542 ExpensiveToCopyType orig;
543 ExpensiveToCopyType neg_copy_3 = orig;
544 mutate(neg_copy_3);
545 }
546
547 void NegativeLocalCopyUsedAsPointerArg() {
548 ExpensiveToCopyType orig;
549 ExpensiveToCopyType neg_copy_4 = orig;
550 mutate(&neg_copy_4);
551 }
552
553 void NegativeLocalCopyCopyFromGlobal() {
554 ExpensiveToCopyType neg_copy_5 = global_expensive_to_copy_type;
555 }
556
557 void NegativeLocalCopyCopyToStatic() {
558 ExpensiveToCopyType orig;
559 static ExpensiveToCopyType neg_copy_6 = orig;
560 }
561
562 void NegativeLocalCopyNonConstInForLoop() {
563 ExpensiveToCopyType orig;
564 for (ExpensiveToCopyType neg_copy_7 = orig; orig.constMethod();
565 orig.nonConstMethod()) {
566 orig.constMethod();
567 }
568 }
569
570 void NegativeLocalCopyWeirdNonCopy() {
571 WeirdCopyCtorType orig;
572 WeirdCopyCtorType neg_weird_1(orig, false);
573 WeirdCopyCtorType neg_weird_2(orig, true);
574 }
575 void WarningOnlyMultiDeclStmt() {
576 ExpensiveToCopyType orig;
577 ExpensiveToCopyType copy = orig, copy2;
578 // CHECK-MESSAGES: [[@LINE-1]]:23: warning: local copy 'copy' of the variable 'orig' is never modified; consider avoiding the copy [performance-unnecessary-copy-initialization]
579 // CHECK-FIXES: ExpensiveToCopyType copy = orig, copy2;
580 copy.constMethod();
581 }
582
583 class Element {};
584
585 void implicitVarFalsePositive() {
586 for (const Element &E : Container<Element>()) {
587 }
588 }
589
590 // This should not trigger the check as the argument could introduce an alias.
591 void negativeInitializedFromFreeFunctionWithArg() {
592 ExpensiveToCopyType Orig;
593 const ExpensiveToCopyType Copy = freeFunctionWithArg(Orig);
594 }
595
596 void negativeInitializedFromFreeFunctionWithDefaultArg() {
597 const ExpensiveToCopyType Copy = freeFunctionWithDefaultArg();
598 }
599
600 void negativeInitialzedFromFreeFunctionWithNonDefaultArg() {
601 ExpensiveToCopyType Orig;
602 const ExpensiveToCopyType Copy = freeFunctionWithDefaultArg(&Orig);
603 }
604
605 namespace std {
606 inline namespace __1 {
607
608 template <class>
609 class function;
610 template <class R, class... ArgTypes>
611 class function<R(ArgTypes...)> {
612 public:
613 function();
614 function(const function &Other);
615 R operator()(ArgTypes... Args) const;
616 };
617
618 } // namespace __1
619 } // namespace std
620
621 void negativeStdFunction() {
622 std::function<int()> Orig;
623 std::function<int()> Copy = Orig;
624 int i = Orig();
625 }
626
627 using Functor = std::function<int()>;
628
629 void negativeAliasedStdFunction() {
630 Functor Orig;
631 Functor Copy = Orig;
632 int i = Orig();
633 }
634
635 typedef std::function<int()> TypedefFunc;
636
637 void negativeTypedefedStdFunction() {
638 TypedefFunc Orig;
639 TypedefFunc Copy = Orig;
640 int i = Orig();
641 }
642
643 namespace fake {
644 namespace std {
645 template <class R, class... Args>
646 struct function {
647 // Custom copy constructor makes it expensive to copy;
648 function(const function &);
649 void constMethod() const;
650 };
651 } // namespace std
652
653 void positiveFakeStdFunction(std::function<void(int)> F) {
654 auto Copy = F;
655 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: local copy 'Copy' of the variable 'F' is never modified;
656 // CHECK-FIXES: const auto& Copy = F;
657 Copy.constMethod();
658 }
659
660 } // namespace fake
661
662 void positiveInvokedOnStdFunction(
663 std::function<void(const ExpensiveToCopyType &)> Update,
664 const ExpensiveToCopyType Orig) {
665 auto Copy = Orig.reference();
666 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: the variable 'Copy' is copy-constructed from a const reference
667 // CHECK-FIXES: const auto& Copy = Orig.reference();
668 Update(Copy);
669 }
670
671 void negativeInvokedOnStdFunction(
672 std::function<void(ExpensiveToCopyType &)> Update,
673 const ExpensiveToCopyType Orig) {
674 auto Copy = Orig.reference();
675 Update(Copy);
676 }
677
678 void negativeCopiedFromReferenceToModifiedVar() {
679 ExpensiveToCopyType Orig;
680 const auto &Ref = Orig;
681 const auto NecessaryCopy = Ref;
682 Orig.nonConstMethod();
683 }
684
685 void positiveCopiedFromReferenceToConstVar() {
686 ExpensiveToCopyType Orig;
687 const auto &Ref = Orig;
688 const auto UnnecessaryCopy = Ref;
689 // CHECK-MESSAGES: [[@LINE-1]]:14: warning: local copy 'UnnecessaryCopy' of
690 // CHECK-FIXES: const auto& UnnecessaryCopy = Ref;
691 Orig.constMethod();
692 UnnecessaryCopy.constMethod();
693 }
694
695 void negativeCopiedFromGetterOfReferenceToModifiedVar() {
696 ExpensiveToCopyType Orig;
697 const auto &Ref = Orig.reference();
698 const auto NecessaryCopy = Ref.reference();
699 Orig.nonConstMethod();
700 }
701
702 void negativeAliasNonCanonicalPointerType() {
703 ExpensiveToCopyType Orig;
704 // The use of auto here hides that the type is a pointer type. The check needs
705 // to look at the canonical type to detect the aliasing through this pointer.
706 const auto Pointer = Orig.pointer();
707 const auto NecessaryCopy = Pointer->reference();
708 Orig.nonConstMethod();
709 }
710
711 void negativeAliasTypedefedType() {
712 typedef const ExpensiveToCopyType &ReferenceType;
713 ExpensiveToCopyType Orig;
714 // The typedef hides the fact that this is a reference type. The check needs
715 // to look at the canonical type to detect the aliasing.
716 ReferenceType Ref = Orig.reference();
717 const auto NecessaryCopy = Ref.reference();
718 Orig.nonConstMethod();
719 }
720
721 void positiveCopiedFromGetterOfReferenceToConstVar() {
722 ExpensiveToCopyType Orig;
723 const auto &Ref = Orig.reference();
724 auto UnnecessaryCopy = Ref.reference();
725 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: the variable 'UnnecessaryCopy' is
726 // CHECK-FIXES: const auto& UnnecessaryCopy = Ref.reference();
727 Orig.constMethod();
728 UnnecessaryCopy.constMethod();
729 }
730
731 void positiveUnusedReferenceIsRemoved() {
732 // clang-format off
733 const auto AutoAssigned = ExpensiveTypeReference(); int i = 0; // Foo bar.
734 // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoAssigned' is copy-constructed from a const reference but is never used; consider removing the statement [performance-unnecessary-copy-initialization]
735 // CHECK-FIXES-NOT: const auto AutoAssigned = ExpensiveTypeReference();
736 // CHECK-FIXES: int i = 0; // Foo bar.
737 auto TrailingCommentRemoved = ExpensiveTypeReference(); // Trailing comment.
738 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: the variable 'TrailingCommentRemoved' is copy-constructed from a const reference but is never used;
739 // CHECK-FIXES-NOT: auto TrailingCommentRemoved = ExpensiveTypeReference();
740 // CHECK-FIXES-NOT: // Trailing comment.
741 // clang-format on
742
743 auto UnusedAndUnnecessary = ExpensiveTypeReference();
744 // Comments on a new line should not be deleted.
745 // CHECK-MESSAGES: [[@LINE-2]]:8: warning: the variable 'UnusedAndUnnecessary' is copy-constructed
746 // CHECK-FIXES-NOT: auto UnusedAndUnnecessary = ExpensiveTypeReference();
747 // CHECK-FIXES: // Comments on a new line should not be deleted.
748 }
749
750 void positiveLoopedOverObjectIsConst() {
751 const Container<ExpensiveToCopyType> Orig;
752 for (const auto &Element : Orig) {
753 const auto Copy = Element;
754 // CHECK-MESSAGES: [[@LINE-1]]:16: warning: local copy 'Copy'
755 // CHECK-FIXES: const auto& Copy = Element;
756 Orig.constMethod();
757 Copy.constMethod();
758 }
759
760 auto Lambda = []() {
761 const Container<ExpensiveToCopyType> Orig;
762 for (const auto &Element : Orig) {
763 const auto Copy = Element;
764 // CHECK-MESSAGES: [[@LINE-1]]:18: warning: local copy 'Copy'
765 // CHECK-FIXES: const auto& Copy = Element;
766 Orig.constMethod();
767 Copy.constMethod();
768 }
769 };
770 }
771
772 void negativeLoopedOverObjectIsModified() {
773 Container<ExpensiveToCopyType> Orig;
774 for (const auto &Element : Orig) {
775 const auto Copy = Element;
776 Orig.nonConstMethod();
777 Copy.constMethod();
778 }
779
780 auto Lambda = []() {
781 Container<ExpensiveToCopyType> Orig;
782 for (const auto &Element : Orig) {
783 const auto Copy = Element;
784 Orig.nonConstMethod();
785 Copy.constMethod();
786 }
787 };
788 }
789
790 void negativeReferenceIsInitializedOutsideOfBlock() {
791 ExpensiveToCopyType Orig;
792 const auto &E2 = Orig;
793 if (1 != 2 * 3) {
794 const auto C2 = E2;
795 Orig.nonConstMethod();
796 C2.constMethod();
797 }
798
799 auto Lambda = []() {
800 ExpensiveToCopyType Orig;
801 const auto &E2 = Orig;
802 if (1 != 2 * 3) {
803 const auto C2 = E2;
804 Orig.nonConstMethod();
805 C2.constMethod();
806 }
807 };
808 }
809
810 void negativeStructuredBinding() {
811 // Structured bindings are not yet supported but can trigger false positives
812 // since the DecompositionDecl itself is unused and the check doesn't traverse
813 // VarDecls of the BindingDecls.
814 struct Pair {
815 ExpensiveToCopyType first;
816 ExpensiveToCopyType second;
817 };
818
819 Pair P;
820 const auto [C, D] = P;
821 C.constMethod();
822 D.constMethod();
823 }
824
825 template <typename A>
826 const A &templatedReference();
827
828 template <typename A, typename B>
829 void negativeTemplateTypes() {
830 A Orig;
831 // Different replaced template type params do not trigger the check. In some
832 // template instantiation this might not be a copy but an implicit
833 // conversion, so converting this to a reference might not work.
834 B AmbiguousCopy = Orig;
835 // CHECK-NOT-FIXES: B AmbiguousCopy = Orig;
836
837 B NecessaryCopy = templatedReference<A>();
838 // CHECK-NOT-FIXES: B NecessaryCopy = templatedReference<A>();
839
840 B NecessaryCopy2 = Orig.template templatedAccessor<A>();
841
842 // Non-dependent types in template still trigger the check.
843 const auto UnnecessaryCopy = ExpensiveTypeReference();
844 // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'UnnecessaryCopy' is copy-constructed
845 // CHECK-FIXES: const auto& UnnecessaryCopy = ExpensiveTypeReference();
846 UnnecessaryCopy.constMethod();
847 }
848
849 void instantiateNegativeTemplateTypes() {
850 negativeTemplateTypes<ExpensiveToCopyType, ExpensiveToCopyType>();
851 // This template instantiation would not compile if the `AmbiguousCopy` above was made a reference.
852 negativeTemplateTypes<ExpensiveToCopyType, int>();
853 }
854
855 template <typename A>
856 void positiveSingleTemplateType() {
857 A Orig;
858 A SingleTmplParmTypeCopy = Orig;
859 // CHECK-MESSAGES: [[@LINE-1]]:5: warning: local copy 'SingleTmplParmTypeCopy' of the variable 'Orig' is never modified
860 // CHECK-FIXES: const A& SingleTmplParmTypeCopy = Orig;
861 SingleTmplParmTypeCopy.constMethod();
862
863 A UnnecessaryCopy2 = templatedReference<A>();
864 // CHECK-MESSAGES: [[@LINE-1]]:5: warning: the variable 'UnnecessaryCopy2' is copy-constructed from a const reference
865 // CHECK-FIXES: const A& UnnecessaryCopy2 = templatedReference<A>();
866 UnnecessaryCopy2.constMethod();
867
868 A UnnecessaryCopy3 = Orig.template templatedAccessor<A>();
869 // CHECK-MESSAGES: [[@LINE-1]]:5: warning: the variable 'UnnecessaryCopy3' is copy-constructed from a const reference
870 // CHECK-FIXES: const A& UnnecessaryCopy3 = Orig.template templatedAccessor<A>();
871 UnnecessaryCopy3.constMethod();
872 }
873
874 void instantiatePositiveSingleTemplateType() { positiveSingleTemplateType<ExpensiveToCopyType>(); }
875
876 struct Struct {
877 ExpensiveToCopyType Member;
878 };
879
880 void positiveConstMemberExpr() {
881 Struct Orig;
882 auto UC = Orig;
883 // CHECK-MESSAGES: [[@LINE-1]]:8: warning: local copy 'UC'
884 // CHECK-FIXES: const auto& UC = Orig;
885 const auto &ConstRef = UC.Member;
886 auto MemberCopy = UC.Member;
887 bool b = UC.Member.constMethod();
888 useByValue(UC.Member);
889 useAsConstReference(UC.Member);
890 useByValue(UC.Member);
891 }
892
893 void negativeNonConstMemberExpr() {
894 Struct Orig;
895 {
896 auto Copy = Orig;
897 Copy.Member.nonConstMethod();
898 }
899 {
900 auto Copy = Orig;
901 mutate(Copy.Member);
902 }
903 {
904 auto Copy = Orig;
905 mutate(&Copy.Member);
906 }
907 }
908
909
910 bool operator==(ExpensiveToCopyType, ExpensiveToCopyType);
911
912 template<typename T> bool OperatorWithNoDirectCallee(T t) {
913 ExpensiveToCopyType a1;
914 ExpensiveToCopyType a2 = a1;
915 return a1 == t;
916 }
917