blob: 414fa8bbcabbb7280e406dfbb800727cc451e2dc [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 Kornienko50913722014-10-01 12:47:53 +00006enum Enum { Enum1 };
Alexander Kornienko73666162014-07-14 07:37:05 +00007struct X {};
8struct Y : public X {};
Alexander Kornienko276fc642014-06-29 22:19:53 +00009
Alexander Kornienko73666162014-07-14 07:37:05 +000010void 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 char *pc = (char*)cpc;
16 // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: C-style casts are discouraged. Use const_cast. {{.*}}
Alexander Kornienko50913722014-10-01 12:47:53 +000017 // CHECK-FIXES: char *pc = const_cast<char*>(cpc);
Alexander Kornienko73666162014-07-14 07:37:05 +000018
19 char *pc2 = (char*)(cpc + 33);
20 // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: C-style casts are discouraged. Use const_cast. {{.*}}
Alexander Kornienko50913722014-10-01 12:47:53 +000021 // CHECK-FIXES: char *pc2 = const_cast<char*>(cpc + 33);
Alexander Kornienko73666162014-07-14 07:37:05 +000022
23 const char &crc = *cpc;
24 char &rc = (char&)crc;
25 // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: C-style casts are discouraged. Use const_cast. {{.*}}
Alexander Kornienko50913722014-10-01 12:47:53 +000026 // CHECK-FIXES: char &rc = const_cast<char&>(crc);
Alexander Kornienko73666162014-07-14 07:37:05 +000027
28 char &rc2 = (char&)*cpc;
29 // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: C-style casts are discouraged. Use const_cast. {{.*}}
Alexander Kornienko50913722014-10-01 12:47:53 +000030 // CHECK-FIXES: char &rc2 = const_cast<char&>(*cpc);
Alexander Kornienko73666162014-07-14 07:37:05 +000031
32 char ** const* const* ppcpcpc;
33 char ****ppppc = (char****)ppcpcpc;
34 // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: C-style casts are discouraged. Use const_cast. {{.*}}
Alexander Kornienko50913722014-10-01 12:47:53 +000035 // CHECK-FIXES: char ****ppppc = const_cast<char****>(ppcpcpc);
Alexander Kornienko73666162014-07-14 07:37:05 +000036
Alexander Kornienko1f317d62014-07-16 13:38:48 +000037 char ***pppc = (char***)*(ppcpcpc);
38 // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: C-style casts are discouraged. Use const_cast. {{.*}}
Alexander Kornienko50913722014-10-01 12:47:53 +000039 // CHECK-FIXES: char ***pppc = const_cast<char***>(*(ppcpcpc));
Alexander Kornienko1f317d62014-07-16 13:38:48 +000040
41 char ***pppc2 = (char***)(*ppcpcpc);
42 // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: C-style casts are discouraged. Use const_cast. {{.*}}
Alexander Kornienko50913722014-10-01 12:47:53 +000043 // CHECK-FIXES: char ***pppc2 = const_cast<char***>(*ppcpcpc);
Alexander Kornienko1f317d62014-07-16 13:38:48 +000044
45 char *pc5 = (char*)(const char*)(cpv);
46 // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: C-style casts are discouraged. Use const_cast. {{.*}}
47 // CHECK-MESSAGES: :[[@LINE-2]]:22: warning: C-style casts are discouraged. Use reinterpret_cast. {{.*}}
Alexander Kornienko50913722014-10-01 12:47:53 +000048 // CHECK-FIXES: char *pc5 = const_cast<char*>(reinterpret_cast<const char*>(cpv));
Alexander Kornienko1f317d62014-07-16 13:38:48 +000049
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]
Alexander Kornienko50913722014-10-01 12:47:53 +000061 // CHECK-FIXES: const char *pc3 = reinterpret_cast<const char*>(cpv);
Alexander Kornienko73666162014-07-14 07:37:05 +000062
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
Alexander Kornienko50913722014-10-01 12:47:53 +000067 b1 = (int)Enum1;
68 // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: C-style casts are discouraged. Use static_cast. [google-readability-casting]
69 // CHECK-FIXES: b1 = static_cast<int>(Enum1);
70
71 Enum e = (Enum)b1;
72 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: C-style casts are discouraged. Use static_cast. [google-readability-casting]
73 // CHECK-FIXES: Enum e = static_cast<Enum>(b1);
74
Alexander Kornienko73666162014-07-14 07:37:05 +000075 // CHECK-MESSAGES-NOT: warning:
Alexander Kornienko276fc642014-06-29 22:19:53 +000076 int b2 = int(b);
77 int b3 = static_cast<double>(b);
78 int b4 = b;
79 double aa = a;
80 (void)b2;
81 return (void)g();
82}
83
Alexander Kornienko73666162014-07-14 07:37:05 +000084template <typename T>
85void template_function(T t, int n) {
86 int i = (int)t;
87 // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: C-style casts are discouraged. Use static_cast/const_cast/reinterpret_cast.
88 // CHECK-FIXES: int i = (int)t;
89 int j = (int)n;
90 // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: Redundant cast to the same type.
91 // CHECK-FIXES: int j = n;
92}
93
94template <typename T>
95struct TemplateStruct {
96 void f(T t, int n) {
97 int k = (int)t;
98 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: C-style casts are discouraged. Use static_cast/const_cast/reinterpret_cast.
99 // CHECK-FIXES: int k = (int)t;
100 int l = (int)n;
101 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: Redundant cast to the same type.
102 // CHECK-FIXES: int l = n;
103 }
104};
105
106void test_templates() {
107 template_function(1, 42);
108 template_function(1.0, 42);
109 TemplateStruct<int>().f(1, 42);
110 TemplateStruct<double>().f(1.0, 42);
111}
112
113#define CAST(type, value) (type)(value)
114void macros(double d) {
115 int i = CAST(int, d);
Alexander Kornienko73666162014-07-14 07:37:05 +0000116}
117
Alexander Kornienko276fc642014-06-29 22:19:53 +0000118enum E { E1 = 1 };
119template <E e>
Alexander Kornienko73666162014-07-14 07:37:05 +0000120struct A {
121 // Usage of template argument e = E1 is represented as (E)1 in the AST for
122 // some reason. We have a special treatment of this case to avoid warnings
123 // here.
124 static const E ee = e;
125};
Alexander Kornienko276fc642014-06-29 22:19:53 +0000126struct B : public A<E1> {};