File tree Expand file tree Collapse file tree 2 files changed +9
-4
lines changed Expand file tree Collapse file tree 2 files changed +9
-4
lines changed Original file line number Diff line number Diff line change @@ -183,7 +183,7 @@ void UseStartsEndsWithCheck::registerMatchers(MatchFinder *Finder) {
183183 .bind (" expr" ),
184184 this );
185185
186- // Case 6: X.substr(0, LEN(Y)) [!=]= Y -> ends_with .
186+ // Case 6: X.substr(0, LEN(Y)) [!=]= Y -> starts_with .
187187 Finder->addMatcher (
188188 cxxOperatorCallExpr (
189189 hasAnyOperatorName (" ==" , " !=" ),
@@ -218,6 +218,9 @@ void UseStartsEndsWithCheck::check(const MatchFinder::MatchResult &Result) {
218218 FindExpr->getBeginLoc ().isMacroID ())
219219 return ;
220220
221+ if (FindExpr->getNumArgs () == 0 )
222+ return ;
223+
221224 const bool Neg = isNegativeComparison (ComparisonExpr);
222225
223226 // Retrieve the source text of the search expression.
@@ -240,9 +243,6 @@ void UseStartsEndsWithCheck::check(const MatchFinder::MatchResult &Result) {
240243 ReplacementFunction->getName ());
241244
242245 // Replace arguments and everything after the function call.
243- if (FindExpr->getNumArgs () == 0 ) {
244- return ;
245- }
246246 Diag << FixItHint::CreateReplacement (
247247 CharSourceRange::getTokenRange (FindExpr->getArg (0 )->getBeginLoc (),
248248 ComparisonExpr->getEndLoc ()),
Original file line number Diff line number Diff line change @@ -303,6 +303,11 @@ void test_substr() {
303303 // CHECK-FIXES: str.starts_with(prefix);
304304
305305 // Tests to verify macro behavior
306+ #define MSG " hello"
307+ str.substr (0 , strlen (MSG)) == MSG;
308+ // CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with instead of substr() == [modernize-use-starts-ends-with]
309+ // CHECK-FIXES: str.starts_with(MSG);
310+
306311 #define STARTS_WITH (X, Y ) (X).substr(0 , (Y).size()) == (Y)
307312 STARTS_WITH (str, prefix);
308313
You can’t perform that action at this time.
0 commit comments