You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[clang-tidy] Access checks not done classes derived of std::array
Index accessing checks are not performed for derived classes of
of `std::array`, as only `std::array` itself and its aliases
seems to be checked.
This patch aims to extend it for derived classes such as:
```
template<class T, size_t N>
class DerivedArray : public std::array<T, N> {};
```
Reviewed By: PiotrZSL
Differential Revision: https://reviews.llvm.org/D156624
Copy file name to clipboardExpand all lines: clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/pro-bounds-constant-array-index.cpp
+78Lines changed: 78 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -23,6 +23,9 @@ constexpr int const_index(int base) {
23
23
return base + 3;
24
24
}
25
25
26
+
template<classT, size_t N>
27
+
classDerivedArray : publicstd::array<T, N> {};
28
+
26
29
voidf(std::array<int, 10> a, int pos) {
27
30
a [ pos / 2/*comment*/] = 1;
28
31
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use array subscript when the index is not an integer constant expression [cppcoreguidelines-pro-bounds-constant-array-index]
@@ -68,6 +71,81 @@ void f(std::array<int, 10> a, int pos) {
68
71
m[const_index(6)] = 3; // OK, constant index and inside bounds
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use array subscript when the index is not an integer constant expression [cppcoreguidelines-pro-bounds-constant-array-index]
88
+
int j = a[pos - 1];
89
+
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: do not use array subscript when the index is not an integer constant expression
90
+
91
+
a.at(pos-1) = 2; // OK, at() instead of []
92
+
gsl::at(a, pos-1) = 2; // OK, gsl::at() instead of []
93
+
94
+
a[-1] = 3;
95
+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: std::array<> index -1 is negative [cppcoreguidelines-pro-bounds-constant-array-index]
96
+
a[10] = 4;
97
+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: std::array<> index 10 is past the end of the array (which contains 10 elements) [cppcoreguidelines-pro-bounds-constant-array-index]
98
+
99
+
a[const_index(7)] = 3;
100
+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: std::array<> index 10 is past the end of the array (which contains 10 elements)
101
+
102
+
a[0] = 3; // OK, constant index and inside bounds
103
+
a[1] = 3; // OK, constant index and inside bounds
104
+
a[9] = 3; // OK, constant index and inside bounds
105
+
a[const_index(6)] = 3; // OK, constant index and inside bounds
106
+
107
+
using MyArray = DerivedArray<int, 10>;
108
+
MyArray m{};
109
+
m [ pos / 2/*comment*/] = 1;
110
+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not use array subscript when the index is not an integer constant expression [cppcoreguidelines-pro-bounds-constant-array-index]
111
+
int jj = m[pos - 1];
112
+
// CHECK-MESSAGES: :[[@LINE-1]]:12: warning: do not use array subscript when the index is not an integer constant expression
113
+
114
+
m.at(pos-1) = 2; // OK, at() instead of []
115
+
gsl::at(m, pos-1) = 2; // OK, gsl::at() instead of []
116
+
m[-1] = 3;
117
+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: std::array<> index -1 is negative [cppcoreguidelines-pro-bounds-constant-array-index]
118
+
m[10] = 4;
119
+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: std::array<> index 10 is past the end of the array (which contains 10 elements) [cppcoreguidelines-pro-bounds-constant-array-index]
120
+
121
+
m[const_index(7)] = 3;
122
+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: std::array<> index 10 is past the end of the array (which contains 10 elements)
123
+
124
+
m[0] = 3; // OK, constant index and inside bounds
125
+
m[1] = 3; // OK, constant index and inside bounds
126
+
m[9] = 3; // OK, constant index and inside bounds
127
+
m[const_index(6)] = 3; // OK, constant index and inside bounds
128
+
129
+
using MyPrivateArray = PrivateDerivedArray<int, 10>;
130
+
MyPrivateArray pm{};
131
+
pm [ pos / 2/*comment*/] = 1;
132
+
int jjj = pm[pos - 1];
133
+
134
+
pm.at(pos-1) = 2; // OK, at() instead of []
135
+
pm[-1] = 3;
136
+
pm[10] = 4;
137
+
138
+
pm[const_index(7)] = 3;
139
+
140
+
pm[0] = 3; // OK, constant index and inside bounds
141
+
pm[1] = 3; // OK, constant index and inside bounds
142
+
pm[9] = 3; // OK, constant index and inside bounds
143
+
pm[const_index(6)] = 3; // OK, constant index and inside bounds
0 commit comments