File: unnecessary-copy-initialization.cpp

package info (click to toggle)
llvm-toolchain-17 1%3A17.0.6-22
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,799,624 kB
  • sloc: cpp: 6,428,607; ansic: 1,383,196; asm: 793,408; python: 223,504; objc: 75,364; f90: 60,502; lisp: 33,869; pascal: 15,282; sh: 9,684; perl: 7,453; ml: 4,937; awk: 3,523; makefile: 2,889; javascript: 2,149; xml: 888; fortran: 619; cs: 573
file content (878 lines) | stat: -rw-r--r-- 31,810 bytes parent folder | download | duplicates (3)
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
// RUN: %check_clang_tidy -std=c++17-or-later %s performance-unnecessary-copy-initialization %t

template <typename T>
struct Iterator {
  void operator++();
  T &operator*() const;
  bool operator!=(const Iterator &) const;
  typedef const T &const_reference;
};

template <typename T>
struct ConstIterator {
  void operator++();
  const T &operator*() const;
  bool operator!=(const ConstIterator &) const;
  typedef const T &const_reference;
};

struct ExpensiveToCopyType {
  ExpensiveToCopyType();
  virtual ~ExpensiveToCopyType();
  const ExpensiveToCopyType &reference() const;
  using ConstRef = const ExpensiveToCopyType &;
  ConstRef referenceWithAlias() const;
  const ExpensiveToCopyType *pointer() const;
  void nonConstMethod();
  bool constMethod() const;
  template <typename A>
  const A &templatedAccessor() const;
  operator int() const; // Implicit conversion to int.
};

template <typename T>
struct Container {
  bool empty() const;
  const T& operator[](int) const;
  const T& operator[](int);
  Iterator<T> begin();
  Iterator<T> end();
  ConstIterator<T> begin() const;
  ConstIterator<T> end() const;
  void nonConstMethod();
  bool constMethod() const;
};

using ExpensiveToCopyContainerAlias = Container<ExpensiveToCopyType>;

struct TrivialToCopyType {
  const TrivialToCopyType &reference() const;
};

struct WeirdCopyCtorType {
  WeirdCopyCtorType();
  WeirdCopyCtorType(const WeirdCopyCtorType &w, bool oh_yes = true);

  void nonConstMethod();
  bool constMethod() const;
};

ExpensiveToCopyType global_expensive_to_copy_type;

const ExpensiveToCopyType &ExpensiveTypeReference();
const ExpensiveToCopyType &freeFunctionWithArg(const ExpensiveToCopyType &);
const ExpensiveToCopyType &freeFunctionWithDefaultArg(
    const ExpensiveToCopyType *arg = nullptr);
const TrivialToCopyType &TrivialTypeReference();

void mutate(ExpensiveToCopyType &);
void mutate(ExpensiveToCopyType *);
void useAsConstPointer(const ExpensiveToCopyType *);
void useAsConstReference(const ExpensiveToCopyType &);
void useByValue(ExpensiveToCopyType);

void PositiveFunctionCall() {
  const auto AutoAssigned = ExpensiveTypeReference();
  // 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]
  // CHECK-FIXES: const auto& AutoAssigned = ExpensiveTypeReference();
  AutoAssigned.constMethod();

  const auto AutoCopyConstructed(ExpensiveTypeReference());
  // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoCopyConstructed'
  // CHECK-FIXES: const auto& AutoCopyConstructed(ExpensiveTypeReference());
  AutoCopyConstructed.constMethod();

  const ExpensiveToCopyType VarAssigned = ExpensiveTypeReference();
  // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable 'VarAssigned'
  // CHECK-FIXES:   const ExpensiveToCopyType& VarAssigned = ExpensiveTypeReference();
  VarAssigned.constMethod();

  const ExpensiveToCopyType VarCopyConstructed(ExpensiveTypeReference());
  // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable 'VarCopyConstructed'
  // CHECK-FIXES: const ExpensiveToCopyType& VarCopyConstructed(ExpensiveTypeReference());
  VarCopyConstructed.constMethod();
}

void PositiveMethodCallConstReferenceParam(const ExpensiveToCopyType &Obj) {
  const auto AutoAssigned = Obj.reference();
  // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoAssigned'
  // CHECK-FIXES: const auto& AutoAssigned = Obj.reference();
  AutoAssigned.constMethod();

  const auto AutoCopyConstructed(Obj.reference());
  // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoCopyConstructed'
  // CHECK-FIXES: const auto& AutoCopyConstructed(Obj.reference());
  AutoCopyConstructed.constMethod();

  const ExpensiveToCopyType VarAssigned = Obj.reference();
  // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable 'VarAssigned'
  // CHECK-FIXES: const ExpensiveToCopyType& VarAssigned = Obj.reference();
  VarAssigned.constMethod();

  const ExpensiveToCopyType VarCopyConstructed(Obj.reference());
  // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable 'VarCopyConstructed'
  // CHECK-FIXES: const ExpensiveToCopyType& VarCopyConstructed(Obj.reference());
  VarCopyConstructed.constMethod();
}

void PositiveMethodCallConstParam(const ExpensiveToCopyType Obj) {
  const auto AutoAssigned = Obj.reference();
  // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoAssigned'
  // CHECK-FIXES: const auto& AutoAssigned = Obj.reference();
  AutoAssigned.constMethod();

  const auto AutoCopyConstructed(Obj.reference());
  // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoCopyConstructed'
  // CHECK-FIXES: const auto& AutoCopyConstructed(Obj.reference());
  AutoCopyConstructed.constMethod();

  const ExpensiveToCopyType VarAssigned = Obj.reference();
  // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable 'VarAssigned'
  // CHECK-FIXES: const ExpensiveToCopyType& VarAssigned = Obj.reference();
  VarAssigned.constMethod();

  const ExpensiveToCopyType VarCopyConstructed(Obj.reference());
  // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable 'VarCopyConstructed'
  // CHECK-FIXES: const ExpensiveToCopyType& VarCopyConstructed(Obj.reference());
  VarCopyConstructed.constMethod();
}

void PositiveMethodCallConstPointerParam(const ExpensiveToCopyType *const Obj) {
  const auto AutoAssigned = Obj->reference();
  // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoAssigned'
  // CHECK-FIXES: const auto& AutoAssigned = Obj->reference();
  AutoAssigned.constMethod();

  const auto AutoCopyConstructed(Obj->reference());
  // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoCopyConstructed'
  // CHECK-FIXES: const auto& AutoCopyConstructed(Obj->reference());
  AutoCopyConstructed.constMethod();

  const ExpensiveToCopyType VarAssigned = Obj->reference();
  // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable 'VarAssigned'
  // CHECK-FIXES: const ExpensiveToCopyType& VarAssigned = Obj->reference();
  VarAssigned.constMethod();

  const ExpensiveToCopyType VarCopyConstructed(Obj->reference());
  // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable 'VarCopyConstructed'
  // CHECK-FIXES: const ExpensiveToCopyType& VarCopyConstructed(Obj->reference());
  VarCopyConstructed.constMethod();
}

void PositiveOperatorCallConstReferenceParam(const Container<ExpensiveToCopyType> &C) {
  const auto AutoAssigned = C[42];
  // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoAssigned'
  // CHECK-FIXES: const auto& AutoAssigned = C[42];
  AutoAssigned.constMethod();

  const auto AutoCopyConstructed(C[42]);
  // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoCopyConstructed'
  // CHECK-FIXES: const auto& AutoCopyConstructed(C[42]);
  AutoCopyConstructed.constMethod();

  const ExpensiveToCopyType VarAssigned = C[42];
  // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable 'VarAssigned'
  // CHECK-FIXES: const ExpensiveToCopyType& VarAssigned = C[42];
  VarAssigned.constMethod();

  const ExpensiveToCopyType VarCopyConstructed(C[42]);
  // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable 'VarCopyConstructed'
  // CHECK-FIXES: const ExpensiveToCopyType& VarCopyConstructed(C[42]);
  VarCopyConstructed.constMethod();
}

void PositiveOperatorCallConstValueParam(const Container<ExpensiveToCopyType> C) {
  const auto AutoAssigned = C[42];
  // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoAssigned'
  // CHECK-FIXES: const auto& AutoAssigned = C[42];
  AutoAssigned.constMethod();

  const auto AutoCopyConstructed(C[42]);
  // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoCopyConstructed'
  // CHECK-FIXES: const auto& AutoCopyConstructed(C[42]);
  AutoCopyConstructed.constMethod();

  const ExpensiveToCopyType VarAssigned = C[42];
  // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable 'VarAssigned'
  // CHECK-FIXES: const ExpensiveToCopyType& VarAssigned = C[42];
  VarAssigned.constMethod();

  const ExpensiveToCopyType VarCopyConstructed(C[42]);
  // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable 'VarCopyConstructed'
  // CHECK-FIXES: const ExpensiveToCopyType& VarCopyConstructed(C[42]);
  VarCopyConstructed.constMethod();
}

void PositiveOperatorCallConstValueParamAlias(const ExpensiveToCopyContainerAlias C) {
  const auto AutoAssigned = C[42];
  // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoAssigned'
  // CHECK-FIXES: const auto& AutoAssigned = C[42];
  AutoAssigned.constMethod();

  const auto AutoCopyConstructed(C[42]);
  // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoCopyConstructed'
  // CHECK-FIXES: const auto& AutoCopyConstructed(C[42]);
  AutoCopyConstructed.constMethod();

  const ExpensiveToCopyType VarAssigned = C[42];
  // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable 'VarAssigned'
  // CHECK-FIXES: const ExpensiveToCopyType& VarAssigned = C[42];
  VarAssigned.constMethod();

  const ExpensiveToCopyType VarCopyConstructed(C[42]);
  // CHECK-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable 'VarCopyConstructed'
  // CHECK-FIXES: const ExpensiveToCopyType& VarCopyConstructed(C[42]);
  VarCopyConstructed.constMethod();
}

void PositiveOperatorCallConstValueParam(const Container<ExpensiveToCopyType>* C) {
  const auto AutoAssigned = (*C)[42];
  // TODO-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoAssigned'
  // TODO-FIXES: const auto& AutoAssigned = (*C)[42];
  AutoAssigned.constMethod();

  const auto AutoCopyConstructed((*C)[42]);
  // TODO-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoCopyConstructed'
  // TODO-FIXES: const auto& AutoCopyConstructed((*C)[42]);
  AutoCopyConstructed.constMethod();

  const ExpensiveToCopyType VarAssigned = C->operator[](42);
  // TODO-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable 'VarAssigned'
  // TODO-FIXES: const ExpensiveToCopyType& VarAssigned = C->operator[](42);
  VarAssigned.constMethod();

  const ExpensiveToCopyType VarCopyConstructed(C->operator[](42));
  // TODO-MESSAGES: [[@LINE-1]]:29: warning: the const qualified variable 'VarCopyConstructed'
  // TODO-FIXES: const ExpensiveToCopyType& VarCopyConstructed(C->operator[](42));
  VarCopyConstructed.constMethod();
}

void PositiveLocalConstValue() {
  const ExpensiveToCopyType Obj;
  const auto UnnecessaryCopy = Obj.reference();
  // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'UnnecessaryCopy'
  // CHECK-FIXES: const auto& UnnecessaryCopy = Obj.reference();
  UnnecessaryCopy.constMethod();
}

void PositiveLocalConstRef() {
  const ExpensiveToCopyType Obj;
  const ExpensiveToCopyType &ConstReference = Obj.reference();
  const auto UnnecessaryCopy = ConstReference.reference();
  // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'UnnecessaryCopy'
  // CHECK-FIXES: const auto& UnnecessaryCopy = ConstReference.reference();
  UnnecessaryCopy.constMethod();
}

void PositiveLocalConstPointer() {
  const ExpensiveToCopyType Obj;
  const ExpensiveToCopyType *const ConstPointer = &Obj;
  const auto UnnecessaryCopy = ConstPointer->reference();
  // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'UnnecessaryCopy'
  // CHECK-FIXES: const auto& UnnecessaryCopy = ConstPointer->reference();
  UnnecessaryCopy.constMethod();
}

void NegativeFunctionCallTrivialType() {
  const auto AutoAssigned = TrivialTypeReference();
  const auto AutoCopyConstructed(TrivialTypeReference());
  const TrivialToCopyType VarAssigned = TrivialTypeReference();
  const TrivialToCopyType VarCopyConstructed(TrivialTypeReference());
}

void NegativeStaticLocalVar(const ExpensiveToCopyType &Obj) {
  static const auto StaticVar = Obj.reference();
}

void PositiveFunctionCallExpensiveTypeNonConstVariable() {
  auto AutoAssigned = ExpensiveTypeReference();
  // 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]
  // CHECK-FIXES: const auto& AutoAssigned = ExpensiveTypeReference();
  AutoAssigned.constMethod();

  auto AutoCopyConstructed(ExpensiveTypeReference());
  // CHECK-MESSAGES: [[@LINE-1]]:8: warning: the variable 'AutoCopyConstructed'
  // CHECK-FIXES: const auto& AutoCopyConstructed(ExpensiveTypeReference());
  AutoCopyConstructed.constMethod();

  ExpensiveToCopyType VarAssigned = ExpensiveTypeReference();
  // CHECK-MESSAGES: [[@LINE-1]]:23: warning: the variable 'VarAssigned'
  // CHECK-FIXES: const ExpensiveToCopyType& VarAssigned = ExpensiveTypeReference();
  VarAssigned.constMethod();

  ExpensiveToCopyType VarCopyConstructed(ExpensiveTypeReference());
  // CHECK-MESSAGES: [[@LINE-1]]:23: warning: the variable 'VarCopyConstructed'
  // CHECK-FIXES: const ExpensiveToCopyType& VarCopyConstructed(ExpensiveTypeReference());
  VarCopyConstructed.constMethod();
}

void positiveNonConstVarInCodeBlock(const ExpensiveToCopyType &Obj) {
  {
    auto Assigned = Obj.reference();
    // CHECK-MESSAGES: [[@LINE-1]]:10: warning: the variable 'Assigned'
    // CHECK-FIXES: const auto& Assigned = Obj.reference();
    Assigned.reference();
    useAsConstReference(Assigned);
    useByValue(Assigned);
  }
}

void positiveNonConstVarInCodeBlockWithAlias(const ExpensiveToCopyType &Obj) {
  {
    const ExpensiveToCopyType Assigned = Obj.referenceWithAlias();
    // CHECK-MESSAGES: [[@LINE-1]]:31: warning: the const qualified variable
    // CHECK-FIXES: const ExpensiveToCopyType& Assigned = Obj.referenceWithAlias();
    useAsConstReference(Assigned);
  }
}

void negativeNonConstVarWithNonConstUse(const ExpensiveToCopyType &Obj) {
  {
    auto NonConstInvoked = Obj.reference();
    // CHECK-FIXES: auto NonConstInvoked = Obj.reference();
    NonConstInvoked.nonConstMethod();
  }
  {
    auto Reassigned = Obj.reference();
    // CHECK-FIXES: auto Reassigned = Obj.reference();
    Reassigned = ExpensiveToCopyType();
  }
  {
    auto MutatedByReference = Obj.reference();
    // CHECK-FIXES: auto MutatedByReference = Obj.reference();
    mutate(MutatedByReference);
  }
  {
    auto MutatedByPointer = Obj.reference();
    // CHECK-FIXES: auto MutatedByPointer = Obj.reference();
    mutate(&MutatedByPointer);
  }
}

void PositiveMethodCallNonConstRefNotModified(ExpensiveToCopyType &Obj) {
  const auto AutoAssigned = Obj.reference();
  // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoAssigned'
  // CHECK-FIXES: const auto& AutoAssigned = Obj.reference();
  AutoAssigned.constMethod();
}

void NegativeMethodCallNonConstRefIsModified(ExpensiveToCopyType &Obj) {
  const auto AutoAssigned = Obj.reference();
  const auto AutoCopyConstructed(Obj.reference());
  const ExpensiveToCopyType VarAssigned = Obj.reference();
  const ExpensiveToCopyType VarCopyConstructed(Obj.reference());
  mutate(&Obj);
}

void PositiveMethodCallNonConstNotModified(ExpensiveToCopyType Obj) {
  const auto AutoAssigned = Obj.reference();
  // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoAssigned'
  // CHECK-FIXES: const auto& AutoAssigned = Obj.reference();
  AutoAssigned.constMethod();
}

void NegativeMethodCallNonConstValueArgumentIsModified(ExpensiveToCopyType Obj) {
  Obj.nonConstMethod();
  const auto AutoAssigned = Obj.reference();
}

void PositiveMethodCallNonConstPointerNotModified(ExpensiveToCopyType *const Obj) {
  const auto AutoAssigned = Obj->reference();
  // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoAssigned'
  // CHECK-FIXES: const auto& AutoAssigned = Obj->reference();
  Obj->constMethod();
  AutoAssigned.constMethod();
}

void NegativeMethodCallNonConstPointerIsModified(ExpensiveToCopyType *const Obj) {
  const auto AutoAssigned = Obj->reference();
  const auto AutoCopyConstructed(Obj->reference());
  const ExpensiveToCopyType VarAssigned = Obj->reference();
  const ExpensiveToCopyType VarCopyConstructed(Obj->reference());
  mutate(Obj);
}

void PositiveLocalVarIsNotModified() {
  ExpensiveToCopyType LocalVar;
  const auto AutoAssigned = LocalVar.reference();
  // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'AutoAssigned'
  // CHECK-FIXES: const auto& AutoAssigned = LocalVar.reference();
  AutoAssigned.constMethod();
}

void NegativeLocalVarIsModified() {
  ExpensiveToCopyType Obj;
  const auto AutoAssigned = Obj.reference();
  Obj = AutoAssigned;
}

struct NegativeConstructor {
  NegativeConstructor(const ExpensiveToCopyType &Obj) : Obj(Obj) {}
  ExpensiveToCopyType Obj;
};

#define UNNECESSARY_COPY_INIT_IN_MACRO_BODY(TYPE)                              \
  void functionWith##TYPE(const TYPE &T) {                                     \
    auto AssignedInMacro = T.reference();                                      \
  }                                                                            \
// Ensure fix is not applied.
// CHECK-FIXES: auto AssignedInMacro = T.reference();

UNNECESSARY_COPY_INIT_IN_MACRO_BODY(ExpensiveToCopyType)
// CHECK-MESSAGES: [[@LINE-1]]:1: warning: the variable 'AssignedInMacro' is copy-constructed

#define UNNECESSARY_COPY_INIT_IN_MACRO_ARGUMENT(ARGUMENT) ARGUMENT

void PositiveMacroArgument(const ExpensiveToCopyType &Obj) {
  UNNECESSARY_COPY_INIT_IN_MACRO_ARGUMENT(auto CopyInMacroArg = Obj.reference());
  // CHECK-MESSAGES: [[@LINE-1]]:48: warning: the variable 'CopyInMacroArg' is copy-constructed
  // Ensure fix is not applied.
  // CHECK-FIXES: auto CopyInMacroArg = Obj.reference()
  CopyInMacroArg.constMethod();
}

void PositiveLocalCopyConstMethodInvoked() {
  ExpensiveToCopyType orig;
  ExpensiveToCopyType copy_1 = orig;
  // 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]
  // CHECK-FIXES: const ExpensiveToCopyType& copy_1 = orig;
  copy_1.constMethod();
  orig.constMethod();
}

void PositiveLocalCopyUsingExplicitCopyCtor() {
  ExpensiveToCopyType orig;
  ExpensiveToCopyType copy_2(orig);
  // CHECK-MESSAGES: [[@LINE-1]]:23: warning: local copy 'copy_2'
  // CHECK-FIXES: const ExpensiveToCopyType& copy_2(orig);
  copy_2.constMethod();
  orig.constMethod();
}

void PositiveLocalCopyCopyIsArgument(const ExpensiveToCopyType &orig) {
  ExpensiveToCopyType copy_3 = orig;
  // CHECK-MESSAGES: [[@LINE-1]]:23: warning: local copy 'copy_3'
  // CHECK-FIXES: const ExpensiveToCopyType& copy_3 = orig;
  copy_3.constMethod();
}

void PositiveLocalCopyUsedAsConstRef() {
  ExpensiveToCopyType orig;
  ExpensiveToCopyType copy_4 = orig;
  // CHECK-MESSAGES: [[@LINE-1]]:23: warning: local copy 'copy_4'
  // CHECK-FIXES: const ExpensiveToCopyType& copy_4 = orig;
  useAsConstReference(orig);
  copy_4.constMethod();
}

void PositiveLocalCopyTwice() {
  ExpensiveToCopyType orig;
  ExpensiveToCopyType copy_5 = orig;
  // CHECK-MESSAGES: [[@LINE-1]]:23: warning: local copy 'copy_5'
  // CHECK-FIXES: const ExpensiveToCopyType& copy_5 = orig;
  ExpensiveToCopyType copy_6 = copy_5;
  // CHECK-MESSAGES: [[@LINE-1]]:23: warning: local copy 'copy_6'
  // CHECK-FIXES: const ExpensiveToCopyType& copy_6 = copy_5;
  copy_5.constMethod();
  copy_6.constMethod();
  orig.constMethod();
}


void PositiveLocalCopyWeirdCopy() {
  WeirdCopyCtorType orig;
  WeirdCopyCtorType weird_1(orig);
  // CHECK-MESSAGES: [[@LINE-1]]:21: warning: local copy 'weird_1'
  // CHECK-FIXES: const WeirdCopyCtorType& weird_1(orig);
  weird_1.constMethod();

  WeirdCopyCtorType weird_2 = orig;
  // CHECK-MESSAGES: [[@LINE-1]]:21: warning: local copy 'weird_2'
  // CHECK-FIXES: const WeirdCopyCtorType& weird_2 = orig;
  weird_2.constMethod();
}

void NegativeLocalCopySimpleTypes() {
  int i1 = 0;
  int i2 = i1;
}

void NegativeLocalCopyCopyIsModified() {
  ExpensiveToCopyType orig;
  ExpensiveToCopyType neg_copy_1 = orig;
  neg_copy_1.nonConstMethod();
}

void NegativeLocalCopyOriginalIsModified() {
  ExpensiveToCopyType orig;
  ExpensiveToCopyType neg_copy_2 = orig;
  orig.nonConstMethod();
}

void NegativeLocalCopyUsedAsRefArg() {
  ExpensiveToCopyType orig;
  ExpensiveToCopyType neg_copy_3 = orig;
  mutate(neg_copy_3);
}

void NegativeLocalCopyUsedAsPointerArg() {
  ExpensiveToCopyType orig;
  ExpensiveToCopyType neg_copy_4 = orig;
  mutate(&neg_copy_4);
}

void NegativeLocalCopyCopyFromGlobal() {
  ExpensiveToCopyType neg_copy_5 = global_expensive_to_copy_type;
}

void NegativeLocalCopyCopyToStatic() {
  ExpensiveToCopyType orig;
  static ExpensiveToCopyType neg_copy_6 = orig;
}

void NegativeLocalCopyNonConstInForLoop() {
  ExpensiveToCopyType orig;
  for (ExpensiveToCopyType neg_copy_7 = orig; orig.constMethod();
       orig.nonConstMethod()) {
    orig.constMethod();
  }
}

void NegativeLocalCopyWeirdNonCopy() {
  WeirdCopyCtorType orig;
  WeirdCopyCtorType neg_weird_1(orig, false);
  WeirdCopyCtorType neg_weird_2(orig, true);
}
void WarningOnlyMultiDeclStmt() {
  ExpensiveToCopyType orig;
  ExpensiveToCopyType copy = orig, copy2;
  // CHECK-MESSAGES: [[@LINE-1]]:23: warning: local copy 'copy' of the variable 'orig' is never modified; consider avoiding the copy [performance-unnecessary-copy-initialization]
  // CHECK-FIXES: ExpensiveToCopyType copy = orig, copy2;
  copy.constMethod();
}

class Element {};

void implicitVarFalsePositive() {
  for (const Element &E : Container<Element>()) {
  }
}

// This should not trigger the check as the argument could introduce an alias.
void negativeInitializedFromFreeFunctionWithArg() {
  ExpensiveToCopyType Orig;
  const ExpensiveToCopyType Copy = freeFunctionWithArg(Orig);
}

void negativeInitializedFromFreeFunctionWithDefaultArg() {
  const ExpensiveToCopyType Copy = freeFunctionWithDefaultArg();
}

void negativeInitialzedFromFreeFunctionWithNonDefaultArg() {
  ExpensiveToCopyType Orig;
  const ExpensiveToCopyType Copy = freeFunctionWithDefaultArg(&Orig);
}

namespace std {
inline namespace __1 {

template <class>
class function;
template <class R, class... ArgTypes>
class function<R(ArgTypes...)> {
public:
  function();
  function(const function &Other);
  R operator()(ArgTypes... Args) const;
};

} // namespace __1
} // namespace std

void negativeStdFunction() {
  std::function<int()> Orig;
  std::function<int()> Copy = Orig;
  int i = Orig();
}

using Functor = std::function<int()>;

void negativeAliasedStdFunction() {
  Functor Orig;
  Functor Copy = Orig;
  int i = Orig();
}

typedef std::function<int()> TypedefFunc;

void negativeTypedefedStdFunction() {
  TypedefFunc Orig;
  TypedefFunc Copy = Orig;
  int i = Orig();
}

namespace fake {
namespace std {
template <class R, class... Args>
struct function {
  // Custom copy constructor makes it expensive to copy;
  function(const function &);
  void constMethod() const;
};
} // namespace std

void positiveFakeStdFunction(std::function<void(int)> F) {
  auto Copy = F;
  // CHECK-MESSAGES: [[@LINE-1]]:8: warning: local copy 'Copy' of the variable 'F' is never modified;
  // CHECK-FIXES: const auto& Copy = F;
  Copy.constMethod();
}

} // namespace fake

void positiveInvokedOnStdFunction(
    std::function<void(const ExpensiveToCopyType &)> Update,
    const ExpensiveToCopyType Orig) {
  auto Copy = Orig.reference();
  // CHECK-MESSAGES: [[@LINE-1]]:8: warning: the variable 'Copy' is copy-constructed from a const reference
  // CHECK-FIXES: const auto& Copy = Orig.reference();
  Update(Copy);
}

void negativeInvokedOnStdFunction(
    std::function<void(ExpensiveToCopyType &)> Update,
    const ExpensiveToCopyType Orig) {
  auto Copy = Orig.reference();
  Update(Copy);
}

void negativeCopiedFromReferenceToModifiedVar() {
  ExpensiveToCopyType Orig;
  const auto &Ref = Orig;
  const auto NecessaryCopy = Ref;
  Orig.nonConstMethod();
}

void positiveCopiedFromReferenceToConstVar() {
  ExpensiveToCopyType Orig;
  const auto &Ref = Orig;
  const auto UnnecessaryCopy = Ref;
  // CHECK-MESSAGES: [[@LINE-1]]:14: warning: local copy 'UnnecessaryCopy' of
  // CHECK-FIXES: const auto& UnnecessaryCopy = Ref;
  Orig.constMethod();
  UnnecessaryCopy.constMethod();
}

void negativeCopiedFromGetterOfReferenceToModifiedVar() {
  ExpensiveToCopyType Orig;
  const auto &Ref = Orig.reference();
  const auto NecessaryCopy = Ref.reference();
  Orig.nonConstMethod();
}

void negativeAliasNonCanonicalPointerType() {
  ExpensiveToCopyType Orig;
  // The use of auto here hides that the type is a pointer type. The check needs
  // to look at the canonical type to detect the aliasing through this pointer.
  const auto Pointer = Orig.pointer();
  const auto NecessaryCopy = Pointer->reference();
  Orig.nonConstMethod();
}

void negativeAliasTypedefedType() {
  typedef const ExpensiveToCopyType &ReferenceType;
  ExpensiveToCopyType Orig;
  // The typedef hides the fact that this is a reference type. The check needs
  // to look at the canonical type to detect the aliasing.
  ReferenceType Ref = Orig.reference();
  const auto NecessaryCopy = Ref.reference();
  Orig.nonConstMethod();
}

void positiveCopiedFromGetterOfReferenceToConstVar() {
  ExpensiveToCopyType Orig;
  const auto &Ref = Orig.reference();
  auto UnnecessaryCopy = Ref.reference();
  // CHECK-MESSAGES: [[@LINE-1]]:8: warning: the variable 'UnnecessaryCopy' is
  // CHECK-FIXES: const auto& UnnecessaryCopy = Ref.reference();
  Orig.constMethod();
  UnnecessaryCopy.constMethod();
}

void positiveUnusedReferenceIsRemoved() {
  // clang-format off
  const auto AutoAssigned = ExpensiveTypeReference(); int i = 0; // Foo bar.
  // 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]
  // CHECK-FIXES-NOT: const auto AutoAssigned = ExpensiveTypeReference();
  // CHECK-FIXES: int i = 0; // Foo bar.
  auto TrailingCommentRemoved = ExpensiveTypeReference(); // Trailing comment.
  // CHECK-MESSAGES: [[@LINE-1]]:8: warning: the variable 'TrailingCommentRemoved' is copy-constructed from a const reference but is never used;
  // CHECK-FIXES-NOT: auto TrailingCommentRemoved = ExpensiveTypeReference();
  // CHECK-FIXES-NOT: // Trailing comment.
  // clang-format on

  auto UnusedAndUnnecessary = ExpensiveTypeReference();
  // Comments on a new line should not be deleted.
  // CHECK-MESSAGES: [[@LINE-2]]:8: warning: the variable 'UnusedAndUnnecessary' is copy-constructed
  // CHECK-FIXES-NOT: auto UnusedAndUnnecessary = ExpensiveTypeReference();
  // CHECK-FIXES: // Comments on a new line should not be deleted.
}

void positiveLoopedOverObjectIsConst() {
  const Container<ExpensiveToCopyType> Orig;
  for (const auto &Element : Orig) {
    const auto Copy = Element;
    // CHECK-MESSAGES: [[@LINE-1]]:16: warning: local copy 'Copy'
    // CHECK-FIXES: const auto& Copy = Element;
    Orig.constMethod();
    Copy.constMethod();
  }

  auto Lambda = []() {
    const Container<ExpensiveToCopyType> Orig;
    for (const auto &Element : Orig) {
      const auto Copy = Element;
      // CHECK-MESSAGES: [[@LINE-1]]:18: warning: local copy 'Copy'
      // CHECK-FIXES: const auto& Copy = Element;
      Orig.constMethod();
      Copy.constMethod();
    }
  };
}

void negativeLoopedOverObjectIsModified() {
  Container<ExpensiveToCopyType> Orig;
  for (const auto &Element : Orig) {
    const auto Copy = Element;
    Orig.nonConstMethod();
    Copy.constMethod();
  }

  auto Lambda = []() {
    Container<ExpensiveToCopyType> Orig;
    for (const auto &Element : Orig) {
      const auto Copy = Element;
      Orig.nonConstMethod();
      Copy.constMethod();
    }
  };
}

void negativeReferenceIsInitializedOutsideOfBlock() {
  ExpensiveToCopyType Orig;
  const auto &E2 = Orig;
  if (1 != 2 * 3) {
    const auto C2 = E2;
    Orig.nonConstMethod();
    C2.constMethod();
  }

  auto Lambda = []() {
    ExpensiveToCopyType Orig;
    const auto &E2 = Orig;
    if (1 != 2 * 3) {
      const auto C2 = E2;
      Orig.nonConstMethod();
      C2.constMethod();
    }
  };
}

void negativeStructuredBinding() {
  // Structured bindings are not yet supported but can trigger false positives
  // since the DecompositionDecl itself is unused and the check doesn't traverse
  // VarDecls of the BindingDecls.
  struct Pair {
    ExpensiveToCopyType first;
    ExpensiveToCopyType second;
  };

  Pair P;
  const auto [C, D] = P;
  C.constMethod();
  D.constMethod();
}

template <typename A>
const A &templatedReference();

template <typename A, typename B>
void negativeTemplateTypes() {
  A Orig;
  // Different replaced template type params do not trigger the check. In some
  // template instantiation this might not be a copy but an implicit
  // conversion, so converting this to a reference might not work.
  B AmbiguousCopy = Orig;
  // CHECK-NOT-FIXES: B AmbiguousCopy = Orig;

  B NecessaryCopy = templatedReference<A>();
  // CHECK-NOT-FIXES: B NecessaryCopy = templatedReference<A>();

  B NecessaryCopy2 = Orig.template templatedAccessor<A>();

  // Non-dependent types in template still trigger the check.
  const auto UnnecessaryCopy = ExpensiveTypeReference();
  // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'UnnecessaryCopy' is copy-constructed
  // CHECK-FIXES: const auto& UnnecessaryCopy = ExpensiveTypeReference();
  UnnecessaryCopy.constMethod();
}

void instantiateNegativeTemplateTypes() {
  negativeTemplateTypes<ExpensiveToCopyType, ExpensiveToCopyType>();
  // This template instantiation would not compile if the `AmbiguousCopy` above was made a reference.
  negativeTemplateTypes<ExpensiveToCopyType, int>();
}

template <typename A>
void positiveSingleTemplateType() {
  A Orig;
  A SingleTmplParmTypeCopy = Orig;
  // CHECK-MESSAGES: [[@LINE-1]]:5: warning: local copy 'SingleTmplParmTypeCopy' of the variable 'Orig' is never modified
  // CHECK-FIXES: const A& SingleTmplParmTypeCopy = Orig;
  SingleTmplParmTypeCopy.constMethod();

  A UnnecessaryCopy2 = templatedReference<A>();
  // CHECK-MESSAGES: [[@LINE-1]]:5: warning: the variable 'UnnecessaryCopy2' is copy-constructed from a const reference
  // CHECK-FIXES: const A& UnnecessaryCopy2 = templatedReference<A>();
  UnnecessaryCopy2.constMethod();

  A UnnecessaryCopy3 = Orig.template templatedAccessor<A>();
  // CHECK-MESSAGES: [[@LINE-1]]:5: warning: the variable 'UnnecessaryCopy3' is copy-constructed from a const reference
  // CHECK-FIXES: const A& UnnecessaryCopy3 = Orig.template templatedAccessor<A>();
  UnnecessaryCopy3.constMethod();
}

void instantiatePositiveSingleTemplateType() { positiveSingleTemplateType<ExpensiveToCopyType>(); }

struct Struct {
  ExpensiveToCopyType Member;
};

void positiveConstMemberExpr() {
  Struct Orig;
  auto UC = Orig;
  // CHECK-MESSAGES: [[@LINE-1]]:8: warning: local copy 'UC'
  // CHECK-FIXES: const auto& UC = Orig;
  const auto &ConstRef = UC.Member;
  auto MemberCopy = UC.Member;
  bool b = UC.Member.constMethod();
  useByValue(UC.Member);
  useAsConstReference(UC.Member);
  useByValue(UC.Member);
}

void negativeNonConstMemberExpr() {
  Struct Orig;
  {
    auto Copy = Orig;
    Copy.Member.nonConstMethod();
  }
  {
    auto Copy = Orig;
    mutate(Copy.Member);
  }
  {
    auto Copy = Orig;
    mutate(&Copy.Member);
  }
}