[clang-tools-extra] [clang-tidy] Add readability-use-span-first-last check (PR #118074)

Baranov Victor via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 10 10:34:37 PDT 2026


================
@@ -0,0 +1,194 @@
+// RUN: %check_clang_tidy -std=c++20 %s readability-use-span-first-last %t
+
+namespace std {
+
+enum class byte : unsigned char {};
+
+template <typename T>
+class span {
+  T* ptr;
+  __SIZE_TYPE__ len;
+
+public:
+  span(T* p, __SIZE_TYPE__ l) : ptr(p), len(l) {}
+  
+  span<T> subspan(__SIZE_TYPE__ offset) const {
+    return span(ptr + offset, len - offset);
+  }
+  
+  span<T> subspan(__SIZE_TYPE__ offset, __SIZE_TYPE__ count) const {
+    return span(ptr + offset, count);
+  }
+
+  span<T> first(__SIZE_TYPE__ count) const {
+    return span(ptr, count);
+  }
+
+  span<T> last(__SIZE_TYPE__ count) const {
+    return span(ptr + (len - count), count);
+  }
+
+  __SIZE_TYPE__ size() const { return len; }
+  __SIZE_TYPE__ size_bytes() const { return len * sizeof(T); }
+};
+} // namespace std
+
+// Add here, right after the std namespace closes:
+namespace std::ranges {
+  template<typename T>
+  __SIZE_TYPE__ size(const span<T>& s) { return s.size(); }
+}
----------------
vbvictor wrote:

This should be a separate header under `clang-tools-extra/test/clang-tidy/checkers/Inputs/Headers/std` after https://github.com/llvm/llvm-project/pull/185533 is merged. And you don't need to do any manual `-I`

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


More information about the cfe-commits mailing list