blob: afb1eaaad879bd716e3b919e0244e009969cf499 [file] [log] [blame]
Alexander Kornienko73666162014-07-14 07:37:05 +00001// RUN: $(dirname %s)/check_clang_tidy_fix.sh %s google-readability-casting %t
2// REQUIRES: shell
Alexander Kornienko276fc642014-06-29 22:19:53 +00003
4bool g() { return false; }
5
Alexander Kornienko73666162014-07-14 07:37:05 +00006struct X {};
7struct Y : public X {};
Alexander Kornienko276fc642014-06-29 22:19:53 +00008
Alexander Kornienko73666162014-07-14 07:37:05 +00009void 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 char *pc = (char*)cpc;
15 // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: C-style casts are discouraged. Use const_cast. {{.*}}
16 // CHECK-FIXES: char *pc = const_cast<char *>(cpc);
17
18 char *pc2 = (char*)(cpc + 33);
19 // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: C-style casts are discouraged. Use const_cast. {{.*}}
Alexander Kornienko1f317d62014-07-16 13:38:48 +000020 // CHECK-FIXES: char *pc2 = const_cast<char *>(cpc + 33);
Alexander Kornienko73666162014-07-14 07:37:05 +000021
22 const char &crc = *cpc;
23 char &rc = (char&)crc;
24 // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: C-style casts are discouraged. Use const_cast. {{.*}}
25 // CHECK-FIXES: char &rc = const_cast<char &>(crc);
26
27 char &rc2 = (char&)*cpc;
28 // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: C-style casts are discouraged. Use const_cast. {{.*}}
29 // CHECK-FIXES: char &rc2 = const_cast<char &>(*cpc);
30
31 char ** const* const* ppcpcpc;
32 char ****ppppc = (char****)ppcpcpc;
33 // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: C-style casts are discouraged. Use const_cast. {{.*}}
34 // CHECK-FIXES: char ****ppppc = const_cast<char ****>(ppcpcpc);
35
Alexander Kornienko1f317d62014-07-16 13:38:48 +000036 char ***pppc = (char***)*(ppcpcpc);
37 // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: C-style casts are discouraged. Use const_cast. {{.*}}
38 // CHECK-FIXES: char ***pppc = const_cast<char ***>(*(ppcpcpc));
39
40 char ***pppc2 = (char***)(*ppcpcpc);
41 // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: C-style casts are discouraged. Use const_cast. {{.*}}
42 // CHECK-FIXES: char ***pppc2 = const_cast<char ***>(*ppcpcpc);
43
44 char *pc5 = (char*)(const char*)(cpv);
45 // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: C-style casts are discouraged. Use const_cast. {{.*}}
46 // CHECK-MESSAGES: :[[@LINE-2]]:22: warning: C-style casts are discouraged. Use reinterpret_cast. {{.*}}
47 // CHECK-FIXES: char *pc5 = const_cast<char *>(reinterpret_cast<const char *>(cpv));
48
49
Alexander Kornienko73666162014-07-14 07:37:05 +000050 int b1 = (int)b;
51 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: C-style casts are discouraged. Use static_cast. [google-readability-casting]
52 // CHECK-FIXES: int b1 = static_cast<int>(b);
53
54 Y *pB = (Y*)pX;
55 // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: C-style casts are discouraged. Use static_cast/const_cast/reinterpret_cast. [google-readability-casting]
56 Y &rB = (Y&)*pX;
57 // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: C-style casts are discouraged. Use static_cast/const_cast/reinterpret_cast. [google-readability-casting]
58
59 const char *pc3 = (const char*)cpv;
60 // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: C-style casts are discouraged. Use reinterpret_cast. [google-readability-casting]
61 // CHECK-FIXES: const char *pc3 = reinterpret_cast<const char *>(cpv);
62
63 char *pc4 = (char*)cpv;
64 // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: C-style casts are discouraged. Use static_cast/const_cast/reinterpret_cast. [google-readability-casting]
65 // CHECK-FIXES: char *pc4 = (char*)cpv;
66
67 // CHECK-MESSAGES-NOT: warning:
Alexander Kornienko276fc642014-06-29 22:19:53 +000068 int b2 = int(b);
69 int b3 = static_cast<double>(b);
70 int b4 = b;
71 double aa = a;
72 (void)b2;
73 return (void)g();
74}
75
Alexander Kornienko73666162014-07-14 07:37:05 +000076template <typename T>
77void template_function(T t, int n) {
78 int i = (int)t;
79 // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: C-style casts are discouraged. Use static_cast/const_cast/reinterpret_cast.
80 // CHECK-FIXES: int i = (int)t;
81 int j = (int)n;
82 // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: Redundant cast to the same type.
83 // CHECK-FIXES: int j = n;
84}
85
86template <typename T>
87struct TemplateStruct {
88 void f(T t, int n) {
89 int k = (int)t;
90 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: C-style casts are discouraged. Use static_cast/const_cast/reinterpret_cast.
91 // CHECK-FIXES: int k = (int)t;
92 int l = (int)n;
93 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: Redundant cast to the same type.
94 // CHECK-FIXES: int l = n;
95 }
96};
97
98void test_templates() {
99 template_function(1, 42);
100 template_function(1.0, 42);
101 TemplateStruct<int>().f(1, 42);
102 TemplateStruct<double>().f(1.0, 42);
103}
104
105#define CAST(type, value) (type)(value)
106void macros(double d) {
107 int i = CAST(int, d);
Alexander Kornienko73666162014-07-14 07:37:05 +0000108}
109
Alexander Kornienko276fc642014-06-29 22:19:53 +0000110enum E { E1 = 1 };
111template <E e>
Alexander Kornienko73666162014-07-14 07:37:05 +0000112struct A {
113 // Usage of template argument e = E1 is represented as (E)1 in the AST for
114 // some reason. We have a special treatment of this case to avoid warnings
115 // here.
116 static const E ee = e;
117};
Alexander Kornienko276fc642014-06-29 22:19:53 +0000118struct B : public A<E1> {};