1// RUN: %check_clang_tidy %s bugprone-copy-constructor-init %t
2
3class NonCopyable {
4public:
5 NonCopyable() = default;
6 NonCopyable(const NonCopyable &) = delete;
7
8private:
9 int a;
10};
11
12class NonCopyable2 {
13public:
14 NonCopyable2() = default;
15
16private:
17 NonCopyable2(const NonCopyable2 &);
18 int a;
19};
20
21class Copyable {
22public:
23 Copyable() = default;
24 Copyable(const Copyable &) = default;
25
26private:
27 int a;
28};
29
30class Copyable2 {
31public:
32 Copyable2() = default;
33 Copyable2(const Copyable2 &) = default;
34
35private:
36 int a;
37};
38
39class Copyable3 : public Copyable {
40public:
41 Copyable3() = default;
42 Copyable3(const Copyable3 &) = default;
43};
44
45template <class C>
46class Copyable4 {
47public:
48 Copyable4() = default;
49 Copyable4(const Copyable4 &) = default;
50
51private:
52 int a;
53};
54
55template <class T, class S>
56class Copyable5 {
57public:
58 Copyable5() = default;
59 Copyable5(const Copyable5 &) = default;
60
61private:
62 int a;
63};
64
65class EmptyCopyable {
66public:
67 EmptyCopyable() = default;
68 EmptyCopyable(const EmptyCopyable &) = default;
69};
70
71template <typename T>
72using CopyableAlias = Copyable5<T, int>;
73
74typedef Copyable5<int, int> CopyableAlias2;
75
76class X : public Copyable, public EmptyCopyable {
77 X(const X &other) : Copyable(other) {}
78};
79
80class X2 : public Copyable2 {
81 X2(const X2 &other) {}
82 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: calling a base constructor other than the copy constructor [bugprone-copy-constructor-init]
83 // CHECK-FIXES: X2(const X2 &other) : Copyable2(other) {}
84};
85
86class X2_A : public Copyable2 {
87 X2_A(const X2_A &) {}
88 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: calling a base constructor
89 // CHECK-FIXES: X2_A(const X2_A &) {}
90};
91
92class X3 : public Copyable, public Copyable2 {
93 X3(const X3 &other) : Copyable(other) {}
94 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: calling a base constructor
95 // CHECK-FIXES: X3(const X3 &other) : Copyable(other) {}
96};
97
98class X4 : public Copyable {
99 X4(const X4 &other) : Copyable() {}
100 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: calling a base constructor
101 // CHECK-FIXES: X4(const X4 &other) : Copyable(other) {}
102};
103
104class X5 : public Copyable3 {
105 X5(const X5 &other) {}
106 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: calling a base constructor
107 // CHECK-FIXES: X5(const X5 &other) : Copyable3(other) {}
108};
109
110class X6 : public Copyable2, public Copyable3 {
111 X6(const X6 &other) {}
112 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: calling a base constructor
113 // CHECK-FIXES: X6(const X6 &other) : Copyable2(other), Copyable3(other) {}
114};
115
116class X7 : public Copyable, public Copyable2 {
117 X7(const X7 &other) : Copyable() {}
118 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: calling a base constructor
119 // CHECK-FIXES: X7(const X7 &other) : Copyable(other) {}
120};
121
122class X8 : public Copyable4<int> {
123 X8(const X8 &other) : Copyable4(other) {}
124};
125
126class X9 : public Copyable4<int> {
127 X9(const X9 &other) : Copyable4() {}
128 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: calling a base constructor
129 // CHECK-FIXES: X9(const X9 &other) : Copyable4(other) {}
130};
131
132class X10 : public Copyable4<int> {
133 X10(const X10 &other) {}
134 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: calling a base constructor
135 // CHECK-FIXES: X10(const X10 &other) : Copyable4(other) {}
136};
137
138class X11 : public Copyable5<int, float> {
139 X11(const X11 &other) {}
140 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: calling a base constructor
141 // CHECK-FIXES: X11(const X11 &other) : Copyable5(other) {}
142};
143
144class X12 : public CopyableAlias<float> {
145 X12(const X12 &other) {}
146 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: calling a base constructor
147};
148
149template <typename T>
150class X13 : T {
151 X13(const X13 &other) {}
152};
153
154template class X13<EmptyCopyable>;
155template class X13<Copyable>;
156
157#define FROMMACRO \
158 class X14 : public Copyable2 { \
159 X14(const X14 &other) {} \
160 };
161
162FROMMACRO
163// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: calling a base constructor
164
165class X15 : public CopyableAlias2 {
166 X15(const X15 &other) {}
167 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: calling a base constructor
168 // CHECK-FIXES: X15(const X15 &other) : Copyable5(other) {}
169};
170
171class X16 : public NonCopyable {
172 X16(const X16 &other) {}
173};
174
175class X17 : public NonCopyable2 {
176 X17(const X17 &other) {}
177};
178
179class X18 : private Copyable {
180 X18(const X18 &other) {}
181 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: calling a base constructor
182 // CHECK-FIXES: X18(const X18 &other) : Copyable(other) {}
183};
184
185class X19 : private Copyable {
186 X19(const X19 &other) : Copyable(other) {}
187};
188

source code of clang-tools-extra/test/clang-tidy/checkers/bugprone/copy-constructor-init.cpp