Skip to content

Commit 5c52bea

Browse files
committed
[clang-tidy][modernize-use-starts-ends-with] Fix minor mistake in error message
In one of the cases recently added to this check in #110448, the error message is no longer accurate as the comparison is not with zero. #116033 brought my attention to this as it may add another comparison without zero. Remove the `[!=] 0` part of the diagnostic. This is in line with some other checks e.g. modernize-use-emplace. ``` > cat tmp.cpp #include <string> bool f(std::string u, std::string v) { return u.rfind(v) == u.size() - v.size(); } # Before. > ./build/bin/clang-tidy -checks="-*,modernize-use-starts-ends-with" tmp.cpp -- -std=c++20 tmp.cpp:3:12: warning: use ends_with instead of rfind() == 0 [modernize-use-starts-ends-with] 3 | return u.rfind(v) == u.size() - v.size(); | ^~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~ | ends_with( ) # After. > ./build/bin/clang-tidy -checks="-*,modernize-use-starts-ends-with" tmp.cpp -- -std=c++20 tmp.cpp:3:12: warning: use ends_with instead of rfind [modernize-use-starts-ends-with] 3 | return u.rfind(v) == u.size() - v.size(); | ^~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~ | ends_with( ) ```
1 parent 92604cf commit 5c52bea

File tree

3 files changed

+10
-12
lines changed

3 files changed

+10
-12
lines changed

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,8 @@ void UseStartsEndsWithCheck::check(const MatchFinder::MatchResult &Result) {
189189
if (ComparisonExpr->getBeginLoc().isMacroID())
190190
return;
191191

192-
const bool Neg = ComparisonExpr->getOpcode() == BO_NE;
193-
194-
auto Diagnostic =
195-
diag(FindExpr->getExprLoc(), "use %0 instead of %1() %select{==|!=}2 0")
196-
<< ReplacementFunction->getName() << FindFun->getName() << Neg;
192+
auto Diagnostic = diag(FindExpr->getExprLoc(), "use %0 instead of %1")
193+
<< ReplacementFunction->getName() << FindFun->getName();
197194

198195
// Remove possible arguments after search expression and ' [!=]= .+' suffix.
199196
Diagnostic << FixItHint::CreateReplacement(
@@ -215,7 +212,7 @@ void UseStartsEndsWithCheck::check(const MatchFinder::MatchResult &Result) {
215212
(ReplacementFunction->getName() + "(").str());
216213

217214
// Add possible negation '!'.
218-
if (Neg)
215+
if (ComparisonExpr->getOpcode() == BO_NE)
219216
Diagnostic << FixItHint::CreateInsertion(FindExpr->getBeginLoc(), "!");
220217
}
221218

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,8 @@ Changes in existing checks
244244

245245
- Improved :doc:`modernize-use-starts-ends-with
246246
<clang-tidy/checks/modernize/use-starts-ends-with>` check to handle two cases
247-
that can be replaced with ``ends_with``
247+
that can be replaced with ``ends_with``, and a small adjustment to the
248+
diagnostic message.
248249

249250
- Improved :doc:`modernize-use-std-format
250251
<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: 5 additions & 5 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;
@@ -149,11 +149,11 @@ void test(std::string s, std::string_view sv, sub_string ss, sub_sub_string sss,
149149
// CHECK-FIXES: puvf.starts_with("a");
150150

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

155155
s.compare(0, 1, "a") != 0;
156-
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with instead of compare() != 0
156+
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use starts_with
157157
// CHECK-FIXES: !s.starts_with("a");
158158

159159
s.compare(0, strlen("a"), "a") == 0;
@@ -211,7 +211,7 @@ void test(std::string s, std::string_view sv, sub_string ss, sub_sub_string sss,
211211
// CHECK-FIXES: s.ends_with(suffix);
212212

213213
s.rfind("suffix") == s.size() - 6;
214-
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use ends_with
214+
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use ends_with instead of rfind [modernize-use-starts-ends-with]
215215
// CHECK-FIXES: s.ends_with("suffix");
216216

217217
s.rfind("suffix") == s.size() - strlen("suffix");

0 commit comments

Comments
 (0)