Skip to content

Commit 7261cb4

Browse files
committed
address comments
1 parent cd6de85 commit 7261cb4

File tree

2 files changed

+37
-44
lines changed

2 files changed

+37
-44
lines changed

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

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -84,51 +84,53 @@ UseStartsEndsWithCheck::UseStartsEndsWithCheck(StringRef Name,
8484
void UseStartsEndsWithCheck::registerMatchers(MatchFinder *Finder) {
8585
const auto ZeroLiteral = integerLiteral(equals(0));
8686

87-
const auto ClassTypeWithMethod =
88-
[](const StringRef MethodBoundName,
89-
const llvm::ArrayRef<StringRef> &Methods) {
90-
const auto Method =
91-
cxxMethodDecl(isConst(), parameterCountIs(1),
92-
returns(booleanType()), hasAnyName(Methods))
93-
.bind(MethodBoundName);
94-
return qualType(hasCanonicalType(hasDeclaration(cxxRecordDecl(
95-
anyOf(hasMethod(Method),
96-
hasAnyBase(hasType(hasCanonicalType(
97-
hasDeclaration(cxxRecordDecl(hasMethod(Method)))))))))));
98-
};
99-
100-
const auto OnClassWithStartsWithFunction = on(hasType(ClassTypeWithMethod(
101-
"starts_with_fun", {"starts_with", "startsWith", "startswith"})));
102-
103-
const auto OnClassWithEndsWithFunction =
104-
on(expr(hasType(ClassTypeWithMethod(
105-
"ends_with_fun", {"ends_with", "endsWith", "endswith"})))
106-
.bind("haystack"));
87+
const auto ClassTypeWithMethod = [](const StringRef MethodBoundName,
88+
const auto... Methods) {
89+
return cxxRecordDecl(anyOf(
90+
hasMethod(cxxMethodDecl(isConst(), parameterCountIs(1),
91+
returns(booleanType()), hasAnyName(Methods))
92+
.bind(MethodBoundName))...));
93+
};
94+
95+
const auto OnClassWithStartsWithFunction =
96+
ClassTypeWithMethod("starts_with_fun", "starts_with", "startsWith",
97+
"startswith", "StartsWith");
98+
99+
const auto OnClassWithEndsWithFunction = ClassTypeWithMethod(
100+
"ends_with_fun", "ends_with", "endsWith", "endswith", "EndsWith");
107101

108102
// Case 1: X.find(Y) [!=]= 0 -> starts_with.
109103
const auto FindExpr = cxxMemberCallExpr(
110104
anyOf(argumentCountIs(1), hasArgument(1, ZeroLiteral)),
111-
callee(cxxMethodDecl(hasName("find")).bind("find_fun")),
112-
OnClassWithStartsWithFunction, hasArgument(0, expr().bind("needle")));
105+
callee(
106+
cxxMethodDecl(hasName("find"), ofClass(OnClassWithStartsWithFunction))
107+
.bind("find_fun")),
108+
hasArgument(0, expr().bind("needle")));
113109

114110
// Case 2: X.rfind(Y, 0) [!=]= 0 -> starts_with.
115111
const auto RFindExpr = cxxMemberCallExpr(
116112
hasArgument(1, ZeroLiteral),
117-
callee(cxxMethodDecl(hasName("rfind")).bind("find_fun")),
118-
OnClassWithStartsWithFunction, hasArgument(0, expr().bind("needle")));
113+
callee(cxxMethodDecl(hasName("rfind"),
114+
ofClass(OnClassWithStartsWithFunction))
115+
.bind("find_fun")),
116+
hasArgument(0, expr().bind("needle")));
119117

120118
// Case 3: X.compare(0, LEN(Y), Y) [!=]= 0 -> starts_with.
121119
const auto CompareExpr = cxxMemberCallExpr(
122120
argumentCountIs(3), hasArgument(0, ZeroLiteral),
123-
callee(cxxMethodDecl(hasName("compare")).bind("find_fun")),
124-
OnClassWithStartsWithFunction, hasArgument(2, expr().bind("needle")),
121+
callee(cxxMethodDecl(hasName("compare"),
122+
ofClass(OnClassWithStartsWithFunction))
123+
.bind("find_fun")),
124+
hasArgument(2, expr().bind("needle")),
125125
hasArgument(1, lengthExprForStringNode("needle")));
126126

127127
// Case 4: X.compare(LEN(X) - LEN(Y), LEN(Y), Y) [!=]= 0 -> ends_with.
128128
const auto CompareEndsWithExpr = cxxMemberCallExpr(
129129
argumentCountIs(3),
130-
callee(cxxMethodDecl(hasName("compare")).bind("find_fun")),
131-
OnClassWithEndsWithFunction, hasArgument(2, expr().bind("needle")),
130+
callee(cxxMethodDecl(hasName("compare"),
131+
ofClass(OnClassWithEndsWithFunction))
132+
.bind("find_fun")),
133+
on(expr().bind("haystack")), hasArgument(2, expr().bind("needle")),
132134
hasArgument(1, lengthExprForStringNode("needle")),
133135
hasArgument(0,
134136
binaryOperator(hasOperatorName("-"),
@@ -159,8 +161,10 @@ void UseStartsEndsWithCheck::registerMatchers(MatchFinder *Finder) {
159161
1,
160162
anyOf(declRefExpr(to(varDecl(hasName("npos")))),
161163
memberExpr(member(hasName("npos"))))))),
162-
callee(cxxMethodDecl(hasName("rfind")).bind("find_fun")),
163-
OnClassWithEndsWithFunction,
164+
callee(cxxMethodDecl(hasName("rfind"),
165+
ofClass(OnClassWithEndsWithFunction))
166+
.bind("find_fun")),
167+
on(expr().bind("haystack")),
164168
hasArgument(0, expr().bind("needle")))
165169
.bind("find_expr"),
166170
binaryOperator(hasOperatorName("-"),

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

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,9 @@ struct prefer_underscore_version_flip {
3232
size_t find(const char *s, size_t pos = 0) const;
3333
};
3434

35-
struct prefer_underscore_version_inherit : public string_like {
36-
bool startsWith(const char *s) const;
37-
};
38-
3935
void test(std::string s, std::string_view sv, sub_string ss, sub_sub_string sss,
4036
string_like sl, string_like_camel slc, prefer_underscore_version puv,
41-
prefer_underscore_version_flip puvf,
42-
prefer_underscore_version_inherit puvi) {
37+
prefer_underscore_version_flip puvf) {
4338
s.find("a") == 0;
4439
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with instead of find() == 0
4540
// CHECK-FIXES: s.starts_with("a");
@@ -150,14 +145,8 @@ void test(std::string s, std::string_view sv, sub_string ss, sub_sub_string sss,
150145
// CHECK-FIXES: puv.starts_with("a");
151146

152147
puvf.find("a") == 0;
153-
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use startsWith
154-
// CHECK-FIXES: puvf.startsWith("a");
155-
156-
// Here, the subclass has startsWith, the superclass has starts_with.
157-
// We prefer the version from the subclass.
158-
puvi.find("a") == 0;
159-
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use startsWith
160-
// CHECK-FIXES: puvi.startsWith("a");
148+
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with
149+
// CHECK-FIXES: puvf.starts_with("a");
161150

162151
s.compare(0, 1, "a") == 0;
163152
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with instead of compare() == 0

0 commit comments

Comments
 (0)