|
| 1 | +// RUN: %check_clang_tidy -std=c++17 %s modernize-avoid-c-arrays %t -- \ |
| 2 | +// RUN: -config='{CheckOptions: { modernize-avoid-c-arrays.CheckSugaredTypes: true }}' |
| 3 | + |
| 4 | +int a[] = {1, 2}; |
| 5 | +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: do not declare C-style arrays, use 'std::array' instead |
| 6 | + |
| 7 | +int b[1]; |
| 8 | +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: do not declare C-style arrays, use 'std::array' instead |
| 9 | + |
| 10 | +void foo() { |
| 11 | + int c[b[0]]; |
| 12 | + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C VLA arrays, use 'std::vector' instead |
| 13 | + |
| 14 | + using d = decltype(c); |
| 15 | + // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: do not declare C VLA arrays, use 'std::vector' instead |
| 16 | + // CHECK-MESSAGES: :[[@LINE-2]]:13: note: declared type 'decltype(c)' resolves to 'int[b[0]]' |
| 17 | + |
| 18 | + d e; |
| 19 | + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C VLA arrays, use 'std::vector' instead |
| 20 | + // CHECK-MESSAGES: :[[@LINE-2]]:3: note: declared type 'd' resolves to 'int[b[0]]' |
| 21 | +} |
| 22 | + |
| 23 | +template <typename T, int Size> |
| 24 | +class array { |
| 25 | + T d[Size]; |
| 26 | + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use 'std::array' instead |
| 27 | + |
| 28 | + int e[1]; |
| 29 | + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use 'std::array' instead |
| 30 | +}; |
| 31 | + |
| 32 | +array<int[4], 2> d; |
| 33 | +// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not declare C-style arrays, use 'std::array' instead |
| 34 | + |
| 35 | +using k = int[4]; |
| 36 | +// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: do not declare C-style arrays, use 'std::array' instead |
| 37 | + |
| 38 | +array<k, 2> dk; |
| 39 | +// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not declare C-style arrays, use 'std::array' instead |
| 40 | +// CHECK-MESSAGES: :[[@LINE-2]]:7: note: declared type 'k' resolves to 'int[4]' |
| 41 | + |
| 42 | +template <typename T> |
| 43 | +class unique_ptr { |
| 44 | + T *d; |
| 45 | + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use 'std::array' instead |
| 46 | + // CHECK-MESSAGES: :[[@LINE-2]]:3: note: array type 'int[]' here |
| 47 | + |
| 48 | + int e[1]; |
| 49 | + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use 'std::array' instead |
| 50 | +}; |
| 51 | + |
| 52 | +unique_ptr<int[]> d2; |
| 53 | +// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: do not declare C-style arrays, use 'std::array' instead |
| 54 | + |
| 55 | +using k2 = int[]; |
| 56 | +// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: do not declare C-style arrays, use 'std::array' instead |
| 57 | + |
| 58 | +unique_ptr<k2> dk2; |
| 59 | +// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: do not declare C-style arrays, use 'std::array' instead |
| 60 | +// CHECK-MESSAGES: :[[@LINE-2]]:12: note: declared type 'k2' resolves to 'int[]' |
| 61 | + |
| 62 | +// Some header |
| 63 | +extern "C" { |
| 64 | + |
| 65 | +int f[] = {1, 2}; |
| 66 | + |
| 67 | +int j[1]; |
| 68 | + |
| 69 | +inline void bar() { |
| 70 | + { |
| 71 | + int j[j[0]]; |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +extern "C++" { |
| 76 | +int f3[] = {1, 2}; |
| 77 | +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: do not declare C-style arrays, use 'std::array' instead |
| 78 | + |
| 79 | +int j3[1]; |
| 80 | +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: do not declare C-style arrays, use 'std::array' instead |
| 81 | + |
| 82 | +struct Foo { |
| 83 | + int f3[3] = {1, 2}; |
| 84 | + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use 'std::array' instead |
| 85 | + |
| 86 | + int j3[1]; |
| 87 | + // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use 'std::array' instead |
| 88 | +}; |
| 89 | +} |
| 90 | + |
| 91 | +struct Bar { |
| 92 | + |
| 93 | + int f[3] = {1, 2}; |
| 94 | + |
| 95 | + int j[1]; |
| 96 | +}; |
| 97 | +} |
| 98 | + |
| 99 | +const char name[] = "Some string"; |
| 100 | +// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] |
| 101 | + |
| 102 | +void takeCharArray(const char name[]); |
| 103 | +// CHECK-MESSAGES: :[[@LINE-1]]:26: warning: do not declare C-style arrays, use 'std::array' or 'std::vector' instead [modernize-avoid-c-arrays] |
| 104 | + |
| 105 | +using MyIntArray = int[10]; |
| 106 | +// CHECK-MESSAGES: :[[@LINE-1]]:20: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] |
| 107 | + |
| 108 | +using MyIntArray2D = MyIntArray[10]; |
| 109 | +// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] |
| 110 | +// CHECK-MESSAGES: :[[@LINE-2]]:22: note: declared type 'MyIntArray[10]' resolves to 'int[10][10]' |
| 111 | + |
| 112 | +using MyIntArray3D = MyIntArray2D[10]; |
| 113 | +// CHECK-MESSAGES: :[[@LINE-1]]:22: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] |
| 114 | +// CHECK-MESSAGES: :[[@LINE-2]]:22: note: declared type 'MyIntArray2D[10]' resolves to 'int[10][10][10]' |
| 115 | + |
| 116 | +MyIntArray3D array3D; |
| 117 | +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] |
| 118 | +// CHECK-MESSAGES: :[[@LINE-2]]:1: note: declared type 'MyIntArray3D' resolves to 'int[10][10][10]' |
| 119 | + |
| 120 | +MyIntArray2D array2D; |
| 121 | +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] |
| 122 | +// CHECK-MESSAGES: :[[@LINE-2]]:1: note: declared type 'MyIntArray2D' resolves to 'int[10][10]' |
| 123 | + |
| 124 | +MyIntArray array1D; |
| 125 | +// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] |
| 126 | +// CHECK-MESSAGES: :[[@LINE-2]]:1: note: declared type 'MyIntArray' resolves to 'int[10]' |
0 commit comments