[clang-tools-extra] Enforce SL.con.3: Add check to replace operator[] with at() (PR #90043)

Piotr Zegar via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 25 10:18:16 PDT 2024


================
@@ -0,0 +1,20 @@
+.. title:: clang-tidy - cppcoreguidelines-avoid-bounds-errors
+
+cppcoreguidelines-avoid-bounds-errors
+=====================================
+
+This check enforces the `SL.con.3 <https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#slcon3-avoid-bounds-errors>` guideline.
+It flags all uses of `operator[]` on `std::vector`, `std::array`, `std::deque`, `std::map`, `std::unordered_map`, and `std::flat_map` and suggests to replace it with `at()`.
+Note that `std::span` and `std::mdspan` do not support `at()` as of C++23, so the use of `operator[]` is not flagged.
+
+For example the code
+
+.. code-block:: c++
+  std::array<int, 3> a;
+  int b = a[4];
+
+will be replaced by 
+
+.. code-block:: c++
+  std::vector<int, 3> a;
+  int b = a.at(4);
----------------
PiotrZSL wrote:

consider adding option to specify excluded classes, if class detection will be automatic.

https://github.com/llvm/llvm-project/pull/90043


More information about the cfe-commits mailing list