File tree Expand file tree Collapse file tree 3 files changed +10
-2
lines changed Expand file tree Collapse file tree 3 files changed +10
-2
lines changed Original file line number Diff line number Diff line change @@ -240,6 +240,9 @@ void UseStartsEndsWithCheck::check(const MatchFinder::MatchResult &Result) {
240240 ReplacementFunction->getName ());
241241
242242 // Replace arguments and everything after the function call.
243+ if (FindExpr->getNumArgs () == 0 ) {
244+ return ;
245+ }
243246 Diag << FixItHint::CreateReplacement (
244247 CharSourceRange::getTokenRange (FindExpr->getArg (0 )->getBeginLoc (),
245248 ComparisonExpr->getEndLoc ()),
@@ -248,7 +251,6 @@ void UseStartsEndsWithCheck::check(const MatchFinder::MatchResult &Result) {
248251 // Add negation if necessary.
249252 if (Neg)
250253 Diag << FixItHint::CreateInsertion (FindExpr->getBeginLoc (), " !" );
251-
252254}
253255
254256} // namespace clang::tidy::modernize
Original file line number Diff line number Diff line change @@ -7,7 +7,7 @@ Checks for common roundabout ways to express ``starts_with`` and ``ends_with``
77and suggests replacing with the simpler method when it is available. Notably,
88this will work with ``std::string `` and ``std::string_view ``.
99
10- The check handles the following expressions :
10+ Covered scenarios :
1111
1212==================================================== =====================
1313Expression Replacement
Original file line number Diff line number Diff line change @@ -298,6 +298,10 @@ void test_substr() {
298298 // CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with instead of substr() == [modernize-use-starts-ends-with]
299299 // CHECK-FIXES: str.starts_with(prefix);
300300
301+ str.substr (0 , prefix.length ()) == prefix;
302+ // CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with instead of substr() == [modernize-use-starts-ends-with]
303+ // CHECK-FIXES: str.starts_with(prefix);
304+
301305 // Tests to verify macro behavior
302306 #define STARTS_WITH (X, Y ) (X).substr(0 , (Y).size()) == (Y)
303307 STARTS_WITH (str, prefix);
@@ -308,4 +312,6 @@ void test_substr() {
308312 #define STR () str
309313 SUBSTR (STR (), 0 , 6 ) == " prefix" ;
310314 " prefix" == SUBSTR (STR (), 0 , 6 );
315+
316+ str.substr (0 , strlen (" hello123" )) == " hello" ;
311317}
You can’t perform that action at this time.
0 commit comments