Author: Baranov Victor Date: 2025-03-22T08:07:58-04:00 New Revision: 2909c420f6b1919989819d91d5194d5d50f3910d
URL: https://github.com/llvm/llvm-project/commit/2909c420f6b1919989819d91d5194d5d50f3910d DIFF: https://github.com/llvm/llvm-project/commit/2909c420f6b1919989819d91d5194d5d50f3910d.diff LOG: [clang-tidy] modernize-use-starts-ends-with: fix false positives on `find` and `rfind` (#129564) Also document cases with two or three arguments (matching default arguments) this check matches. Closes https://github.com/llvm/llvm-project/issues/129498. Added: Modified: clang-tools-extra/clang-tidy/modernize/UseStartsEndsWithCheck.cpp clang-tools-extra/docs/ReleaseNotes.rst clang-tools-extra/docs/clang-tidy/checks/modernize/use-starts-ends-with.rst clang-tools-extra/test/clang-tidy/checkers/modernize/use-starts-ends-with.cpp Removed: ################################################################################ diff --git a/clang-tools-extra/clang-tidy/modernize/UseStartsEndsWithCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseStartsEndsWithCheck.cpp index ab72c6796bb4c..2e059f24d47b6 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseStartsEndsWithCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseStartsEndsWithCheck.cpp @@ -113,21 +113,33 @@ void UseStartsEndsWithCheck::registerMatchers(MatchFinder *Finder) { const auto OnClassWithEndsWithFunction = ClassTypeWithMethod( "ends_with_fun", "ends_with", "endsWith", "endswith", "EndsWith"); - // Case 1: X.find(Y) [!=]= 0 -> starts_with. + // Case 1: X.find(Y, [0], [LEN(Y)]) [!=]= 0 -> starts_with. const auto FindExpr = cxxMemberCallExpr( - anyOf(argumentCountIs(1), hasArgument(1, ZeroLiteral)), callee( cxxMethodDecl(hasName("find"), ofClass(OnClassWithStartsWithFunction)) .bind("find_fun")), - hasArgument(0, expr().bind("needle"))); - - // Case 2: X.rfind(Y, 0) [!=]= 0 -> starts_with. + hasArgument(0, expr().bind("needle")), + anyOf( + // Detect the expression: X.find(Y); + argumentCountIs(1), + // Detect the expression: X.find(Y, 0); + allOf(argumentCountIs(2), hasArgument(1, ZeroLiteral)), + // Detect the expression: X.find(Y, 0, LEN(Y)); + allOf(argumentCountIs(3), hasArgument(1, ZeroLiteral), + hasArgument(2, lengthExprForStringNode("needle"))))); + + // Case 2: X.rfind(Y, 0, [LEN(Y)]) [!=]= 0 -> starts_with. const auto RFindExpr = cxxMemberCallExpr( - hasArgument(1, ZeroLiteral), callee(cxxMethodDecl(hasName("rfind"), ofClass(OnClassWithStartsWithFunction)) .bind("find_fun")), - hasArgument(0, expr().bind("needle"))); + hasArgument(0, expr().bind("needle")), + anyOf( + // Detect the expression: X.rfind(Y, 0); + allOf(argumentCountIs(2), hasArgument(1, ZeroLiteral)), + // Detect the expression: X.rfind(Y, 0, LEN(Y)); + allOf(argumentCountIs(3), hasArgument(1, ZeroLiteral), + hasArgument(2, lengthExprForStringNode("needle"))))); // Case 3: X.compare(0, LEN(Y), Y) [!=]= 0 -> starts_with. const auto CompareExpr = cxxMemberCallExpr( diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 72aa05eb4dcd1..ed7da975f3de7 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -175,6 +175,11 @@ Changes in existing checks <clang-tidy/checks/modernize/use-ranges>` check by updating suppress warnings logic for ``nullptr`` in ``std::find``. +- Improved :doc:`modernize-use-starts-ends-with + <clang-tidy/checks/modernize/use-starts-ends-with>` check by adding more + matched scenarios of ``find`` and ``rfind`` methods and fixing false + positives when those methods were called with 3 arguments. + - Improved :doc:`modernize-use-std-numbers <clang-tidy/checks/modernize/use-std-numbers>` check to support math functions of diff erent precisions. diff --git a/clang-tools-extra/docs/clang-tidy/checks/modernize/use-starts-ends-with.rst b/clang-tools-extra/docs/clang-tidy/checks/modernize/use-starts-ends-with.rst index 78cd900885ac3..1babc2d1660ec 100644 --- a/clang-tools-extra/docs/clang-tidy/checks/modernize/use-starts-ends-with.rst +++ b/clang-tools-extra/docs/clang-tidy/checks/modernize/use-starts-ends-with.rst @@ -13,7 +13,10 @@ Covered scenarios: Expression Replacement ---------------------------------------------------- --------------------- ``u.find(v) == 0`` ``u.starts_with(v)`` +``u.find(v, 0) == 0`` ``u.starts_with(v)`` +``u.find(v, 0, v.size()) == 0`` ``u.starts_with(v)`` ``u.rfind(v, 0) != 0`` ``!u.starts_with(v)`` +``u.rfind(v, 0, v.size()) != 0`` ``!u.starts_with(v)`` ``u.compare(0, v.size(), v) == 0`` ``u.starts_with(v)`` ``u.substr(0, v.size()) == v`` ``u.starts_with(v)`` ``v != u.substr(0, v.size())`` ``!u.starts_with(v)`` diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-starts-ends-with.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-starts-ends-with.cpp index 8699ca03ba331..cd8463401e45a 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-starts-ends-with.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-starts-ends-with.cpp @@ -236,6 +236,42 @@ void test(std::string s, std::string_view sv, sub_string ss, sub_sub_string sss, // CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use ends_with // CHECK-FIXES: s.ends_with(suffix); + s.find("a", 0) == 0; + // CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with + // CHECK-FIXES: s.starts_with("a"); + + s.find(s, ZERO) == 0; + // CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with + // CHECK-FIXES: s.starts_with(s); + + s.find(s, 0) == ZERO; + // CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with + // CHECK-FIXES: s.starts_with(s); + + s.find("aaa", 0, 3) == 0; + // CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with + // CHECK-FIXES: s.starts_with("aaa"); + + s.find("aaa", ZERO, 3) == 0; + // CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with + // CHECK-FIXES: s.starts_with("aaa"); + + s.find("aaa", ZERO, strlen(("aaa"))) == ZERO; + // CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with + // CHECK-FIXES: s.starts_with("aaa"); + + s.rfind("aaa", 0, 3) != 0; + // CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with + // CHECK-FIXES: !s.starts_with("aaa"); + + s.rfind("aaa", ZERO, 3) != 0; + // CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with + // CHECK-FIXES: !s.starts_with("aaa"); + + s.rfind("aaa", ZERO, strlen(("aaa"))) == ZERO; + // CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with + // CHECK-FIXES: s.starts_with("aaa"); + struct S { std::string s; } t; @@ -261,6 +297,12 @@ void test(std::string s, std::string_view sv, sub_string ss, sub_sub_string sss, #define STRING s if (0 == STRING.find("ala")) { /* do something */} + + // Cases when literal-size and size parameters are diff erent are not being matched. + s.find("aaa", 0, 2) == 0; + s.find("aaa", 0, strlen("aa")) == 0; + s.rfind("aaa", 0, 2) == 0; + s.rfind("aaa", 0, strlen("aa")) == 0; } void test_substr() { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits