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 | 5091372 | 2014-10-01 12:47:53 +0000 | [diff] [blame^] | 6 | enum Enum { Enum1 }; |
Alexander Kornienko | 7366616 | 2014-07-14 07:37:05 +0000 | [diff] [blame] | 7 | struct X {}; |
| 8 | struct Y : public X {}; |
Alexander Kornienko | 276fc64 | 2014-06-29 22:19:53 +0000 | [diff] [blame] | 9 | |
Alexander Kornienko | 7366616 | 2014-07-14 07:37:05 +0000 | [diff] [blame] | 10 | void 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 Kornienko | 5091372 | 2014-10-01 12:47:53 +0000 | [diff] [blame^] | 17 | // CHECK-FIXES: char *pc = const_cast<char*>(cpc); |
Alexander Kornienko | 7366616 | 2014-07-14 07:37:05 +0000 | [diff] [blame] | 18 | |
| 19 | char *pc2 = (char*)(cpc + 33); |
| 20 | // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: C-style casts are discouraged. Use const_cast. {{.*}} |
Alexander Kornienko | 5091372 | 2014-10-01 12:47:53 +0000 | [diff] [blame^] | 21 | // CHECK-FIXES: char *pc2 = const_cast<char*>(cpc + 33); |
Alexander Kornienko | 7366616 | 2014-07-14 07:37:05 +0000 | [diff] [blame] | 22 | |
| 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 Kornienko | 5091372 | 2014-10-01 12:47:53 +0000 | [diff] [blame^] | 26 | // CHECK-FIXES: char &rc = const_cast<char&>(crc); |
Alexander Kornienko | 7366616 | 2014-07-14 07:37:05 +0000 | [diff] [blame] | 27 | |
| 28 | char &rc2 = (char&)*cpc; |
| 29 | // CHECK-MESSAGES: :[[@LINE-1]]:15: warning: C-style casts are discouraged. Use const_cast. {{.*}} |
Alexander Kornienko | 5091372 | 2014-10-01 12:47:53 +0000 | [diff] [blame^] | 30 | // CHECK-FIXES: char &rc2 = const_cast<char&>(*cpc); |
Alexander Kornienko | 7366616 | 2014-07-14 07:37:05 +0000 | [diff] [blame] | 31 | |
| 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 Kornienko | 5091372 | 2014-10-01 12:47:53 +0000 | [diff] [blame^] | 35 | // CHECK-FIXES: char ****ppppc = const_cast<char****>(ppcpcpc); |
Alexander Kornienko | 7366616 | 2014-07-14 07:37:05 +0000 | [diff] [blame] | 36 | |
Alexander Kornienko | 1f317d6 | 2014-07-16 13:38:48 +0000 | [diff] [blame] | 37 | char ***pppc = (char***)*(ppcpcpc); |
| 38 | // CHECK-MESSAGES: :[[@LINE-1]]:18: warning: C-style casts are discouraged. Use const_cast. {{.*}} |
Alexander Kornienko | 5091372 | 2014-10-01 12:47:53 +0000 | [diff] [blame^] | 39 | // CHECK-FIXES: char ***pppc = const_cast<char***>(*(ppcpcpc)); |
Alexander Kornienko | 1f317d6 | 2014-07-16 13:38:48 +0000 | [diff] [blame] | 40 | |
| 41 | char ***pppc2 = (char***)(*ppcpcpc); |
| 42 | // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: C-style casts are discouraged. Use const_cast. {{.*}} |
Alexander Kornienko | 5091372 | 2014-10-01 12:47:53 +0000 | [diff] [blame^] | 43 | // CHECK-FIXES: char ***pppc2 = const_cast<char***>(*ppcpcpc); |
Alexander Kornienko | 1f317d6 | 2014-07-16 13:38:48 +0000 | [diff] [blame] | 44 | |
| 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 Kornienko | 5091372 | 2014-10-01 12:47:53 +0000 | [diff] [blame^] | 48 | // CHECK-FIXES: char *pc5 = const_cast<char*>(reinterpret_cast<const char*>(cpv)); |
Alexander Kornienko | 1f317d6 | 2014-07-16 13:38:48 +0000 | [diff] [blame] | 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] |
Alexander Kornienko | 5091372 | 2014-10-01 12:47:53 +0000 | [diff] [blame^] | 61 | // CHECK-FIXES: const char *pc3 = reinterpret_cast<const char*>(cpv); |
Alexander Kornienko | 7366616 | 2014-07-14 07:37:05 +0000 | [diff] [blame] | 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 | |
Alexander Kornienko | 5091372 | 2014-10-01 12:47:53 +0000 | [diff] [blame^] | 67 | 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 Kornienko | 7366616 | 2014-07-14 07:37:05 +0000 | [diff] [blame] | 75 | // CHECK-MESSAGES-NOT: warning: |
Alexander Kornienko | 276fc64 | 2014-06-29 22:19:53 +0000 | [diff] [blame] | 76 | 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 Kornienko | 7366616 | 2014-07-14 07:37:05 +0000 | [diff] [blame] | 84 | template <typename T> |
| 85 | void 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 | |
| 94 | template <typename T> |
| 95 | struct 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 | |
| 106 | void 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) |
| 114 | void macros(double d) { |
| 115 | int i = CAST(int, d); |
Alexander Kornienko | 7366616 | 2014-07-14 07:37:05 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Alexander Kornienko | 276fc64 | 2014-06-29 22:19:53 +0000 | [diff] [blame] | 118 | enum E { E1 = 1 }; |
| 119 | template <E e> |
Alexander Kornienko | 7366616 | 2014-07-14 07:37:05 +0000 | [diff] [blame] | 120 | struct 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 Kornienko | 276fc64 | 2014-06-29 22:19:53 +0000 | [diff] [blame] | 126 | struct B : public A<E1> {}; |