Skip to content

Commit 3bed9be

Browse files
committed
feedback
1 parent 115a760 commit 3bed9be

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff 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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Checks for common roundabout ways to express ``starts_with`` and ``ends_with``
77
and suggests replacing with the simpler method when it is available. Notably,
88
this will work with ``std::string`` and ``std::string_view``.
99

10-
The check handles the following expressions:
10+
Covered scenarios:
1111

1212
==================================================== =====================
1313
Expression Replacement

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

0 commit comments

Comments
 (0)