Skip to content

Commit 79838fc

Browse files
committed
feedback
1 parent f948b16 commit 79838fc

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

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

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@
1818
using namespace clang::ast_matchers;
1919

2020
namespace clang::tidy::modernize {
21+
22+
static bool isNegativeComparison(const Expr *ComparisonExpr) {
23+
if (const auto *BO = llvm::dyn_cast<BinaryOperator>(ComparisonExpr))
24+
return BO->getOpcode() == BO_NE;
25+
26+
if (const auto *Op = llvm::dyn_cast<CXXOperatorCallExpr>(ComparisonExpr))
27+
return Op->getOperator() == OO_ExclaimEqual;
28+
29+
return false;
30+
}
31+
2132
struct NotLengthExprForStringNode {
2233
NotLengthExprForStringNode(std::string ID, DynTypedNode Node,
2334
ASTContext *Context)
@@ -71,17 +82,6 @@ struct NotLengthExprForStringNode {
7182
ASTContext *Context;
7283
};
7384

74-
static bool isNegativeComparison(const Expr *ComparisonExpr) {
75-
if (const auto *BO = llvm::dyn_cast<BinaryOperator>(ComparisonExpr)) {
76-
return BO->getOpcode() == BO_NE;
77-
}
78-
79-
if (const auto *Op = llvm::dyn_cast<CXXOperatorCallExpr>(ComparisonExpr)) {
80-
return Op->getOperator() == OO_ExclaimEqual;
81-
}
82-
return false;
83-
}
84-
8585
AST_MATCHER_P(Expr, lengthExprForStringNode, std::string, ID) {
8686
return Builder->removeBindings(NotLengthExprForStringNode(
8787
ID, DynTypedNode::create(Node), &(Finder->getASTContext())));
@@ -183,6 +183,7 @@ void UseStartsEndsWithCheck::registerMatchers(MatchFinder *Finder) {
183183
.bind("expr"),
184184
this);
185185

186+
// Case 6: X.substr(0, LEN(Y)) [!=]= Y -> ends_with.
186187
Finder->addMatcher(
187188
cxxOperatorCallExpr(
188189
hasAnyOperatorName("==", "!="),
@@ -216,7 +217,7 @@ void UseStartsEndsWithCheck::check(const MatchFinder::MatchResult &Result) {
216217
if (ComparisonExpr->getBeginLoc().isMacroID())
217218
return;
218219

219-
bool Neg = isNegativeComparison(ComparisonExpr);
220+
const bool Neg = isNegativeComparison(ComparisonExpr);
220221

221222
// Retrieve the source text of the search expression.
222223
const auto SearchExprText = Lexer::getSourceText(
@@ -244,9 +245,9 @@ void UseStartsEndsWithCheck::check(const MatchFinder::MatchResult &Result) {
244245
(SearchExprText + ")").str());
245246

246247
// Add negation if necessary.
247-
if (Neg) {
248+
if (Neg)
248249
Diag << FixItHint::CreateInsertion(FindExpr->getBeginLoc(), "!");
249-
}
250+
250251
}
251252

252253
} // namespace clang::tidy::modernize

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,9 @@ Changes in existing checks
243243
``NULL``/``__null`` (but not ``0``) when used with a templated type.
244244

245245
- Improved :doc:`modernize-use-starts-ends-with
246-
<clang-tidy/checks/modernize/use-starts-ends-with>` check to handle two new
247-
cases from ``rfind`` and ``compare`` to ``ends_with``, and one new case from
248-
``substr`` to ``starts_with``.
246+
<clang-tidy/checks/modernize/use-starts-ends-with>` check to handle two new
247+
cases from ``rfind`` and ``compare`` to ``ends_with``, and one new case from
248+
``substr`` to ``starts_with``.
249249

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

0 commit comments

Comments
 (0)