1 // RUN: %check_clang_tidy -std=c++11-or-later %s google-readability-casting %t
2
g()3 bool g() { return false; }
4
5 enum Enum { Enum1 };
6 struct X {};
7 struct Y : public X {};
8
f(int a,double b,const char * cpc,const void * cpv,X * pX)9 void f(int a, double b, const char *cpc, const void *cpv, X *pX) {
10 const char *cpc2 = (const char*)cpc;
11 // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: redundant cast to the same type [google-readability-casting]
12 // CHECK-FIXES: const char *cpc2 = cpc;
13
14 typedef const char *Typedef1;
15 typedef const char *Typedef2;
16 Typedef1 t1;
17 (Typedef2)t1;
18 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: C-style casts are discouraged; use static_cast (if needed, the cast may be redundant) [google-readability-casting]
19 // CHECK-FIXES: {{^}} static_cast<Typedef2>(t1);
20 (const char*)t1;
21 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: {{.*}}; use static_cast (if needed
22 // CHECK-FIXES: {{^}} static_cast<const char*>(t1);
23 (Typedef1)cpc;
24 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: {{.*}}; use static_cast (if needed
25 // CHECK-FIXES: {{^}} static_cast<Typedef1>(cpc);
26 (Typedef1)t1;
27 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: redundant cast to the same type
28 // CHECK-FIXES: {{^}} t1;
29
30 char *pc = (char*)cpc;
31 // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: C-style casts are discouraged; use const_cast [google-readability-casting]
32 // CHECK-FIXES: char *pc = const_cast<char*>(cpc);
33 typedef char Char;
34 Char *pChar = (Char*)pc;
35 // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: {{.*}}; use static_cast (if needed
36 // CHECK-FIXES: {{^}} Char *pChar = static_cast<Char*>(pc);
37
38 (Char)*cpc;
39 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: {{.*}}; use static_cast (if needed
40 // CHECK-FIXES: {{^}} static_cast<Char>(*cpc);
41
42 (char)*pChar;
43 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: {{.*}}; use static_cast (if needed
44 // CHECK-FIXES: {{^}} static_cast<char>(*pChar);
45
46 (const char*)cpv;
47 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: {{.*}}; use static_cast [
48 // CHECK-FIXES: static_cast<const char*>(cpv);
49
50 char *pc2 = (char*)(cpc + 33);
51 // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: {{.*}}; use const_cast [
52 // CHECK-FIXES: char *pc2 = const_cast<char*>(cpc + 33);
53
54 const char &crc = *cpc;
55 char &rc = (char&)crc;
56 // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: {{.*}}; use const_cast [
57 // CHECK-FIXES: char &rc = const_cast<char&>(crc);
58
59 char &rc2 = (char&)*cpc;
60 // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: {{.*}}; use const_cast [
61 // CHECK-FIXES: char &rc2 = const_cast<char&>(*cpc);
62
63 char ** const* const* ppcpcpc;
64 char ****ppppc = (char****)ppcpcpc;
65 // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: {{.*}}; use const_cast [
66 // CHECK-FIXES: char ****ppppc = const_cast<char****>(ppcpcpc);
67
68 char ***pppc = (char***)*(ppcpcpc);
69 // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: {{.*}}; use const_cast [
70 // CHECK-FIXES: char ***pppc = const_cast<char***>(*(ppcpcpc));
71
72 char ***pppc2 = (char***)(*ppcpcpc);
73 // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: {{.*}}; use const_cast [
74 // CHECK-FIXES: char ***pppc2 = const_cast<char***>(*ppcpcpc);
75
76 char *pc5 = (char*)(const char*)(cpv);
77 // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: {{.*}}; use const_cast [
78 // CHECK-MESSAGES: :[[@LINE-2]]:22: warning: {{.*}}; use static_cast [
79 // CHECK-FIXES: char *pc5 = const_cast<char*>(static_cast<const char*>(cpv));
80
81 int b1 = (int)b;
82 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: {{.*}}; use static_cast [
83 // CHECK-FIXES: int b1 = static_cast<int>(b);
84 b1 = (const int&)b;
85 // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: {{.*}}; use static_cast/const_cast/reinterpret_cast [
86 // CHECK-FIXES: b1 = (const int&)b;
87
88 b1 = (int) b;
89 // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: {{.*}}; use static_cast {{.*}}
90 // CHECK-FIXES: b1 = static_cast<int>(b);
91
92 b1 = (int) b;
93 // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: {{.*}}; use static_cast {{.*}}
94 // CHECK-FIXES: b1 = static_cast<int>(b);
95
96 b1 = (int) (b);
97 // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: {{.*}}; use static_cast {{.*}}
98 // CHECK-FIXES: b1 = static_cast<int>(b);
99
100 b1 = (int) (b);
101 // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: {{.*}}; use static_cast {{.*}}
102 // CHECK-FIXES: b1 = static_cast<int>(b);
103
104 Y *pB = (Y*)pX;
105 // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: {{.*}}; use static_cast/const_cast/reinterpret_cast [
106 Y &rB = (Y&)*pX;
107 // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: {{.*}}; use static_cast/const_cast/reinterpret_cast [
108
109 const char *pc3 = (const char*)cpv;
110 // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: {{.*}}; use static_cast [
111 // CHECK-FIXES: const char *pc3 = static_cast<const char*>(cpv);
112
113 char *pc4 = (char*)cpv;
114 // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: {{.*}}; use static_cast/const_cast/reinterpret_cast [
115 // CHECK-FIXES: char *pc4 = (char*)cpv;
116
117 b1 = (int)Enum1;
118 // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: {{.*}}; use static_cast [
119 // CHECK-FIXES: b1 = static_cast<int>(Enum1);
120
121 Enum e = (Enum)b1;
122 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: {{.*}}; use static_cast [
123 // CHECK-FIXES: Enum e = static_cast<Enum>(b1);
124
125 e = (Enum)Enum1;
126 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant cast to the same type
127 // CHECK-FIXES: {{^}} e = Enum1;
128
129 e = (Enum)e;
130 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant cast to the same type
131 // CHECK-FIXES: {{^}} e = e;
132
133 e = (Enum) e;
134 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant cast to the same type
135 // CHECK-FIXES: {{^}} e = e;
136
137 e = (Enum) (e);
138 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: redundant cast to the same type
139 // CHECK-FIXES: {{^}} e = (e);
140
141 static const int kZero = 0;
142 (int)kZero;
143 // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: redundant cast to the same type
144 // CHECK-FIXES: {{^}} kZero;
145
146 int b2 = int(b);
147 int b3 = static_cast<double>(b);
148 int b4 = b;
149 double aa = a;
150 (void)b2;
151 return (void)g();
152 }
153
154 template <typename T>
template_function(T t,int n)155 void template_function(T t, int n) {
156 int i = (int)t;
157 // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: {{.*}}; use static_cast/const_cast/reinterpret_cast [
158 // CHECK-FIXES: int i = (int)t;
159 int j = (int)n;
160 // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: redundant cast to the same type
161 // CHECK-FIXES: int j = n;
162 }
163
164 template <typename T>
165 struct TemplateStruct {
fTemplateStruct166 void f(T t, int n) {
167 int k = (int)t;
168 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: {{.*}}; use static_cast/const_cast/reinterpret_cast
169 // CHECK-FIXES: int k = (int)t;
170 int l = (int)n;
171 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: redundant cast to the same type
172 // CHECK-FIXES: int l = n;
173 }
174 };
175
test_templates()176 void test_templates() {
177 template_function(1, 42);
178 template_function(1.0, 42);
179 TemplateStruct<int>().f(1, 42);
180 TemplateStruct<double>().f(1.0, 42);
181 }
182
183 extern "C" {
extern_c_code(const char * cpc)184 void extern_c_code(const char *cpc) {
185 const char *cpc2 = (const char*)cpc;
186 // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: redundant cast to the same type
187 // CHECK-FIXES: const char *cpc2 = cpc;
188 char *pc = (char*)cpc;
189 }
190 }
191
192 #define CAST(type, value) (type)(value)
macros(double d)193 void macros(double d) {
194 int i = CAST(int, d);
195 }
196
197 enum E { E1 = 1 };
198 template <E e>
199 struct A {
200 // Usage of template argument e = E1 is represented as (E)1 in the AST for
201 // some reason. We have a special treatment of this case to avoid warnings
202 // here.
203 static const E ee = e;
204 };
205 struct B : public A<E1> {};
206
207
208 void overloaded_function();
209 void overloaded_function(int);
210
211 template<typename Fn>
g(Fn fn)212 void g(Fn fn) {
213 fn();
214 }
215
function_casts()216 void function_casts() {
217 typedef void (*FnPtrVoid)();
218 typedef void (&FnRefVoid)();
219 typedef void (&FnRefInt)(int);
220
221 g((void (*)())overloaded_function);
222 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: C-style casts are discouraged; use static_cast [
223 // CHECK-FIXES: g(static_cast<void (*)()>(overloaded_function));
224 g((void (*)())&overloaded_function);
225 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: C-style casts are discouraged; use static_cast [
226 // CHECK-FIXES: g(static_cast<void (*)()>(&overloaded_function));
227 g((void (&)())overloaded_function);
228 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: C-style casts are discouraged; use static_cast [
229 // CHECK-FIXES: g(static_cast<void (&)()>(overloaded_function));
230
231 g((FnPtrVoid)overloaded_function);
232 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: C-style casts are discouraged; use static_cast [
233 // CHECK-FIXES: g(static_cast<FnPtrVoid>(overloaded_function));
234 g((FnPtrVoid)&overloaded_function);
235 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: C-style casts are discouraged; use static_cast [
236 // CHECK-FIXES: g(static_cast<FnPtrVoid>(&overloaded_function));
237 g((FnRefVoid)overloaded_function);
238 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: C-style casts are discouraged; use static_cast [
239 // CHECK-FIXES: g(static_cast<FnRefVoid>(overloaded_function));
240
241 FnPtrVoid fn0 = (void (*)())&overloaded_function;
242 // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: C-style casts are discouraged; use static_cast [
243 // CHECK-FIXES: FnPtrVoid fn0 = static_cast<void (*)()>(&overloaded_function);
244 FnPtrVoid fn1 = (void (*)())overloaded_function;
245 // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: C-style casts are discouraged; use static_cast [
246 // CHECK-FIXES: FnPtrVoid fn1 = static_cast<void (*)()>(overloaded_function);
247 FnPtrVoid fn1a = (FnPtrVoid)overloaded_function;
248 // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: C-style casts are discouraged; use static_cast [
249 // CHECK-FIXES: FnPtrVoid fn1a = static_cast<FnPtrVoid>(overloaded_function);
250 FnRefInt fn2 = (void (&)(int))overloaded_function;
251 // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: C-style casts are discouraged; use static_cast [
252 // CHECK-FIXES: FnRefInt fn2 = static_cast<void (&)(int)>(overloaded_function);
253 auto fn3 = (void (*)())&overloaded_function;
254 // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: C-style casts are discouraged; use static_cast [
255 // CHECK-FIXES: auto fn3 = static_cast<void (*)()>(&overloaded_function);
256 auto fn4 = (void (*)())overloaded_function;
257 // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: C-style casts are discouraged; use static_cast [
258 // CHECK-FIXES: auto fn4 = static_cast<void (*)()>(overloaded_function);
259 auto fn5 = (void (&)(int))overloaded_function;
260 // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: C-style casts are discouraged; use static_cast [
261 // CHECK-FIXES: auto fn5 = static_cast<void (&)(int)>(overloaded_function);
262
263 void (*fn6)() = (void (*)())&overloaded_function;
264 // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: C-style casts are discouraged; use static_cast [
265 // CHECK-FIXES: void (*fn6)() = static_cast<void (*)()>(&overloaded_function);
266 void (*fn7)() = (void (*)())overloaded_function;
267 // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: C-style casts are discouraged; use static_cast [
268 // CHECK-FIXES: void (*fn7)() = static_cast<void (*)()>(overloaded_function);
269 void (*fn8)() = (FnPtrVoid)overloaded_function;
270 // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: C-style casts are discouraged; use static_cast [
271 // CHECK-FIXES: void (*fn8)() = static_cast<FnPtrVoid>(overloaded_function);
272 void (&fn9)(int) = (void (&)(int))overloaded_function;
273 // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: C-style casts are discouraged; use static_cast [
274 // CHECK-FIXES: void (&fn9)(int) = static_cast<void (&)(int)>(overloaded_function);
275
276 void (*correct1)() = static_cast<void (*)()>(overloaded_function);
277 FnPtrVoid correct2 = static_cast<void (*)()>(&overloaded_function);
278 FnRefInt correct3 = static_cast<void (&)(int)>(overloaded_function);
279 }
280
281 struct S {
282 S(const char *);
283 };
284 struct ConvertibleToS {
285 operator S() const;
286 };
287 struct ConvertibleToSRef {
288 operator const S&() const;
289 };
290
conversions()291 void conversions() {
292 //auto s1 = (const S&)"";
293 // C HECK-MESSAGES: :[[@LINE-1]]:10: warning: C-style casts are discouraged; use static_cast [
294 // C HECK-FIXES: S s1 = static_cast<const S&>("");
295 auto s2 = (S)"";
296 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: C-style casts are discouraged; use constructor call syntax [
297 // CHECK-FIXES: auto s2 = S("");
298 auto s2a = (struct S)"";
299 // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: C-style casts are discouraged; use static_cast [
300 // CHECK-FIXES: auto s2a = static_cast<struct S>("");
301 auto s2b = (const S)"";
302 // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: C-style casts are discouraged; use static_cast [
303 // FIXME: This should be constructor call syntax: S("").
304 // CHECK-FIXES: auto s2b = static_cast<const S>("");
305 ConvertibleToS c;
306 auto s3 = (const S&)c;
307 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: C-style casts are discouraged; use static_cast/const_cast/reinterpret_cast [
308 // CHECK-FIXES: auto s3 = (const S&)c;
309 // FIXME: This should be a static_cast.
310 // C HECK-FIXES: auto s3 = static_cast<const S&>(c);
311 auto s4 = (S)c;
312 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: C-style casts are discouraged; use constructor call syntax [
313 // CHECK-FIXES: auto s4 = S(c);
314 ConvertibleToSRef cr;
315 auto s5 = (const S&)cr;
316 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: C-style casts are discouraged; use static_cast/const_cast/reinterpret_cast [
317 // CHECK-FIXES: auto s5 = (const S&)cr;
318 // FIXME: This should be a static_cast.
319 // C HECK-FIXES: auto s5 = static_cast<const S&>(cr);
320 auto s6 = (S)cr;
321 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: C-style casts are discouraged; use constructor call syntax [
322 // CHECK-FIXES: auto s6 = S(cr);
323 }
324