[clang-tools-extra] [clang-tidy] Add check performance-lost-std-move (PR #139525)
Fabian Keßler via cfe-commits
cfe-commits at lists.llvm.org
Sun Jun 1 14:51:11 PDT 2025
================
@@ -0,0 +1,32 @@
+.. title:: clang-tidy - performance-lost-std-move
+
+performance-lost-std-move
+=========================
+
+Warns if copy constructor is used instead of std::move() and suggests a fix.
+It honours cycles, lambdas, and unspecified call order in compound expressions.
+
+.. code-block:: c++
+
+ void f(X);
+
+ void g(X x) {
+ f(x); // warning: Could be std::move() [performance-lost-std-move]
+ }
+
+It finds the last local variable usage, and if it is a copy, emits a warning.
+The check is based on pure AST matching and doesn't take into account any data flow information.
+Thus, it doesn't catch assign-after-copy cases.
+Also it doesn't notice variable references "behind the scenes":
+
+.. code-block:: c++
+
+ void f(X);
+
+ void g(X x) {
+ auto &y = x;
+ f(x); // emits a warning...
+ y.f(); // ...but it is still used
+ }
----------------
Febbe wrote:
> The problem is that any binding to a reference should skip the check. E.g.
>
> ```
> void f(X);
> void g(X&);
>
> void func(X x) {
> g(x); // reference is created, we may not move from x anymore
> f(x); // no fix
> }
> ```
>
> I'm afraid too many cases are skipped due to this problem.
Would it be a compromise to enable this via an option that is disabled by default?
Also, disable automatic fixes for those cases, so every change has to be done on purpose?
https://github.com/llvm/llvm-project/pull/139525
More information about the cfe-commits
mailing list