Skip to content

Commit 2909c42

Browse files
authored
[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 #129498.
1 parent 2a45222 commit 2909c42

File tree

4 files changed

+69
-7
lines changed

4 files changed

+69
-7
lines changed

clang-tools-extra/clang-tidy/modernize/UseStartsEndsWithCheck.cpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,21 +113,33 @@ void UseStartsEndsWithCheck::registerMatchers(MatchFinder *Finder) {
113113
const auto OnClassWithEndsWithFunction = ClassTypeWithMethod(
114114
"ends_with_fun", "ends_with", "endsWith", "endswith", "EndsWith");
115115

116-
// Case 1: X.find(Y) [!=]= 0 -> starts_with.
116+
// Case 1: X.find(Y, [0], [LEN(Y)]) [!=]= 0 -> starts_with.
117117
const auto FindExpr = cxxMemberCallExpr(
118-
anyOf(argumentCountIs(1), hasArgument(1, ZeroLiteral)),
119118
callee(
120119
cxxMethodDecl(hasName("find"), ofClass(OnClassWithStartsWithFunction))
121120
.bind("find_fun")),
122-
hasArgument(0, expr().bind("needle")));
123-
124-
// Case 2: X.rfind(Y, 0) [!=]= 0 -> starts_with.
121+
hasArgument(0, expr().bind("needle")),
122+
anyOf(
123+
// Detect the expression: X.find(Y);
124+
argumentCountIs(1),
125+
// Detect the expression: X.find(Y, 0);
126+
allOf(argumentCountIs(2), hasArgument(1, ZeroLiteral)),
127+
// Detect the expression: X.find(Y, 0, LEN(Y));
128+
allOf(argumentCountIs(3), hasArgument(1, ZeroLiteral),
129+
hasArgument(2, lengthExprForStringNode("needle")))));
130+
131+
// Case 2: X.rfind(Y, 0, [LEN(Y)]) [!=]= 0 -> starts_with.
125132
const auto RFindExpr = cxxMemberCallExpr(
126-
hasArgument(1, ZeroLiteral),
127133
callee(cxxMethodDecl(hasName("rfind"),
128134
ofClass(OnClassWithStartsWithFunction))
129135
.bind("find_fun")),
130-
hasArgument(0, expr().bind("needle")));
136+
hasArgument(0, expr().bind("needle")),
137+
anyOf(
138+
// Detect the expression: X.rfind(Y, 0);
139+
allOf(argumentCountIs(2), hasArgument(1, ZeroLiteral)),
140+
// Detect the expression: X.rfind(Y, 0, LEN(Y));
141+
allOf(argumentCountIs(3), hasArgument(1, ZeroLiteral),
142+
hasArgument(2, lengthExprForStringNode("needle")))));
131143

132144
// Case 3: X.compare(0, LEN(Y), Y) [!=]= 0 -> starts_with.
133145
const auto CompareExpr = cxxMemberCallExpr(

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ Changes in existing checks
175175
<clang-tidy/checks/modernize/use-ranges>` check by updating suppress
176176
warnings logic for ``nullptr`` in ``std::find``.
177177

178+
- Improved :doc:`modernize-use-starts-ends-with
179+
<clang-tidy/checks/modernize/use-starts-ends-with>` check by adding more
180+
matched scenarios of ``find`` and ``rfind`` methods and fixing false
181+
positives when those methods were called with 3 arguments.
182+
178183
- Improved :doc:`modernize-use-std-numbers
179184
<clang-tidy/checks/modernize/use-std-numbers>` check to support math
180185
functions of different precisions.

clang-tools-extra/docs/clang-tidy/checks/modernize/use-starts-ends-with.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ Covered scenarios:
1313
Expression Replacement
1414
---------------------------------------------------- ---------------------
1515
``u.find(v) == 0`` ``u.starts_with(v)``
16+
``u.find(v, 0) == 0`` ``u.starts_with(v)``
17+
``u.find(v, 0, v.size()) == 0`` ``u.starts_with(v)``
1618
``u.rfind(v, 0) != 0`` ``!u.starts_with(v)``
19+
``u.rfind(v, 0, v.size()) != 0`` ``!u.starts_with(v)``
1720
``u.compare(0, v.size(), v) == 0`` ``u.starts_with(v)``
1821
``u.substr(0, v.size()) == v`` ``u.starts_with(v)``
1922
``v != u.substr(0, v.size())`` ``!u.starts_with(v)``

clang-tools-extra/test/clang-tidy/checkers/modernize/use-starts-ends-with.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,42 @@ void test(std::string s, std::string_view sv, sub_string ss, sub_sub_string sss,
236236
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use ends_with
237237
// CHECK-FIXES: s.ends_with(suffix);
238238

239+
s.find("a", 0) == 0;
240+
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with
241+
// CHECK-FIXES: s.starts_with("a");
242+
243+
s.find(s, ZERO) == 0;
244+
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with
245+
// CHECK-FIXES: s.starts_with(s);
246+
247+
s.find(s, 0) == ZERO;
248+
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with
249+
// CHECK-FIXES: s.starts_with(s);
250+
251+
s.find("aaa", 0, 3) == 0;
252+
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with
253+
// CHECK-FIXES: s.starts_with("aaa");
254+
255+
s.find("aaa", ZERO, 3) == 0;
256+
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with
257+
// CHECK-FIXES: s.starts_with("aaa");
258+
259+
s.find("aaa", ZERO, strlen(("aaa"))) == ZERO;
260+
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with
261+
// CHECK-FIXES: s.starts_with("aaa");
262+
263+
s.rfind("aaa", 0, 3) != 0;
264+
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with
265+
// CHECK-FIXES: !s.starts_with("aaa");
266+
267+
s.rfind("aaa", ZERO, 3) != 0;
268+
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with
269+
// CHECK-FIXES: !s.starts_with("aaa");
270+
271+
s.rfind("aaa", ZERO, strlen(("aaa"))) == ZERO;
272+
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with
273+
// CHECK-FIXES: s.starts_with("aaa");
274+
239275
struct S {
240276
std::string s;
241277
} t;
@@ -261,6 +297,12 @@ void test(std::string s, std::string_view sv, sub_string ss, sub_sub_string sss,
261297

262298
#define STRING s
263299
if (0 == STRING.find("ala")) { /* do something */}
300+
301+
// Cases when literal-size and size parameters are different are not being matched.
302+
s.find("aaa", 0, 2) == 0;
303+
s.find("aaa", 0, strlen("aa")) == 0;
304+
s.rfind("aaa", 0, 2) == 0;
305+
s.rfind("aaa", 0, strlen("aa")) == 0;
264306
}
265307

266308
void test_substr() {

0 commit comments

Comments
 (0)