[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:17 PDT 2024


================
@@ -0,0 +1,66 @@
+namespace std {
+  template<typename T, unsigned size>
+  struct array {
+    T operator[](unsigned i) {
+      return T{1};
+    }
+    T at(unsigned i) {
+      return T{1};
+    }
+  };
+
+  template<typename T>
+  struct unique_ptr {
+    T operator[](unsigned i) {
+      return T{1};
+    }
+  };
+
+  template<typename T>
+  struct span {
+    T operator[](unsigned i) {
+      return T{1};
+    }
+  };
+} // namespace std
+
+namespace json {
+  template<typename T>
+  struct node{
+    T operator[](unsigned i) {
+      return T{1};
+    }
+  };
+} // namespace json
+
+
+// RUN: %check_clang_tidy %s cppcoreguidelines-avoid-bounds-errors %t
+std::array<int, 3> a;
----------------
PiotrZSL wrote:

add tests with:
- std::map,
- where operator[] is behind macro
- where argument to operator is behind macro
- where object is behind macro
- where class derive from std::map/array
- where std::array is behind typedef
- where object is an template parameter, and method is called once with class that has .at and once with class that has not
- ...


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


More information about the cfe-commits mailing list