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

Julian Schmidt via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 19 17:23:33 PST 2024


https://github.com/5chmidti commented:

You could try to implement the check with `TK_TraverseUnlessSpelledInSource` if you'd like, which would only add two more matchers via `addMatcher`, and the traversal kind function (https://godbolt.org/z/GrfMTxeGa). IMO, this can be done in a follow-up by someone else as well.

`TK_TraversUnlessSpelledInSource` would not only have performance implications, but also niche correctness improvements: 
```diff
struct IntSpan {
    IntSpan(int*);
    IntSpan subspan(int, int);
};

template <typename T>
void test_instantiations(T val) {
-    val.subspan(0, 5);
+    val.first(5); // `IntSpan` does not have `first`
}

void test_intantiations() {
    int array[] = {0, 1, 2, 3, 4};
    auto sp = std::span<int>{array, 5};
    test_instantiations(sp);
    IntSpan intsp = array;
    test_instantiations(intsp);
}
```

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


More information about the cfe-commits mailing list