1// RUN: %check_clang_tidy %s modernize-avoid-c-arrays %t
2
3int a[] = {1, 2};
4// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: do not declare C-style arrays, use std::array<> instead
5
6int b[1];
7// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: do not declare C-style arrays, use std::array<> instead
8
9void 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
20template <typename T, int Size>
21class 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
29array<int[4], 2> d;
30// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not declare C-style arrays, use std::array<> instead
31
32using k = int[4];
33// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: do not declare C-style arrays, use std::array<> instead
34
35array<k, 2> dk;
36
37template <typename T>
38class 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
45unique_ptr<int[]> d2;
46// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: do not declare C-style arrays, use std::array<> instead
47
48using k2 = int[];
49// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: do not declare C-style arrays, use std::array<> instead
50
51unique_ptr<k2> dk2;
52
53// Some header
54extern "C" {
55
56int f[] = {1, 2};
57
58int j[1];
59
60inline void bar() {
61 {
62 int j[j[0]];
63 }
64}
65
66extern "C++" {
67int f3[] = {1, 2};
68// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: do not declare C-style arrays, use std::array<> instead
69
70int j3[1];
71// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: do not declare C-style arrays, use std::array<> instead
72
73struct 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
82struct Bar {
83
84 int f[3] = {1, 2};
85
86 int j[1];
87};
88}
89
90const 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
93void takeCharArray(const char name[]);
94// CHECK-MESSAGES: :[[@LINE-1]]:26: warning: do not declare C-style arrays, use std::array<> instead [modernize-avoid-c-arrays]
95

source code of clang-tools-extra/test/clang-tidy/checkers/modernize/avoid-c-arrays.cpp