@@ -84,51 +84,53 @@ UseStartsEndsWithCheck::UseStartsEndsWithCheck(StringRef Name,
8484void 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 (" -" ),
0 commit comments