[clang-tools-extra] [clang-tidy] Enhance modernize-use-starts-ends-with to handle substr patterns (PR #116033)

via cfe-commits cfe-commits at lists.llvm.org
Sun Nov 17 17:49:01 PST 2024


github-actions[bot] wrote:

<!--LLVM CODE FORMAT COMMENT: {clang-format}-->


:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff 5845688e91d85d46c0f47daaf4edfdfc772853cf 23b4bcdf52041aad1c5581e0f7dc01028770a154 --extensions h,cpp -- clang-tools-extra/clang-tidy/modernize/UseStartsEndsWithCheck.cpp clang-tools-extra/clang-tidy/modernize/UseStartsEndsWithCheck.h clang-tools-extra/test/clang-tidy/checkers/modernize/use-starts-ends-with.cpp
``````````

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/clang-tools-extra/clang-tidy/modernize/UseStartsEndsWithCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseStartsEndsWithCheck.cpp
index 12ff31dfa0..6aabbf809d 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseStartsEndsWithCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseStartsEndsWithCheck.cpp
@@ -187,16 +187,15 @@ void UseStartsEndsWithCheck::registerMatchers(MatchFinder *Finder) {
       cxxOperatorCallExpr(
           hasAnyOperatorName("==", "!="),
           anyOf(
-              hasOperands(
-                  cxxMemberCallExpr(
-                      argumentCountIs(2), hasArgument(0, ZeroLiteral),
-                      hasArgument(1, lengthExprForStringNode("needle")),
-                      callee(
-                          cxxMethodDecl(hasName("substr"),
-                                        ofClass(OnClassWithStartsWithFunction))
-                              .bind("find_fun")))
-                      .bind("find_expr"),
-                  expr().bind("needle")),
+              hasOperands(cxxMemberCallExpr(
+                              argumentCountIs(2), hasArgument(0, ZeroLiteral),
+                              hasArgument(1, lengthExprForStringNode("needle")),
+                              callee(cxxMethodDecl(
+                                         hasName("substr"),
+                                         ofClass(OnClassWithStartsWithFunction))
+                                         .bind("find_fun")))
+                              .bind("find_expr"),
+                          expr().bind("needle")),
               hasOperands(expr().bind("needle"),
                           cxxMemberCallExpr(
                               argumentCountIs(2), hasArgument(0, ZeroLiteral),
@@ -210,31 +209,31 @@ void UseStartsEndsWithCheck::registerMatchers(MatchFinder *Finder) {
       this);
 }
 
-bool UseStartsEndsWithCheck::isNegativeComparison(const Expr* ComparisonExpr) {
+bool UseStartsEndsWithCheck::isNegativeComparison(const Expr *ComparisonExpr) {
   // Handle direct != operator
   if (const auto *BO = llvm::dyn_cast<BinaryOperator>(ComparisonExpr)) {
     return BO->getOpcode() == BO_NE;
   }
-  
+
   // Handle operator!= call
   if (const auto *Op = llvm::dyn_cast<CXXOperatorCallExpr>(ComparisonExpr)) {
     return Op->getOperator() == OO_ExclaimEqual;
   }
-  
+
   // Handle rewritten !(expr == expr)
   if (const auto *UO = llvm::dyn_cast<UnaryOperator>(ComparisonExpr)) {
     if (UO->getOpcode() == UO_LNot) {
-      if (const auto *InnerBO = 
-          llvm::dyn_cast<BinaryOperator>(UO->getSubExpr()->IgnoreParens())) {
+      if (const auto *InnerBO = llvm::dyn_cast<BinaryOperator>(
+              UO->getSubExpr()->IgnoreParens())) {
         return InnerBO->getOpcode() == BO_EQ;
       }
-      if (const auto *InnerOp = 
-          llvm::dyn_cast<CXXOperatorCallExpr>(UO->getSubExpr()->IgnoreParens())) {
+      if (const auto *InnerOp = llvm::dyn_cast<CXXOperatorCallExpr>(
+              UO->getSubExpr()->IgnoreParens())) {
         return InnerOp->getOperator() == OO_EqualEqual;
       }
     }
   }
-  
+
   return false;
 }
 
diff --git a/clang-tools-extra/clang-tidy/modernize/UseStartsEndsWithCheck.h b/clang-tools-extra/clang-tidy/modernize/UseStartsEndsWithCheck.h
index 9a19206847..be2bddbf19 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseStartsEndsWithCheck.h
+++ b/clang-tools-extra/clang-tidy/modernize/UseStartsEndsWithCheck.h
@@ -25,7 +25,7 @@ public:
   UseStartsEndsWithCheck(StringRef Name, ClangTidyContext *Context);
   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
-  bool isNegativeComparison(const Expr* ComparisonExpr);
+  bool isNegativeComparison(const Expr *ComparisonExpr);
   bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
     return LangOpts.CPlusPlus;
   }

``````````

</details>


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


More information about the cfe-commits mailing list