Alexander Kornienko | 7366616 | 2014-07-14 07:37:05 +0000 | [diff] [blame] | 1 | // RUN: $(dirname %s)/check_clang_tidy_fix.sh %s google-readability-casting %t |
| 2 | // REQUIRES: shell |
Alexander Kornienko | 276fc64 | 2014-06-29 22:19:53 +0000 | [diff] [blame] | 3 | |
| 4 | bool g() { return false; } |
| 5 | |
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; |
| 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 Kornienko | 1f317d6 | 2014-07-16 13:38:48 +0000 | [diff] [blame] | 20 | // CHECK-FIXES: char *pc2 = const_cast<char *>(cpc + 33); |
Alexander Kornienko | 7366616 | 2014-07-14 07:37:05 +0000 | [diff] [blame] | 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 | |
Alexander Kornienko | 1f317d6 | 2014-07-16 13:38:48 +0000 | [diff] [blame] | 36 | 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 Kornienko | 7366616 | 2014-07-14 07:37:05 +0000 | [diff] [blame] | 50 | 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 Kornienko | 276fc64 | 2014-06-29 22:19:53 +0000 | [diff] [blame] | 68 | 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 Kornienko | 7366616 | 2014-07-14 07:37:05 +0000 | [diff] [blame] | 76 | template <typename T> |
| 77 | void 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 | |
| 86 | template <typename T> |
| 87 | struct 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 | |
| 98 | void 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) |
| 106 | void macros(double d) { |
| 107 | int i = CAST(int, d); |
Alexander Kornienko | 7366616 | 2014-07-14 07:37:05 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Alexander Kornienko | 276fc64 | 2014-06-29 22:19:53 +0000 | [diff] [blame] | 110 | enum E { E1 = 1 }; |
| 111 | template <E e> |
Alexander Kornienko | 7366616 | 2014-07-14 07:37:05 +0000 | [diff] [blame] | 112 | struct 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 Kornienko | 276fc64 | 2014-06-29 22:19:53 +0000 | [diff] [blame] | 118 | struct B : public A<E1> {}; |