[PATCH] D137205: [clang-tidy] Add performance-unnecessary-copy-on-last-use check

Nathan James via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Nov 20 05:10:59 PST 2022


njames93 added a subscriber: sammccall.
njames93 added a comment.

@sammccall I have a feeling you're gonna want to examine this checks feasibility in clangd.



================
Comment at: clang-tools-extra/clang-tidy/performance/UnnecessaryCopyOnLastUseCheck.cpp:79
+
+static auto findDeclRefBlock(CFG const *TheCFG, DeclRefExpr const *DeclRef)
+    -> FindDeclRefBlockReturn {
----------------
We generally avoid trailing return types.


================
Comment at: clang-tools-extra/clang-tidy/performance/UnnecessaryCopyOnLastUseCheck.cpp:96
+
+static auto
+nextUsageInCurrentBlock(FindDeclRefBlockReturn const &StartBlockElement,
----------------
The same rules for auto apply here, this should be explicit about what pointer it's returning.


================
Comment at: clang-tools-extra/clang-tidy/performance/UnnecessaryCopyOnLastUseCheck.cpp:123
+          hasLHS(ignoringParenImpCasts(declRefExpr(equalsNode(DeclRef)))))),
+      Context);
+  return !Matches.empty();
----------------
Matching over the entire context seems pretty and a huge drain on performance, would it not make sense to just match inside the function declaration.
Side note maybe a RecursiveASTVisitor would make more sense here in terms of performance.

Same goes for isInLambdaCapture.


================
Comment at: clang-tools-extra/clang-tidy/performance/UnnecessaryCopyOnLastUseCheck.cpp:290
+
+    if (isInLambdaCapture(Param, *Result.Context)) {
+      // Lambda captures should not be fixed.
----------------
What's the reason for this logic, they require a different fix -
`x` => `x(std::move(x))` 
And a checking langopts for c++14.

Or is this because of implicit captures?


================
Comment at: clang-tools-extra/clang-tidy/performance/UnnecessaryCopyOnLastUseCheck.h:30
+  UnnecessaryCopyOnLastUseCheck(StringRef Name, ClangTidyContext *Context);
+  UnnecessaryCopyOnLastUseCheck(UnnecessaryCopyOnLastUseCheck &&) = delete;
+  UnnecessaryCopyOnLastUseCheck(const UnnecessaryCopyOnLastUseCheck &) = delete;
----------------
We typically avoid defining all the special member functions for clang tidy checks. The destructor typically only needs to be defined if it's definition can't be inside the header file.


================
Comment at: clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-copy-on-last-use.cpp:1
+// RUN: %check_clang_tidy %s -std=c++17 performance-unnecessary-copy-on-last-use %t
+// RUN: %check_clang_tidy %s -std=c++11 performance-unnecessary-copy-on-last-use %t
----------------
Running this check explicitly in c++11/17 implies you expect different diagnostics, if so you can use the `--check-suffixes` flag to enable checking configurations. Search for other tests which use that if you're unsure 


================
Comment at: clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-copy-on-last-use.cpp:16
+
+struct Copyable {
+  Copyable() = default;
----------------
This struct appears to be unused and has exactly the same definition as `Movable`.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D137205/new/

https://reviews.llvm.org/D137205



More information about the cfe-commits mailing list