brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.1 KiB · b7a87bf Raw
78 lines · plain
1.. title:: clang-tidy - modernize-avoid-c-arrays2 3modernize-avoid-c-arrays4========================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 13For parameters of incomplete C-style array type, it would be better to14use ``std::span`` / ``gsl::span`` as replacement.15 16However, fix-it are potentially dangerous in header files and are therefore not17emitted right now.18 19.. code:: c++20 21  int a[] = {1, 2}; // warning: do not declare C-style arrays, use 'std::array' instead22 23  int b[1]; // warning: do not declare C-style arrays, use 'std::array' instead24 25  void foo() {26    int c[b[0]]; // warning: do not declare C VLA arrays, use 'std::vector' instead27  }28 29  template <typename T, int Size>30  class array {31    T d[Size]; // warning: do not declare C-style arrays, use 'std::array' instead32 33    int e[1]; // warning: do not declare C-style arrays, use 'std::array' instead34  };35 36  array<int[4], 2> d; // warning: do not declare C-style arrays, use 'std::array' instead37 38  using k = int[4]; // warning: do not declare C-style arrays, use 'std::array' instead39 40 41However, the ``extern "C"`` code is ignored, since it is common to share42such headers between C code, and C++ code.43 44.. code:: c++45 46  // Some header47  extern "C" {48 49  int f[] = {1, 2}; // not diagnosed50 51  int j[1]; // not diagnosed52 53  inline void bar() {54    {55      int j[j[0]]; // not diagnosed56    }57  }58 59  }60 61Similarly, the ``main()`` function is ignored. Its second and third parameters62can be either ``char* argv[]`` or ``char** argv``, but cannot be63``std::array<>``.64 65Options66-------67 68.. option:: AllowStringArrays69 70  When set to `true` (default is `false`), variables of character array type71  with deduced length, initialized directly from string literals, will be ignored.72  This option doesn't affect cases where length can't be deduced, resembling73  pointers, as seen in class members and parameters. Example:74 75  .. code:: c++76 77    const char name[] = "Some name";78