Manuel Klimek | b91bee0 | 2015-10-22 11:31:44 +0000 | [diff] [blame] | 1 | // RUN: %check_clang_tidy %s google-readability-casting %t |
Alexander Kornienko | 276fc64 | 2014-06-29 22:19:53 +0000 | [diff] [blame] | 2 | |
| 3 | bool g() { return false; } |
| 4 | |
Alexander Kornienko | 5091372 | 2014-10-01 12:47:53 +0000 | [diff] [blame] | 5 | enum Enum { Enum1 }; |
Alexander Kornienko | 7366616 | 2014-07-14 07:37:05 +0000 | [diff] [blame] | 6 | struct X {}; |
| 7 | struct Y : public X {}; |
Alexander Kornienko | 276fc64 | 2014-06-29 22:19:53 +0000 | [diff] [blame] | 8 | |
Alexander Kornienko | 7366616 | 2014-07-14 07:37:05 +0000 | [diff] [blame] | 9 | void f(int a, double b, const char *cpc, const void *cpv, X *pX) { |
| 10 | const char *cpc2 = (const char*)cpc; |
Alexander Kornienko | 78070fb | 2015-01-29 15:17:13 +0000 | [diff] [blame] | 11 | // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: redundant cast to the same type [google-readability-casting] |
Alexander Kornienko | 7366616 | 2014-07-14 07:37:05 +0000 | [diff] [blame] | 12 | // CHECK-FIXES: const char *cpc2 = cpc; |
| 13 | |
Alexander Kornienko | 78070fb | 2015-01-29 15:17:13 +0000 | [diff] [blame] | 14 | typedef const char *Typedef1; |
| 15 | typedef const char *Typedef2; |
| 16 | Typedef1 t1; |
| 17 | (Typedef2)t1; |
| 18 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: possibly redundant cast between typedefs of the same type [google-readability-casting] |
| 19 | // CHECK-FIXES: {{^}} (Typedef2)t1; |
| 20 | (const char*)t1; |
| 21 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: possibly redundant cast {{.*}} |
| 22 | // CHECK-FIXES: {{^}} (const char*)t1; |
| 23 | (Typedef1)cpc; |
| 24 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: possibly redundant cast {{.*}} |
| 25 | // CHECK-FIXES: {{^}} (Typedef1)cpc; |
| 26 | (Typedef1)t1; |
| 27 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: redundant cast to the same type |
| 28 | // CHECK-FIXES: {{^}} t1; |
| 29 | |
Alexander Kornienko | 7366616 | 2014-07-14 07:37:05 +0000 | [diff] [blame] | 30 | char *pc = (char*)cpc; |
Alexander Kornienko | b1b2f87 | 2016-01-08 15:21:40 +0000 | [diff] [blame] | 31 | // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: C-style casts are discouraged; use const_cast [google-readability-casting] |
Alexander Kornienko | 5091372 | 2014-10-01 12:47:53 +0000 | [diff] [blame] | 32 | // CHECK-FIXES: char *pc = const_cast<char*>(cpc); |
Alexander Kornienko | 7366616 | 2014-07-14 07:37:05 +0000 | [diff] [blame] | 33 | |
| 34 | char *pc2 = (char*)(cpc + 33); |
Alexander Kornienko | b1b2f87 | 2016-01-08 15:21:40 +0000 | [diff] [blame] | 35 | // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: {{.*}}; use const_cast {{.*}} |
Alexander Kornienko | 5091372 | 2014-10-01 12:47:53 +0000 | [diff] [blame] | 36 | // CHECK-FIXES: char *pc2 = const_cast<char*>(cpc + 33); |
Alexander Kornienko | 7366616 | 2014-07-14 07:37:05 +0000 | [diff] [blame] | 37 | |
| 38 | const char &crc = *cpc; |
| 39 | char &rc = (char&)crc; |
Alexander Kornienko | b1b2f87 | 2016-01-08 15:21:40 +0000 | [diff] [blame] | 40 | // CHECK-MESSAGES: :[[@LINE-1]]:14: warning: {{.*}}; use const_cast {{.*}} |
Alexander Kornienko | 5091372 | 2014-10-01 12:47:53 +0000 | [diff] [blame] | 41 | // CHECK-FIXES: char &rc = const_cast<char&>(crc); |
Alexander Kornienko | 7366616 | 2014-07-14 07:37:05 +0000 | [diff] [blame] | 42 | |
| 43 | char &rc2 = (char&)*cpc; |
Alexander Kornienko | b1b2f87 | 2016-01-08 15:21:40 +0000 | [diff] [blame] | 44 | // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: {{.*}}; use const_cast {{.*}} |
Alexander Kornienko | 5091372 | 2014-10-01 12:47:53 +0000 | [diff] [blame] | 45 | // CHECK-FIXES: char &rc2 = const_cast<char&>(*cpc); |
Alexander Kornienko | 7366616 | 2014-07-14 07:37:05 +0000 | [diff] [blame] | 46 | |
| 47 | char ** const* const* ppcpcpc; |
| 48 | char ****ppppc = (char****)ppcpcpc; |
Alexander Kornienko | b1b2f87 | 2016-01-08 15:21:40 +0000 | [diff] [blame] | 49 | // CHECK-MESSAGES: :[[@LINE-1]]:20: warning: {{.*}}; use const_cast {{.*}} |
Alexander Kornienko | 5091372 | 2014-10-01 12:47:53 +0000 | [diff] [blame] | 50 | // CHECK-FIXES: char ****ppppc = const_cast<char****>(ppcpcpc); |
Alexander Kornienko | 7366616 | 2014-07-14 07:37:05 +0000 | [diff] [blame] | 51 | |
Alexander Kornienko | 1f317d6 | 2014-07-16 13:38:48 +0000 | [diff] [blame] | 52 | char ***pppc = (char***)*(ppcpcpc); |
Alexander Kornienko | b1b2f87 | 2016-01-08 15:21:40 +0000 | [diff] [blame] | 53 | // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: {{.*}}; use const_cast {{.*}} |
Alexander Kornienko | 5091372 | 2014-10-01 12:47:53 +0000 | [diff] [blame] | 54 | // CHECK-FIXES: char ***pppc = const_cast<char***>(*(ppcpcpc)); |
Alexander Kornienko | 1f317d6 | 2014-07-16 13:38:48 +0000 | [diff] [blame] | 55 | |
| 56 | char ***pppc2 = (char***)(*ppcpcpc); |
Alexander Kornienko | b1b2f87 | 2016-01-08 15:21:40 +0000 | [diff] [blame] | 57 | // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: {{.*}}; use const_cast {{.*}} |
Alexander Kornienko | 5091372 | 2014-10-01 12:47:53 +0000 | [diff] [blame] | 58 | // CHECK-FIXES: char ***pppc2 = const_cast<char***>(*ppcpcpc); |
Alexander Kornienko | 1f317d6 | 2014-07-16 13:38:48 +0000 | [diff] [blame] | 59 | |
| 60 | char *pc5 = (char*)(const char*)(cpv); |
Alexander Kornienko | b1b2f87 | 2016-01-08 15:21:40 +0000 | [diff] [blame] | 61 | // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: {{.*}}; use const_cast {{.*}} |
| 62 | // CHECK-MESSAGES: :[[@LINE-2]]:22: warning: {{.*}}; use reinterpret_cast {{.*}} |
Alexander Kornienko | 5091372 | 2014-10-01 12:47:53 +0000 | [diff] [blame] | 63 | // CHECK-FIXES: char *pc5 = const_cast<char*>(reinterpret_cast<const char*>(cpv)); |
Alexander Kornienko | 1f317d6 | 2014-07-16 13:38:48 +0000 | [diff] [blame] | 64 | |
Alexander Kornienko | 7366616 | 2014-07-14 07:37:05 +0000 | [diff] [blame] | 65 | int b1 = (int)b; |
Alexander Kornienko | b1b2f87 | 2016-01-08 15:21:40 +0000 | [diff] [blame] | 66 | // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: {{.*}}; use static_cast {{.*}} |
Alexander Kornienko | 7366616 | 2014-07-14 07:37:05 +0000 | [diff] [blame] | 67 | // CHECK-FIXES: int b1 = static_cast<int>(b); |
| 68 | |
| 69 | Y *pB = (Y*)pX; |
Alexander Kornienko | b1b2f87 | 2016-01-08 15:21:40 +0000 | [diff] [blame] | 70 | // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: {{.*}}; use static_cast/const_cast/reinterpret_cast {{.*}} |
Alexander Kornienko | 7366616 | 2014-07-14 07:37:05 +0000 | [diff] [blame] | 71 | Y &rB = (Y&)*pX; |
Alexander Kornienko | b1b2f87 | 2016-01-08 15:21:40 +0000 | [diff] [blame] | 72 | // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: {{.*}}; use static_cast/const_cast/reinterpret_cast {{.*}} |
Alexander Kornienko | 7366616 | 2014-07-14 07:37:05 +0000 | [diff] [blame] | 73 | |
| 74 | const char *pc3 = (const char*)cpv; |
Alexander Kornienko | b1b2f87 | 2016-01-08 15:21:40 +0000 | [diff] [blame] | 75 | // CHECK-MESSAGES: :[[@LINE-1]]:21: warning: {{.*}}; use reinterpret_cast {{.*}} |
Alexander Kornienko | 5091372 | 2014-10-01 12:47:53 +0000 | [diff] [blame] | 76 | // CHECK-FIXES: const char *pc3 = reinterpret_cast<const char*>(cpv); |
Alexander Kornienko | 7366616 | 2014-07-14 07:37:05 +0000 | [diff] [blame] | 77 | |
| 78 | char *pc4 = (char*)cpv; |
Alexander Kornienko | b1b2f87 | 2016-01-08 15:21:40 +0000 | [diff] [blame] | 79 | // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: {{.*}}; use static_cast/const_cast/reinterpret_cast {{.*}} |
Alexander Kornienko | 7366616 | 2014-07-14 07:37:05 +0000 | [diff] [blame] | 80 | // CHECK-FIXES: char *pc4 = (char*)cpv; |
| 81 | |
Alexander Kornienko | 5091372 | 2014-10-01 12:47:53 +0000 | [diff] [blame] | 82 | b1 = (int)Enum1; |
Alexander Kornienko | b1b2f87 | 2016-01-08 15:21:40 +0000 | [diff] [blame] | 83 | // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: {{.*}}; use static_cast {{.*}} |
Alexander Kornienko | 5091372 | 2014-10-01 12:47:53 +0000 | [diff] [blame] | 84 | // CHECK-FIXES: b1 = static_cast<int>(Enum1); |
| 85 | |
| 86 | Enum e = (Enum)b1; |
Alexander Kornienko | b1b2f87 | 2016-01-08 15:21:40 +0000 | [diff] [blame] | 87 | // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: {{.*}}; use static_cast {{.*}} |
Alexander Kornienko | 5091372 | 2014-10-01 12:47:53 +0000 | [diff] [blame] | 88 | // CHECK-FIXES: Enum e = static_cast<Enum>(b1); |
| 89 | |
Alexander Kornienko | 7366616 | 2014-07-14 07:37:05 +0000 | [diff] [blame] | 90 | // CHECK-MESSAGES-NOT: warning: |
Alexander Kornienko | 276fc64 | 2014-06-29 22:19:53 +0000 | [diff] [blame] | 91 | int b2 = int(b); |
| 92 | int b3 = static_cast<double>(b); |
| 93 | int b4 = b; |
| 94 | double aa = a; |
| 95 | (void)b2; |
| 96 | return (void)g(); |
| 97 | } |
| 98 | |
Alexander Kornienko | 7366616 | 2014-07-14 07:37:05 +0000 | [diff] [blame] | 99 | template <typename T> |
| 100 | void template_function(T t, int n) { |
| 101 | int i = (int)t; |
Alexander Kornienko | b1b2f87 | 2016-01-08 15:21:40 +0000 | [diff] [blame] | 102 | // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: {{.*}}; use static_cast/const_cast/reinterpret_cast {{.*}} |
Alexander Kornienko | 7366616 | 2014-07-14 07:37:05 +0000 | [diff] [blame] | 103 | // CHECK-FIXES: int i = (int)t; |
| 104 | int j = (int)n; |
Alexander Kornienko | 78070fb | 2015-01-29 15:17:13 +0000 | [diff] [blame] | 105 | // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: redundant cast to the same type |
Alexander Kornienko | 7366616 | 2014-07-14 07:37:05 +0000 | [diff] [blame] | 106 | // CHECK-FIXES: int j = n; |
| 107 | } |
| 108 | |
| 109 | template <typename T> |
| 110 | struct TemplateStruct { |
| 111 | void f(T t, int n) { |
| 112 | int k = (int)t; |
Alexander Kornienko | b1b2f87 | 2016-01-08 15:21:40 +0000 | [diff] [blame] | 113 | // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: {{.*}}; use static_cast/const_cast/reinterpret_cast |
Alexander Kornienko | 7366616 | 2014-07-14 07:37:05 +0000 | [diff] [blame] | 114 | // CHECK-FIXES: int k = (int)t; |
| 115 | int l = (int)n; |
Alexander Kornienko | 78070fb | 2015-01-29 15:17:13 +0000 | [diff] [blame] | 116 | // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: redundant cast to the same type |
Alexander Kornienko | 7366616 | 2014-07-14 07:37:05 +0000 | [diff] [blame] | 117 | // CHECK-FIXES: int l = n; |
| 118 | } |
| 119 | }; |
| 120 | |
| 121 | void test_templates() { |
| 122 | template_function(1, 42); |
| 123 | template_function(1.0, 42); |
| 124 | TemplateStruct<int>().f(1, 42); |
| 125 | TemplateStruct<double>().f(1.0, 42); |
| 126 | } |
| 127 | |
Alexander Kornienko | 78070fb | 2015-01-29 15:17:13 +0000 | [diff] [blame] | 128 | extern "C" { |
| 129 | void extern_c_code(const char *cpc) { |
| 130 | const char *cpc2 = (const char*)cpc; |
| 131 | // CHECK-MESSAGES: :[[@LINE-1]]:22: warning: redundant cast to the same type |
| 132 | // CHECK-FIXES: const char *cpc2 = cpc; |
| 133 | char *pc = (char*)cpc; |
| 134 | } |
| 135 | } |
| 136 | |
Alexander Kornienko | 7366616 | 2014-07-14 07:37:05 +0000 | [diff] [blame] | 137 | #define CAST(type, value) (type)(value) |
| 138 | void macros(double d) { |
| 139 | int i = CAST(int, d); |
Alexander Kornienko | 7366616 | 2014-07-14 07:37:05 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Alexander Kornienko | 276fc64 | 2014-06-29 22:19:53 +0000 | [diff] [blame] | 142 | enum E { E1 = 1 }; |
| 143 | template <E e> |
Alexander Kornienko | 7366616 | 2014-07-14 07:37:05 +0000 | [diff] [blame] | 144 | struct A { |
| 145 | // Usage of template argument e = E1 is represented as (E)1 in the AST for |
| 146 | // some reason. We have a special treatment of this case to avoid warnings |
| 147 | // here. |
| 148 | static const E ee = e; |
| 149 | }; |
Alexander Kornienko | 276fc64 | 2014-06-29 22:19:53 +0000 | [diff] [blame] | 150 | struct B : public A<E1> {}; |