1// RUN: %check_clang_tidy %s performance-unnecessary-copy-initialization %t -- -config="{CheckOptions: {performance-unnecessary-copy-initialization.AllowedTypes: '[Pp]ointer$;[Pp]tr$;[Rr]ef(erence)?$'}}" --
2
3struct SmartPointer {
4 ~SmartPointer();
5};
6
7struct smart_pointer {
8 ~smart_pointer();
9};
10
11struct SmartPtr {
12 ~SmartPtr();
13};
14
15struct smart_ptr {
16 ~smart_ptr();
17};
18
19struct SmartReference {
20 ~SmartReference();
21};
22
23struct smart_reference {
24 ~smart_reference();
25};
26
27struct SmartRef {
28 ~SmartRef();
29};
30
31struct smart_ref {
32 ~smart_ref();
33};
34
35struct OtherType {
36 ~OtherType();
37 void constMethod() const;
38};
39
40template <typename T> struct SomeComplexTemplate {
41 ~SomeComplexTemplate();
42};
43
44typedef SomeComplexTemplate<int> NotTooComplexRef;
45
46const SmartPointer &getSmartPointer();
47const smart_pointer &get_smart_pointer();
48const SmartPtr &getSmartPtr();
49const smart_ptr &get_smart_ptr();
50const SmartReference &getSmartReference();
51const smart_reference &get_smart_reference();
52const SmartRef &getSmartRef();
53const smart_ref &get_smart_ref();
54const OtherType &getOtherType();
55const NotTooComplexRef &getNotTooComplexRef();
56
57void negativeSmartPointer() {
58 const auto P = getSmartPointer();
59}
60
61void negative_smart_pointer() {
62 const auto p = get_smart_pointer();
63}
64
65void negativeSmartPtr() {
66 const auto P = getSmartPtr();
67}
68
69void negative_smart_ptr() {
70 const auto p = get_smart_ptr();
71}
72
73void negativeSmartReference() {
74 const auto R = getSmartReference();
75}
76
77void negative_smart_reference() {
78 const auto r = get_smart_reference();
79}
80
81void negativeSmartRef() {
82 const auto R = getSmartRef();
83}
84
85void negative_smart_ref() {
86 const auto r = get_smart_ref();
87}
88
89void positiveOtherType() {
90 const auto O = getOtherType();
91 // CHECK-MESSAGES: [[@LINE-1]]:14: warning: the const qualified variable 'O' is copy-constructed from a const reference; consider making it a const reference [performance-unnecessary-copy-initialization]
92 // CHECK-FIXES: const auto& O = getOtherType();
93 O.constMethod();
94}
95
96void negativeNotTooComplexRef() {
97 const NotTooComplexRef R = getNotTooComplexRef();
98 // Using `auto` here would result in the "canonical" type which does not match
99 // the pattern.
100}
101

source code of clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-copy-initialization-allowed-types.cpp