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] do not diagn. array types in implicit templ. instantiations
So far, the clang-tidy check `modernize-avoid-c-arrays` also diagnosed
array types for type template parameters even though no actual array
type got written there but it got deduced to one. In such case, there
is nothing a developer can do at that location to fix the diagnostic.
Since actually, the location where the template got instantiated
would have to be adjusted. And this is in most cases some totally
distant code where implementers of a template do not have access to.
Also adding suppressions to the declaration of the template is not an
option since that would clutter the code unnecessarily and is in many
cases also simply not possible. Hence, we propose to not diagnose any
occurrence of an array type in an implicit instantiation of a template
but rather at the point where template arguments involve array types.
// within below template decl, no array type findings are expected within the template parameter declarations since not a single C-style array type got written explicitly
// here, however, plenty of array type findings are expected for below template parameter declarations since C-style array types are written explicitly
142
+
template <typename U = T,
143
+
bool = std::is_same_v<U, int[]>,
144
+
// CHECK-MESSAGES: :[[@LINE-1]]:38: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]
145
+
bool = std::is_same<U, int[10]>::value,
146
+
// CHECK-MESSAGES: :[[@LINE-1]]:36: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]
// CHECK-MESSAGES: :[[@LINE-1]]:51: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]
152
+
typename = std::remove_const_t<int[]>>
153
+
// CHECK-MESSAGES: :[[@LINE-1]]:44: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]
154
+
classMyInnerClassTemplate {
155
+
public:
156
+
MyInnerClassTemplate(const U&) {}
157
+
private:
158
+
U field[3];
159
+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]
160
+
};
161
+
162
+
MyClassTemplate(const T&) {}
163
+
164
+
private:
165
+
T field[7];
166
+
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]
167
+
};
168
+
169
+
// an explicit instantiation
170
+
template
171
+
classMyClassTemplate<int[2]>;
172
+
// CHECK-MESSAGES: :[[@LINE-1]]:23: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]
173
+
174
+
using MyArrayType = int[3];
175
+
// CHECK-MESSAGES: :[[@LINE-1]]:21: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]
176
+
177
+
// another explicit instantiation
178
+
template
179
+
classMyClassTemplate<MyArrayType>;
180
+
// no diagnostic is expected here since no C-style array type got written here
181
+
182
+
// within below template decl, no array type findings are expected within the template parameter declarations since not a single C-style array type got written explicitly
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]
198
+
199
+
T array2[2];
200
+
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]
201
+
202
+
T value;
203
+
}
204
+
205
+
// here, however, plenty of array type findings are expected for below template parameter declarations since C-style array types are written explicitly
206
+
template <typename T = int[],
207
+
// CHECK-MESSAGES: :[[@LINE-1]]:24: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]
208
+
bool = std::is_same_v<T, int[]>,
209
+
// CHECK-MESSAGES: :[[@LINE-1]]:36: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]
210
+
bool = std::is_same<T, int[10]>::value,
211
+
// CHECK-MESSAGES: :[[@LINE-1]]:34: warning: do not declare C-style arrays, use 'std::array' instead [modernize-avoid-c-arrays]
0 commit comments