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

Nicolas van Kempen via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 22 17:00:17 PST 2024


================
@@ -183,38 +210,43 @@ void UseStartsEndsWithCheck::check(const MatchFinder::MatchResult &Result) {
   const auto *EndsWithFunction =
       Result.Nodes.getNodeAs<CXXMethodDecl>("ends_with_fun");
   assert(bool(StartsWithFunction) != bool(EndsWithFunction));
+
   const CXXMethodDecl *ReplacementFunction =
       StartsWithFunction ? StartsWithFunction : EndsWithFunction;
 
-  if (ComparisonExpr->getBeginLoc().isMacroID())
+  if (ComparisonExpr->getBeginLoc().isMacroID() ||
+      FindExpr->getBeginLoc().isMacroID())
     return;
 
-  const bool Neg = ComparisonExpr->getOpcode() == BO_NE;
+  // Make sure FindExpr->getArg(0) can be used to make a range in the FitItHint.
+  if (FindExpr->getNumArgs() == 0)
+    return;
 
-  auto Diagnostic =
-      diag(FindExpr->getExprLoc(), "use %0 instead of %1() %select{==|!=}2 0")
-      << ReplacementFunction->getName() << FindFun->getName() << Neg;
+  const bool Neg = isNegativeComparison(ComparisonExpr);
----------------
nicovank wrote:

Nit: `Neg` is not used here anymore, move it into the if statement:
```cpp
if (isNegativeComparison(ComparisonExpr))
  Diagnostic << FixItHint::CreateInsertion(FindExpr->getBeginLoc(), "!");
```

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


More information about the cfe-commits mailing list