blob: a81c26246b044b1953fcf92bb6d2822b779c0ff0 [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. {{.*}}
20 // CHECK-FIXES: char *pc2 = const_cast<char *>((cpc + 33));
21
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
36 int b1 = (int)b;
37 // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: C-style casts are discouraged. Use static_cast. [google-readability-casting]
38 // CHECK-FIXES: int b1 = static_cast<int>(b);
39
40 Y *pB = (Y*)pX;
41 // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: C-style casts are discouraged. Use static_cast/const_cast/reinterpret_cast. [google-readability-casting]
42 Y &rB = (Y&)*pX;
43 // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: C-style casts are discouraged. Use static_cast/const_cast/reinterpret_cast. [google-readability-casting]
44
45 const char *pc3 = (const char*)cpv;
46 // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: C-style casts are discouraged. Use reinterpret_cast. [google-readability-casting]
47 // CHECK-FIXES: const char *pc3 = reinterpret_cast<const char *>(cpv);
48
49 char *pc4 = (char*)cpv;
50 // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: C-style casts are discouraged. Use static_cast/const_cast/reinterpret_cast. [google-readability-casting]
51 // CHECK-FIXES: char *pc4 = (char*)cpv;
52
53 // CHECK-MESSAGES-NOT: warning:
Alexander Kornienko276fc642014-06-29 22:19:53 +000054 int b2 = int(b);
55 int b3 = static_cast<double>(b);
56 int b4 = b;
57 double aa = a;
58 (void)b2;
59 return (void)g();
60}
61
Alexander Kornienko73666162014-07-14 07:37:05 +000062template <typename T>
63void template_function(T t, int n) {
64 int i = (int)t;
65 // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: C-style casts are discouraged. Use static_cast/const_cast/reinterpret_cast.
66 // CHECK-FIXES: int i = (int)t;
67 int j = (int)n;
68 // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: Redundant cast to the same type.
69 // CHECK-FIXES: int j = n;
70}
71
72template <typename T>
73struct TemplateStruct {
74 void f(T t, int n) {
75 int k = (int)t;
76 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: C-style casts are discouraged. Use static_cast/const_cast/reinterpret_cast.
77 // CHECK-FIXES: int k = (int)t;
78 int l = (int)n;
79 // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: Redundant cast to the same type.
80 // CHECK-FIXES: int l = n;
81 }
82};
83
84void test_templates() {
85 template_function(1, 42);
86 template_function(1.0, 42);
87 TemplateStruct<int>().f(1, 42);
88 TemplateStruct<double>().f(1.0, 42);
89}
90
91#define CAST(type, value) (type)(value)
92void macros(double d) {
93 int i = CAST(int, d);
94 // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: C-style casts are discouraged. Use static_cast.
95 // CHECK-FIXES: #define CAST(type, value) (type)(value)
96 // CHECK-FIXES: int i = CAST(int, d);
97}
98
Alexander Kornienko276fc642014-06-29 22:19:53 +000099enum E { E1 = 1 };
100template <E e>
Alexander Kornienko73666162014-07-14 07:37:05 +0000101struct A {
102 // Usage of template argument e = E1 is represented as (E)1 in the AST for
103 // some reason. We have a special treatment of this case to avoid warnings
104 // here.
105 static const E ee = e;
106};
Alexander Kornienko276fc642014-06-29 22:19:53 +0000107struct B : public A<E1> {};