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

Manuel Pietsch via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 3 01:47:30 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;
----------------
leunam99 wrote:

As suggested, we added a test where the object is a template parameter and the method is called once with a class that has `at()` and once with a class that has not, but we are not sure what the best behaviour is. Should we 

-  not warn, because the fix suggested in the message will lead the other instantiation to not compile?
- warn at the place that requires the template instantiation?
- keep the warning and add the name of the class of the object / the template parameters that lead to the message?

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


More information about the cfe-commits mailing list