| 1 | // RUN: %check_clang_tidy -std=c++17 %s modernize-avoid-c-arrays %t |
| 2 | |
| 3 | int a[] = {1, 2}; |
| 4 | // CHECK-MESSAGES: :[[@LINE-1]]:1: warning: do not declare C-style arrays, use 'std::array' instead |
| 5 | |
| 6 | int b[1]; |
| 7 | // CHECK-MESSAGES: :[[@LINE-1]]:1: warning: do not declare C-style arrays, use 'std::array' instead |
| 8 | |
| 9 | void foo() { |
| 10 | int c[b[0]]; |
| 11 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C VLA arrays, use 'std::vector' instead |
| 12 | |
| 13 | using d = decltype(c); |
| 14 | d e; |
| 15 | // Semi-FIXME: we do not diagnose these last two lines separately, |
| 16 | // because we point at typeLoc.getBeginLoc(), which is the decl before that |
| 17 | // (int c[b[0]];), which is already diagnosed. |
| 18 | } |
| 19 | |
| 20 | template <typename T, int Size> |
| 21 | class array { |
| 22 | T d[Size]; |
| 23 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use 'std::array' instead |
| 24 | |
| 25 | int e[1]; |
| 26 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use 'std::array' instead |
| 27 | }; |
| 28 | |
| 29 | array<int[4], 2> d; |
| 30 | // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not declare C-style arrays, use 'std::array' instead |
| 31 | |
| 32 | using k = int[4]; |
| 33 | // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: do not declare C-style arrays, use 'std::array' instead |
| 34 | |
| 35 | array<k, 2> dk; |
| 36 | |
| 37 | template <typename T> |
| 38 | class unique_ptr { |
| 39 | T *d; |
| 40 | |
| 41 | int e[1]; |
| 42 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use 'std::array' instead |
| 43 | }; |
| 44 | |
| 45 | unique_ptr<int[]> d2; |
| 46 | // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: do not declare C-style arrays, use 'std::array' instead |
| 47 | |
| 48 | using k2 = int[]; |
| 49 | // CHECK-MESSAGES: :[[@LINE-1]]:12: warning: do not declare C-style arrays, use 'std::array' instead |
| 50 | |
| 51 | unique_ptr<k2> dk2; |
| 52 | |
| 53 | // Some header |
| 54 | extern "C" { |
| 55 | |
| 56 | int f[] = {1, 2}; |
| 57 | |
| 58 | int j[1]; |
| 59 | |
| 60 | inline void bar() { |
| 61 | { |
| 62 | int j[j[0]]; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | extern "C++" { |
| 67 | int f3[] = {1, 2}; |
| 68 | // CHECK-MESSAGES: :[[@LINE-1]]:1: warning: do not declare C-style arrays, use 'std::array' instead |
| 69 | |
| 70 | int j3[1]; |
| 71 | // CHECK-MESSAGES: :[[@LINE-1]]:1: warning: do not declare C-style arrays, use 'std::array' instead |
| 72 | |
| 73 | struct Foo { |
| 74 | int f3[3] = {1, 2}; |
| 75 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use 'std::array' instead |
| 76 | |
| 77 | int j3[1]; |
| 78 | // CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use 'std::array' instead |
| 79 | }; |
| 80 | } |
| 81 | |
| 82 | struct Bar { |
| 83 | |
| 84 | int f[3] = {1, 2}; |
| 85 | |
| 86 | int j[1]; |
| 87 | }; |
| 88 | } |
| 89 | |
| 90 | const char name[] = "Some string" ; |
| 91 | // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays] |
| 92 | |
| 93 | void takeCharArray(const char name[]); |
| 94 | // CHECK-MESSAGES: :[[@LINE-1]]:26: warning: do not declare C-style arrays, use 'std::array' or 'std::vector' instead [modernize-avoid-c-arrays] |
| 95 | |