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

Paul Heidekrüger via cfe-commits cfe-commits at lists.llvm.org
Tue May 27 07:23:50 PDT 2025


================
@@ -0,0 +1,50 @@
+.. title:: clang-tidy - cppcoreguidelines-pro-bounds-avoid-unchecked-container-accesses
+
+cppcoreguidelines-pro-bounds-avoid-unchecked-container-accesses
+===============================================================
+
+Flags calls to ``operator[]`` in STL containers and suggests replacing it with
+safe alternatives.
+
+For example, both
+
+.. code-block:: c++
+
+  std::vector<int> a;
+  int b = a[4];
+
+and
+
+.. code-block:: c++
+
+  std::unique_ptr<vector> a;
+  int b = a[0];
+
+will generate a warning.
+
+STL containers with well-defined behavior for ``operator[]`` are excluded from this
+check.
+
+This check enforces part of the `SL.con.3
+<https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#slcon3-avoid-bounds-errors>`
+guideline and is part of the `Bounds Safety (Bounds 4)
+<https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Pro-bounds-arrayindex>`
+profile from the C++ Core Guidelines.
+
+Options
+-------
+
+.. option:: ExcludeClasses
+
+    Semicolon-delimited list of class names that should additionally be
----------------
paulhdk wrote:

Ah, I see! The semantics change. Now the option overwrites the default exclusions (vs extending them). I've implemented your requested changes and as per the suggestion [on discourse](https://discourse.llvm.org/t/rfc-teach-check-clang-tidy-py-the-filecheck-implicit-check-not-option/86480), I've split the tests using `split-file`. Closing this as completed. Feel free to re-open if I missed anything! Thanks for your feedback, @vbvictor - much appreciated!

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


More information about the cfe-commits mailing list