Skip to content

Commit 26ebe24

Browse files
committed
change format
1 parent e03d67e commit 26ebe24

File tree

3 files changed

+19
-22
lines changed

3 files changed

+19
-22
lines changed

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -229,29 +229,26 @@ void UseStartsEndsWithCheck::check(const MatchFinder::MatchResult &Result) {
229229
CharSourceRange::getTokenRange(SearchExpr->getSourceRange()),
230230
*Result.SourceManager, Result.Context->getLangOpts());
231231

232-
auto Diag = diag(FindExpr->getExprLoc(),
233-
FindFun->getName() == "substr"
234-
? "use %0 instead of %1() %select{==|!=}2"
235-
: "use %0 instead of %1() %select{==|!=}2 0")
236-
<< ReplacementFunction->getName() << FindFun->getName() << Neg;
232+
auto Diagnostic = diag(FindExpr->getExprLoc(), "use %0 instead of %1")
233+
<< ReplacementFunction->getName() << FindFun->getName();
237234

238235
// Remove everything before the function call.
239-
Diag << FixItHint::CreateRemoval(CharSourceRange::getCharRange(
236+
Diagnostic << FixItHint::CreateRemoval(CharSourceRange::getCharRange(
240237
ComparisonExpr->getBeginLoc(), FindExpr->getBeginLoc()));
241238

242239
// Rename the function to `starts_with` or `ends_with`.
243-
Diag << FixItHint::CreateReplacement(FindExpr->getExprLoc(),
244-
ReplacementFunction->getName());
240+
Diagnostic << FixItHint::CreateReplacement(FindExpr->getExprLoc(),
241+
ReplacementFunction->getName());
245242

246243
// Replace arguments and everything after the function call.
247-
Diag << FixItHint::CreateReplacement(
244+
Diagnostic << FixItHint::CreateReplacement(
248245
CharSourceRange::getTokenRange(FindExpr->getArg(0)->getBeginLoc(),
249246
ComparisonExpr->getEndLoc()),
250247
(SearchExprText + ")").str());
251248

252249
// Add negation if necessary.
253250
if (Neg)
254-
Diag << FixItHint::CreateInsertion(FindExpr->getBeginLoc(), "!");
251+
Diagnostic << FixItHint::CreateInsertion(FindExpr->getBeginLoc(), "!");
255252
}
256253

257254
} // namespace clang::tidy::modernize

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ Changes in existing checks
245245
- Improved :doc:`modernize-use-starts-ends-with
246246
<clang-tidy/checks/modernize/use-starts-ends-with>` check to handle two new
247247
cases from ``rfind`` and ``compare`` to ``ends_with``, and one new case from
248-
``substr`` to ``starts_with``.
248+
``substr`` to ``starts_with``, and a small adjustment to the diagnostic message.
249249

250250
- Improved :doc:`modernize-use-std-format
251251
<clang-tidy/checks/modernize/use-std-format>` check to support replacing

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ void test(std::string s, std::string_view sv, sub_string ss, sub_sub_string sss,
3636
string_like sl, string_like_camel slc, prefer_underscore_version puv,
3737
prefer_underscore_version_flip puvf) {
3838
s.find("a") == 0;
39-
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with instead of find() == 0
39+
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with instead of find [modernize-use-starts-ends-with]
4040
// CHECK-FIXES: s.starts_with("a");
4141

4242
(((((s)).find("a")))) == ((0));
@@ -68,7 +68,7 @@ void test(std::string s, std::string_view sv, sub_string ss, sub_sub_string sss,
6868
// CHECK-FIXES: !s.starts_with("a");
6969

7070
s.rfind("a", 0) == 0;
71-
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with instead of rfind() == 0
71+
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with instead of rfind [modernize-use-starts-ends-with]
7272
// CHECK-FIXES: s.starts_with("a");
7373

7474
s.rfind(s, 0) == 0;
@@ -139,11 +139,11 @@ void test(std::string s, std::string_view sv, sub_string ss, sub_sub_string sss,
139139
// CHECK-FIXES: puvf.starts_with("a");
140140

141141
s.compare(0, 1, "a") == 0;
142-
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with instead of compare() == 0
142+
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with instead of compare [modernize-use-starts-ends-with]
143143
// CHECK-FIXES: s.starts_with("a");
144144

145145
s.compare(0, 1, "a") != 0;
146-
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with instead of compare() != 0
146+
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with instead of compare [modernize-use-starts-ends-with]
147147
// CHECK-FIXES: !s.starts_with("a");
148148

149149
s.compare(0, strlen("a"), "a") == 0;
@@ -269,17 +269,17 @@ void test_substr() {
269269

270270
// Basic pattern
271271
str.substr(0, 5) == "hello";
272-
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with instead of substr() == [modernize-use-starts-ends-with]
272+
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with instead of substr [modernize-use-starts-ends-with]
273273
// CHECK-FIXES: str.starts_with("hello");
274274

275275
// With string literal on left side
276276
"hello" == str.substr(0, 5);
277-
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with instead of substr() == [modernize-use-starts-ends-with]
277+
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with instead of substr [modernize-use-starts-ends-with]
278278
// CHECK-FIXES: str.starts_with("hello");
279279

280280
// Inequality comparison
281281
str.substr(0, 5) != "world";
282-
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with instead of substr() != [modernize-use-starts-ends-with]
282+
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with instead of substr [modernize-use-starts-ends-with]
283283
// CHECK-FIXES: !str.starts_with("world");
284284

285285
// Ensure non-zero start position is not transformed
@@ -291,21 +291,21 @@ void test_substr() {
291291

292292
// String literal with size calculation
293293
str.substr(0, strlen("hello")) == "hello";
294-
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with instead of substr() == [modernize-use-starts-ends-with]
294+
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with instead of substr [modernize-use-starts-ends-with]
295295
// CHECK-FIXES: str.starts_with("hello");
296296

297297
str.substr(0, prefix.size()) == prefix;
298-
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with instead of substr() == [modernize-use-starts-ends-with]
298+
// 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

301301
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]
302+
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with instead of substr [modernize-use-starts-ends-with]
303303
// CHECK-FIXES: str.starts_with(prefix);
304304

305305
// Tests to verify macro behavior
306306
#define MSG "hello"
307307
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]
308+
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with instead of substr [modernize-use-starts-ends-with]
309309
// CHECK-FIXES: str.starts_with(MSG);
310310

311311
#define STARTS_WITH(X, Y) (X).substr(0, (Y).size()) == (Y)

0 commit comments

Comments
 (0)