|
18 | 18 | using namespace clang::ast_matchers; |
19 | 19 |
|
20 | 20 | 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 | + |
21 | 32 | struct NotLengthExprForStringNode { |
22 | 33 | NotLengthExprForStringNode(std::string ID, DynTypedNode Node, |
23 | 34 | ASTContext *Context) |
@@ -71,17 +82,6 @@ struct NotLengthExprForStringNode { |
71 | 82 | ASTContext *Context; |
72 | 83 | }; |
73 | 84 |
|
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 | | - |
85 | 85 | AST_MATCHER_P(Expr, lengthExprForStringNode, std::string, ID) { |
86 | 86 | return Builder->removeBindings(NotLengthExprForStringNode( |
87 | 87 | ID, DynTypedNode::create(Node), &(Finder->getASTContext()))); |
@@ -183,6 +183,7 @@ void UseStartsEndsWithCheck::registerMatchers(MatchFinder *Finder) { |
183 | 183 | .bind("expr"), |
184 | 184 | this); |
185 | 185 |
|
| 186 | + // Case 6: X.substr(0, LEN(Y)) [!=]= Y -> ends_with. |
186 | 187 | Finder->addMatcher( |
187 | 188 | cxxOperatorCallExpr( |
188 | 189 | hasAnyOperatorName("==", "!="), |
@@ -216,7 +217,7 @@ void UseStartsEndsWithCheck::check(const MatchFinder::MatchResult &Result) { |
216 | 217 | if (ComparisonExpr->getBeginLoc().isMacroID()) |
217 | 218 | return; |
218 | 219 |
|
219 | | - bool Neg = isNegativeComparison(ComparisonExpr); |
| 220 | + const bool Neg = isNegativeComparison(ComparisonExpr); |
220 | 221 |
|
221 | 222 | // Retrieve the source text of the search expression. |
222 | 223 | const auto SearchExprText = Lexer::getSourceText( |
@@ -244,9 +245,9 @@ void UseStartsEndsWithCheck::check(const MatchFinder::MatchResult &Result) { |
244 | 245 | (SearchExprText + ")").str()); |
245 | 246 |
|
246 | 247 | // Add negation if necessary. |
247 | | - if (Neg) { |
| 248 | + if (Neg) |
248 | 249 | Diag << FixItHint::CreateInsertion(FindExpr->getBeginLoc(), "!"); |
249 | | - } |
| 250 | + |
250 | 251 | } |
251 | 252 |
|
252 | 253 | } // namespace clang::tidy::modernize |
0 commit comments