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

Helmut Januschka via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 5 00:04:08 PST 2024


================
@@ -18,108 +18,77 @@ using namespace clang::ast_matchers;
 namespace clang::tidy::readability {
 
 void UseSpanFirstLastCheck::registerMatchers(MatchFinder *Finder) {
-  // Match span::subspan calls
   const auto HasSpanType =
       hasType(hasUnqualifiedDesugaredType(recordType(hasDeclaration(
           classTemplateSpecializationDecl(hasName("::std::span"))))));
 
-  Finder->addMatcher(cxxMemberCallExpr(callee(memberExpr(hasDeclaration(
-                                           cxxMethodDecl(hasName("subspan"))))),
-                                       on(expr(HasSpanType)))
-                         .bind("subspan"),
-                     this);
+  // Match span.subspan(0, n) -> first(n)
+  Finder->addMatcher(
+      cxxMemberCallExpr(
+          callee(memberExpr(hasDeclaration(cxxMethodDecl(hasName("subspan"))))),
+          on(expr(HasSpanType).bind("span_object")),
+          hasArgument(0, integerLiteral(equals(0))),
+          hasArgument(1, expr().bind("count")), argumentCountIs(2))
+          .bind("first_subspan"),
+      this);
+
+  // Match span.subspan(size() - n) -> last(n)
+  const auto SizeCall = cxxMemberCallExpr(
+      callee(memberExpr(hasDeclaration(cxxMethodDecl(hasName("size"))))));
----------------
hjanuschka wrote:

thanks, great feedback, added matchers and test cases for ranges

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


More information about the cfe-commits mailing list