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