[clang-tools-extra] [clang-tidy] rewrite matchers in modernize-use-starts-ends-with (PR #112101)

Nicolas van Kempen via cfe-commits cfe-commits at lists.llvm.org
Sat Oct 12 11:32:07 PDT 2024


================
@@ -82,34 +84,25 @@ UseStartsEndsWithCheck::UseStartsEndsWithCheck(StringRef Name,
 void UseStartsEndsWithCheck::registerMatchers(MatchFinder *Finder) {
   const auto ZeroLiteral = integerLiteral(equals(0));
 
-  const auto HasStartsWithMethodWithName = [](const std::string &Name) {
-    return hasMethod(
-        cxxMethodDecl(hasName(Name), isConst(), parameterCountIs(1))
-            .bind("starts_with_fun"));
-  };
-  const auto HasStartsWithMethod =
-      anyOf(HasStartsWithMethodWithName("starts_with"),
-            HasStartsWithMethodWithName("startsWith"),
-            HasStartsWithMethodWithName("startswith"));
-  const auto OnClassWithStartsWithFunction =
-      on(hasType(hasCanonicalType(hasDeclaration(cxxRecordDecl(
-          anyOf(HasStartsWithMethod,
-                hasAnyBase(hasType(hasCanonicalType(
-                    hasDeclaration(cxxRecordDecl(HasStartsWithMethod)))))))))));
-
-  const auto HasEndsWithMethodWithName = [](const std::string &Name) {
-    return hasMethod(
-        cxxMethodDecl(hasName(Name), isConst(), parameterCountIs(1))
-            .bind("ends_with_fun"));
-  };
-  const auto HasEndsWithMethod = anyOf(HasEndsWithMethodWithName("ends_with"),
-                                       HasEndsWithMethodWithName("endsWith"),
-                                       HasEndsWithMethodWithName("endswith"));
+  const auto ClassTypeWithMethod =
+      [](const StringRef MethodBoundName,
+         const llvm::ArrayRef<StringRef> &Methods) {
+        const auto Method =
+            cxxMethodDecl(isConst(), parameterCountIs(1),
+                          returns(booleanType()), hasAnyName(Methods))
+                .bind(MethodBoundName);
+        return qualType(hasCanonicalType(hasDeclaration(cxxRecordDecl(
+            anyOf(hasMethod(Method),
+                  hasAnyBase(hasType(hasCanonicalType(
+                      hasDeclaration(cxxRecordDecl(hasMethod(Method)))))))))));
----------------
nicovank wrote:

I don't think we care much about the case where a class inheriting a starts-with-class prefers a different overload, so maybe we can simplify to this:
```
        return cxxRecordDecl(hasMethod(Method));
      };

  const auto OnClassWithStartsWithFunction =
      callee(cxxMethodDecl(ofClass(ClassTypeWithMethod(
          "starts_with_fun", {"starts_with", "startsWith", "startswith"}))));

  const auto OnClassWithEndsWithFunction = cxxMemberCallExpr(
      callee(cxxMethodDecl(ofClass(ClassTypeWithMethod(
          "ends_with_fun", {"ends_with", "endsWith", "endswith"})))),
      on(expr().bind("haystack")));
```

This will require deleting the `prefer_underscore_version_inherit` test case that was testing this behavior, I think that's fine.

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


More information about the cfe-commits mailing list