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. {{.*}} |
| 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 Kornienko | 276fc64 | 2014-06-29 22:19:53 +0000 | [diff] [blame] | 54 | 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 Kornienko | 7366616 | 2014-07-14 07:37:05 +0000 | [diff] [blame^] | 62 | template <typename T> |
| 63 | void 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 | |
| 72 | template <typename T> |
| 73 | struct 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 | |
| 84 | void 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) |
| 92 | void 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 Kornienko | 276fc64 | 2014-06-29 22:19:53 +0000 | [diff] [blame] | 99 | enum E { E1 = 1 }; |
| 100 | template <E e> |
Alexander Kornienko | 7366616 | 2014-07-14 07:37:05 +0000 | [diff] [blame^] | 101 | struct 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 Kornienko | 276fc64 | 2014-06-29 22:19:53 +0000 | [diff] [blame] | 107 | struct B : public A<E1> {}; |