aboutsummaryrefslogtreecommitdiff
path: root/docs/clang-tidy/checks/modernize-avoid-c-arrays.rst
blob: d7bc7474e27f87718edab6ac94ceb6dad92ebbad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
.. title:: clang-tidy - modernize-avoid-c-arrays

modernize-avoid-c-arrays
========================

`cppcoreguidelines-avoid-c-arrays` redirects here as an alias for this check.

`hicpp-avoid-c-arrays` redirects here as an alias for this check.

Finds C-style array types and recommend to use ``std::array<>`` /
``std::vector<>``. All types of C arrays are diagnosed.

However, fix-it are potentially dangerous in header files and are therefore not
emitted right now.

.. code:: c++

  int a[] = {1, 2}; // warning: do not declare C-style arrays, use std::array<> instead

  int b[1]; // warning: do not declare C-style arrays, use std::array<> instead

  void foo() {
    int c[b[0]]; // warning: do not declare C VLA arrays, use std::vector<> instead
  }

  template <typename T, int Size>
  class array {
    T d[Size]; // warning: do not declare C-style arrays, use std::array<> instead

    int e[1]; // warning: do not declare C-style arrays, use std::array<> instead
  };

  array<int[4], 2> d; // warning: do not declare C-style arrays, use std::array<> instead

  using k = int[4]; // warning: do not declare C-style arrays, use std::array<> instead


However, the ``extern "C"`` code is ignored, since it is common to share
such headers between C code, and C++ code.

.. code:: c++

  // Some header
  extern "C" {

  int f[] = {1, 2}; // not diagnosed

  int j[1]; // not diagnosed

  inline void bar() {
    {
      int j[j[0]]; // not diagnosed
    }
  }

  }

Similarly, the ``main()`` function is ignored. Its second and third parameters
can be either ``char* argv[]`` or ``char** argv``, but can not be
``std::array<>``.