• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1.. title:: clang-tidy - modernize-avoid-c-arrays
2
3modernize-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
10Finds C-style array types and recommend to use ``std::array<>`` /
11``std::vector<>``. All types of C arrays are diagnosed.
12
13However, fix-it are potentially dangerous in header files and are therefore not
14emitted 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
38However, the ``extern "C"`` code is ignored, since it is common to share
39such 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
58Similarly, the ``main()`` function is ignored. Its second and third parameters
59can be either ``char* argv[]`` or ``char** argv``, but can not be
60``std::array<>``.
61