Debian Codegraph @

clang-tools-extra/docs/clang-tidy/checks/modernize/avoid-c-arrays.rst

raw
1 .. title:: clang-tidy - modernize-avoid-c-arrays
2
3 modernize-avoid-c-arrays
4 ========================
5
6 `cppcoreguidelines-avoid-c-arrays` redirects here as an alias for this check.
7
8 `hicpp-avoid-c-arrays` redirects here as an alias for this check.
9
10 Finds C-style array types and recommend to use ``std::array<>`` /
11 ``std::vector<>``. All types of C arrays are diagnosed.
12
13 However, fix-it are potentially dangerous in header files and are therefore not
14 emitted right now.
15
16 .. code:: c++
17
18 int a[] = {1, 2}; // warning: do not declare C-style arrays, use std::array<> instead
19
20 int b[1]; // warning: do not declare C-style arrays, use std::array<> instead
21
22 void foo() {
23 int c[b[0]]; // warning: do not declare C VLA arrays, use std::vector<> instead
24 }
25
26 template <typename T, int Size>
27 class array {
28 T d[Size]; // warning: do not declare C-style arrays, use std::array<> instead
29
30 int e[1]; // warning: do not declare C-style arrays, use std::array<> instead
31 };
32
33 array<int[4], 2> d; // warning: do not declare C-style arrays, use std::array<> instead
34
35 using k = int[4]; // warning: do not declare C-style arrays, use std::array<> instead
36
37
38 However, the ``extern "C"`` code is ignored, since it is common to share
39 such headers between C code, and C++ code.
40
41 .. code:: c++
42
43 // Some header
44 extern "C" {
45
46 int f[] = {1, 2}; // not diagnosed
47
48 int j[1]; // not diagnosed
49
50 inline void bar() {
51 {
52 int j[j[0]]; // not diagnosed
53 }
54 }
55
56 }
57
58 Similarly, the ``main()`` function is ignored. Its second and third parameters
59 can be either ``char* argv[]`` or ``char** argv``, but cannot be
60 ``std::array<>``.
61
62 .. option:: AllowStringArrays
63
64 When set to `true` (default is `false`), variables of character array type
65 with deduced length, initialized directly from string literals, will be ignored.
66 This option doesn't affect cases where length can't be deduced, resembling
67 pointers, as seen in class members and parameters. Example:
68
69 .. code:: c++
70
71 const char name[] = "Some name";