65 lines · plain
1.. title:: clang-tidy - cppcoreguidelines-pro-bounds-avoid-unchecked-container-access2 3cppcoreguidelines-pro-bounds-avoid-unchecked-container-access4=============================================================5 6Finds calls to ``operator[]`` in STL containers and suggests replacing them7with safe alternatives.8Safe alternatives include STL ``at`` or GSL ``at`` functions, ``begin()`` or9``end()`` functions, ``range-for`` loops, ``std::span``, or an appropriate10function from ``<algorithms>``.11 12For example, both13 14.. code-block:: c++15 16 std::vector<int> a;17 int b = a[4];18 19and20 21.. code-block:: c++22 23 std::unique_ptr<vector> a;24 int b = a[0];25 26will generate a warning.27 28STL containers for which ``operator[]`` is well-defined for all inputs are29excluded from this check (e.g.: ``std::map::operator[]``).30 31This check enforces part of the `SL.con.332<https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#slcon3-avoid-bounds-errors>`_33guideline and is part of the `Bounds Safety (Bounds 4)34<https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#pro-bounds-arrayindex>`_35profile from the C++ Core Guidelines.36 37Options38-------39 40.. option:: ExcludeClasses41 42 Semicolon-separated list of regular expressions matching class names that43 overwrites the default exclusion list. The default is:44 `::std::map;::std::unordered_map;::std::flat_map`.45 46.. option:: FixMode47 48 Determines what fixes are suggested. Either `none`, `at` (use49 ``a.at(index)`` if a fitting function exists) or `function` (use a50 function ``f(a, index)``). The default is `none`.51 52.. option:: FixFunction53 54 The function to use in the `function` mode. For C++23 and beyond, the55 passed function must support the empty subscript operator, i.e., the case56 where ``a[]`` becomes ``f(a)``. :option:`FixFunctionEmptyArgs` can be57 used to override the suggested function in that case. The default is `gsl::at`.58 59.. option:: FixFunctionEmptyArgs60 61 The function to use in the `function` mode for the empty subscript operator62 case in C++23 and beyond only. If no fixes should be made for empty63 subscript operators, pass an empty string. In that case, only the warnings64 will be printed. The default is the value of :option:`FixFunction`.65