[clang-tools-extra] [clang-tidy] Add check performance-lost-std-move (PR #139525)
Baranov Victor via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 11 05:49:17 PST 2025
================
@@ -0,0 +1,196 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 "LostStdMoveCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Lex/Lexer.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::performance {
+
+template <typename Node>
+static void extractNodesByIdTo(ArrayRef<BoundNodes> Matches, StringRef ID,
+ llvm::SmallPtrSet<const Node *, 16> &Nodes) {
+ for (const BoundNodes &Match : Matches)
+ Nodes.insert(Match.getNodeAs<Node>(ID));
+}
+
+static llvm::SmallPtrSet<const DeclRefExpr *, 16>
+allDeclRefExprsHonourLambda(const VarDecl &VarDecl, const Decl &Decl,
+ ASTContext &Context) {
+ auto Matches = match(
+ decl(forEachDescendant(
+ declRefExpr(to(varDecl(equalsNode(&VarDecl))),
+ unless(hasAncestor(lambdaExpr(hasAnyCapture(lambdaCapture(
+ capturesVar(varDecl(equalsNode(&VarDecl)))))))))
+ .bind("declRef"))),
+ Decl, Context);
+ llvm::SmallPtrSet<const DeclRefExpr *, 16> DeclRefs;
+ extractNodesByIdTo(Matches, "declRef", DeclRefs);
+ return DeclRefs;
+}
+
+static llvm::SmallPtrSet<const VarDecl *, 16>
+allVarDeclsExprs(const VarDecl &VarDecl, const Decl &Decl,
+ ASTContext &Context) {
+ auto Matches = match(
+ decl(forEachDescendant(declRefExpr(
+ to(varDecl(equalsNode(&VarDecl))),
+ hasParent(decl(
+ varDecl(hasType(qualType(referenceType()))).bind("varDecl"))),
+ unless(hasAncestor(lambdaExpr(hasAnyCapture(
+ lambdaCapture(capturesVar(varDecl(equalsNode(&VarDecl))))))))))),
+ Decl, Context);
+ llvm::SmallPtrSet<const class VarDecl *, 16> VarDecls;
+ extractNodesByIdTo(Matches, "varDecl", VarDecls);
+ return VarDecls;
+}
+
+static const Expr *
+getLastVarUsage(const llvm::SmallPtrSet<const DeclRefExpr *, 16> &Exprs) {
+ const Expr *LastExpr = nullptr;
+ for (const clang::DeclRefExpr *Expr : Exprs) {
+ if (!LastExpr)
+ LastExpr = Expr;
+
+ if (LastExpr->getBeginLoc() < Expr->getBeginLoc())
+ LastExpr = Expr;
+ }
+
+ return LastExpr;
+}
+
+namespace {
+
+AST_MATCHER(CXXRecordDecl, hasTrivialMoveConstructor) {
+ return Node.hasDefinition() && Node.hasTrivialMoveConstructor();
+}
+
+AST_MATCHER_P(Expr, ignoreParens, ast_matchers::internal::Matcher<Expr>,
+ InnerMatcher) {
+ return InnerMatcher.matches(*Node.IgnoreParens(), Finder, Builder);
+}
+
+} // namespace
+
+void LostStdMoveCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
+ Options.store(Opts, "StrictMode", StrictMode);
+}
+
+LostStdMoveCheck::LostStdMoveCheck(StringRef Name, ClangTidyContext *Context)
+ : ClangTidyCheck(Name, Context),
+ StrictMode(Options.get("StrictMode", false)) {}
+
+void LostStdMoveCheck::registerMatchers(MatchFinder *Finder) {
+ auto ReturnParent =
+ hasParent(expr(hasParent(cxxConstructExpr(hasParent(returnStmt())))));
----------------
vbvictor wrote:
Could we just use `hasAncestor`?
https://github.com/llvm/llvm-project/pull/139525
More information about the cfe-commits
mailing list