[clang-tools-extra] [clang-tidy][modernize-use-std-erase] Replace iterator erase-remove idiom with std::(erase/erase_if) (PR #193407)
Baranov Victor via cfe-commits
cfe-commits at lists.llvm.org
Sat Jun 6 12:36:33 PDT 2026
================
@@ -0,0 +1,110 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "UseStdEraseCheck.h"
+#include "../utils/Matchers.h"
+#include "clang/Lex/Lexer.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::modernize {
+
+constexpr std::array<llvm::StringRef, 2> EraseEndMethodNames = {"end", "cend"};
+constexpr std::array<llvm::StringRef, 2> EraseEndFreeNames = {"end", "cend"};
+constexpr const llvm::StringRef EraseThis = "EraseThis";
+
+namespace {
+AST_MATCHER(Expr, hasSideEffects) {
+ return Node.HasSideEffects(Finder->getASTContext());
+}
+} // namespace
+
+static auto
+makeExprMatcher(const ast_matchers::internal::Matcher<Expr> &ArgumentMatcher,
+ ArrayRef<StringRef> MethodNames,
+ ArrayRef<StringRef> FreeNames) {
+ return expr(
+ anyOf(cxxMemberCallExpr(argumentCountIs(0),
+ callee(cxxMethodDecl(hasAnyName(MethodNames))),
+ on(ArgumentMatcher)),
+ callExpr(argumentCountIs(1), hasArgument(0, ArgumentMatcher),
+ hasDeclaration(functionDecl(hasAnyName(FreeNames))))));
+}
+
+static ast_matchers::internal::Matcher<Expr> makeMatcherPair() {
+ const ast_matchers::internal::Matcher<CallExpr> ArgumentMatcher = allOf(
+ hasArgument(
+ 0, makeExprMatcher(expr(unless(hasSideEffects())).bind(EraseThis),
+ {"begin"}, {"::std::begin"})),
+ hasArgument(
+ 1, makeExprMatcher(expr(matchers::isStatementIdenticalToBoundNode(
+ EraseThis.str())),
+ {"end"}, {"::std::end"})),
+ hasArgument(2, expr().bind("valueOrCond")));
+
+ return callExpr(callee(functionDecl(hasAnyName("remove", "remove_if"))),
+ argumentCountIs(3), ArgumentMatcher)
+ .bind("remove");
+}
+
+void UseStdEraseCheck::registerMatchers(MatchFinder *Finder) {
+ const auto IsCpp20EraseContainer = cxxRecordDecl(
+ hasAnyName("vector", "deque", "list", "forward_list", "basic_string"),
----------------
vbvictor wrote:
I think this should become configurable via option for custom containers.
https://github.com/llvm/llvm-project/pull/193407
More information about the cfe-commits
mailing list