[clang-tools-extra] [clang-tidy] Add llvm-use-vector-utils (PR #177722)
Jakub Kuderski via cfe-commits
cfe-commits at lists.llvm.org
Sat Jan 24 15:23:36 PST 2026
================
@@ -0,0 +1,97 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 "UseVectorUtilsCheck.h"
+#include "../utils/LexerUtils.h"
+#include "clang/AST/ASTContext.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::llvm_check {
+
+UseVectorUtilsCheck::UseVectorUtilsCheck(StringRef Name,
+ ClangTidyContext *Context)
+ : ClangTidyCheck(Name, Context),
+ Inserter(utils::IncludeSorter::IS_LLVM, areDiagsSelfContained()) {}
+
+void UseVectorUtilsCheck::registerPPCallbacks(const SourceManager &SM,
+ Preprocessor *PP,
+ Preprocessor *ModuleExpanderPP) {
+ Inserter.registerPreprocessor(PP);
+}
+
+void UseVectorUtilsCheck::registerMatchers(MatchFinder *Finder) {
+ // Match `llvm::to_vector(llvm::map_range(X, F))` or
+ // `llvm::to_vector(llvm::make_filter_range(X, Pred))`.
+ Finder->addMatcher(
+ callExpr(callee(functionDecl(hasName("::llvm::to_vector"))),
+ hasArgument(0, callExpr(callee(functionDecl(hasAnyName(
+ "::llvm::map_range",
+ "::llvm::make_filter_range"))),
+ argumentCountIs(2))
+ .bind("inner_call")),
+ argumentCountIs(1))
+ .bind("outer_call"),
+ this);
+}
+
+void UseVectorUtilsCheck::check(const MatchFinder::MatchResult &Result) {
+ const auto *OuterCall = Result.Nodes.getNodeAs<CallExpr>("outer_call");
+ assert(OuterCall);
+
+ const auto *InnerCall = Result.Nodes.getNodeAs<CallExpr>("inner_call");
+ assert(InnerCall);
----------------
kuhar wrote:
I consider this an anti-pattern in general because combining assertions makes it harder to pinpoint what exactly failed
https://github.com/llvm/llvm-project/pull/177722
More information about the cfe-commits
mailing list