-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[clang-tidy] Add check performance-lost-std-move #139525
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 12 commits
3abbce9
4a1a77b
f74066d
edd4800
5359c69
5bb6af6
592d79b
1710c2f
b4824ef
60e4aca
a27b1b3
6aacd41
71cd708
402ba55
fdf01b6
24357a2
b48425b
986d6aa
5fb15e1
4a1d653
0d612ec
6f55a1c
8963056
55c4d16
ee844fd
2fd53f6
54571f9
d28087b
b212770
aa46750
4ae1247
62e4951
e12c39c
97db45c
7b091cc
9816cdb
6b31812
e8b2d48
777eacd
67790b8
708317b
ed6d376
4f62ad9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,180 @@ | ||||
//===--- LostStdMoveCheck.cpp - clang-tidy --------------------------------===// | ||||
// | ||||
// 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> | ||||
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 const Expr* getLastVarUsage(const VarDecl& Var, const Decl& Func, | ||||
ASTContext& Context) { | ||||
auto Exprs = allDeclRefExprsHonourLambda(Var, Func, Context); | ||||
|
||||
const Expr* LastExpr = nullptr; | ||||
for (const clang::DeclRefExpr* Expr : Exprs) { | ||||
if (!LastExpr) LastExpr = Expr; | ||||
|
||||
if (LastExpr->getBeginLoc() < Expr->getBeginLoc()) LastExpr = Expr; | ||||
} | ||||
|
||||
return LastExpr; | ||||
} | ||||
|
||||
AST_MATCHER(CXXRecordDecl, hasTrivialMoveConstructor) { | ||||
return Node.hasDefinition() && Node.hasTrivialMoveConstructor(); | ||||
} | ||||
|
||||
void LostStdMoveCheck::registerMatchers(MatchFinder* Finder) { | ||||
auto ReturnParent = | ||||
hasParent(expr(hasParent(cxxConstructExpr(hasParent(returnStmt()))))); | ||||
|
||||
auto OutermostExpr = expr(unless(hasParent(expr()))); | ||||
auto LeafStatement = stmt(OutermostExpr); | ||||
|
||||
Finder->addMatcher( | ||||
declRefExpr( | ||||
// not "return x;" | ||||
unless(ReturnParent), | ||||
|
||||
|
||||
unless(hasType(namedDecl(hasName("::std::string_view")))), | ||||
|
||||
// non-trivial type | ||||
hasType(hasCanonicalType(hasDeclaration(cxxRecordDecl()))), | ||||
|
||||
// non-trivial X(X&&) | ||||
unless(hasType(hasCanonicalType( | ||||
hasDeclaration(cxxRecordDecl(hasTrivialMoveConstructor()))))), | ||||
|
||||
// Not in a cycle | ||||
unless(hasAncestor(forStmt())), | ||||
|
||||
unless(hasAncestor(doStmt())), | ||||
|
||||
unless(hasAncestor(whileStmt())), | ||||
|
||||
// Not in a body of lambda | ||||
unless(hasAncestor(compoundStmt(hasAncestor(lambdaExpr())))), | ||||
|
||||
// only non-X& | ||||
unless(hasDeclaration( | ||||
varDecl(hasType(qualType(lValueReferenceType()))))), | ||||
|
||||
hasAncestor(LeafStatement.bind("leaf_statement")), | ||||
|
||||
hasDeclaration( | ||||
varDecl(hasAncestor(functionDecl().bind("func"))).bind("decl")), | ||||
|
||||
anyOf( | ||||
|
||||
// f(x) | ||||
hasParent(expr(hasParent(cxxConstructExpr())).bind("use_parent")), | ||||
|
||||
// f((x)) | ||||
hasParent(parenExpr(hasParent( | ||||
expr(hasParent(cxxConstructExpr())).bind("use_parent")))) | ||||
|
||||
) | ||||
|
||||
) | ||||
.bind("use"), | ||||
this); | ||||
} | ||||
|
||||
void LostStdMoveCheck::check(const MatchFinder::MatchResult& Result) { | ||||
const auto* MatchedDecl = Result.Nodes.getNodeAs<VarDecl>("decl"); | ||||
const auto* MatchedFunc = Result.Nodes.getNodeAs<FunctionDecl>("func"); | ||||
const auto* MatchedUse = Result.Nodes.getNodeAs<Expr>("use"); | ||||
const auto* MatchedUseCall = Result.Nodes.getNodeAs<CallExpr>("use_parent"); | ||||
const auto* MatchedLeafStatement = | ||||
Result.Nodes.getNodeAs<Stmt>("leaf_statement"); | ||||
|
||||
if (!MatchedDecl->hasLocalStorage()) return; | ||||
|
||||
|
||||
if (MatchedUseCall) { | ||||
return; | ||||
} | ||||
|
||||
const Expr* LastUsage = | ||||
getLastVarUsage(*MatchedDecl, *MatchedFunc, *Result.Context); | ||||
|
||||
if (LastUsage && LastUsage->getBeginLoc() > MatchedUse->getBeginLoc()) { | ||||
// "use" is not the last reference to x | ||||
return; | ||||
} | ||||
|
||||
if (LastUsage && | ||||
LastUsage->getSourceRange() != MatchedUse->getSourceRange()) { | ||||
return; | ||||
} | ||||
|
||||
// Calculate X usage count in the statement | ||||
llvm::SmallPtrSet<const DeclRefExpr*, 16> DeclRefs; | ||||
ArrayRef<BoundNodes> Matches = match( | ||||
findAll(declRefExpr( | ||||
|
||||
|
Same below.
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,33 @@ | ||||||
//===--- LostStdMoveCheck.h - clang-tidy ------------------------*- C++ -*-===// | ||||||
|
//===--- LostStdMoveCheck.h - clang-tidy ------------------------*- C++ -*-===// | |
//===----------------------------------------------------------------------===// |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be synced with release notes and check docs
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// http://clang.llvm.org/extra/clang-tidy/checks/performance/lost-std-move.html | |
/// https://clang.llvm.org/extra/clang-tidy/checks/performance/lost-std-move.html |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -145,8 +145,75 @@ New checks | |
- New :doc:`readability-ambiguous-smartptr-reset-call | ||
<clang-tidy/checks/readability/ambiguous-smartptr-reset-call>` check. | ||
|
||
<<<<<<< HEAD | ||
|
||
Detects implicit conversions between pointers of different levels of | ||
indirection. | ||
|
||
- New :doc:`bugprone-optional-value-conversion | ||
<clang-tidy/checks/bugprone/optional-value-conversion>` check. | ||
|
||
Detects potentially unintentional and redundant conversions where a value is | ||
extracted from an optional-like type and then used to create a new instance | ||
of the same optional-like type. | ||
|
||
- New :doc:`cppcoreguidelines-no-suspend-with-lock | ||
<clang-tidy/checks/cppcoreguidelines/no-suspend-with-lock>` check. | ||
|
||
Flags coroutines that suspend while a lock guard is in scope at the | ||
suspension point. | ||
|
||
- New :doc:`hicpp-ignored-remove-result | ||
<clang-tidy/checks/hicpp/ignored-remove-result>` check. | ||
|
||
Ensure that the result of ``std::remove``, ``std::remove_if`` and | ||
``std::unique`` are not ignored according to rule 17.5.1. | ||
|
||
- New :doc:`misc-coroutine-hostile-raii | ||
<clang-tidy/checks/misc/coroutine-hostile-raii>` check. | ||
|
||
Detects when objects of certain hostile RAII types persists across suspension | ||
points in a coroutine. Such hostile types include scoped-lockable types and | ||
types belonging to a configurable denylist. | ||
|
||
- New :doc:`modernize-use-constraints | ||
<clang-tidy/checks/modernize/use-constraints>` check. | ||
|
||
Replace ``enable_if`` with C++20 requires clauses. | ||
|
||
- New :doc:`modernize-use-starts-ends-with | ||
<clang-tidy/checks/modernize/use-starts-ends-with>` check. | ||
|
||
|
||
Checks whether a ``find`` or ``rfind`` result is compared with 0 and suggests | ||
replacing with ``starts_with`` when the method exists in the class. Notably, | ||
this will work with ``std::string`` and ``std::string_view``. | ||
|
||
- New :doc:`modernize-use-std-numbers | ||
<clang-tidy/checks/modernize/use-std-numbers>` check. | ||
|
||
Finds constants and function calls to math functions that can be replaced | ||
with C++20's mathematical constants from the ``numbers`` header and | ||
offers fix-it hints. | ||
|
||
- New :doc:`performance-enum-size | ||
<clang-tidy/checks/performance/enum-size>` check. | ||
|
||
Recommends the smallest possible underlying type for an ``enum`` or ``enum`` | ||
class based on the range of its enumerators. | ||
|
||
- New :doc:`performance-lost-std-move | ||
<clang-tidy/checks/performance/lost-std-move>` check. | ||
|
||
Searches for lost std::move(). | ||
|
||
- New :doc:`readability-reference-to-constructed-temporary | ||
<clang-tidy/checks/readability/reference-to-constructed-temporary>` check. | ||
|
||
Detects C++ code where a reference variable is used to extend the lifetime | ||
of a temporary object that has just been constructed. | ||
======= | ||
Finds potentially erroneous calls to ``reset`` method on smart pointers when | ||
the pointee type also has a ``reset`` method. | ||
>>>>>>> origin/main | ||
|
||
New check aliases | ||
^^^^^^^^^^^^^^^^^ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
.. title:: clang-tidy - performance-lost-std-move | ||
|
||
performance-lost-std-move | ||
========================= | ||
|
||
The check warns if copy constructor is used instead of std::move(). | ||
|
||
|
||
.. code-block:: c++ | ||
|
||
void f(X); | ||
|
||
void g(X x) { | ||
f(x); // warning: Could be std::move() [performance-lost-std-move] | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.