| 1 |
.. title:: clang-tidy - cppcoreguidelines-rvalue-reference-param-not-moved |
| 2 |
|
| 3 |
cppcoreguidelines-rvalue-reference-param-not-moved |
| 4 |
================================================== |
| 5 |
|
| 6 |
Warns when an rvalue reference function parameter is never moved within |
| 7 |
the function body. |
| 8 |
|
| 9 |
Rvalue reference parameters indicate a parameter that should be moved with |
| 10 |
``std::move`` from within the function body. Any such parameter that is |
| 11 |
never moved is confusing and potentially indicative of a buggy program. |
| 12 |
|
| 13 |
Example: |
| 14 |
|
| 15 |
.. code-block:: c++ |
| 16 |
|
| 17 |
void logic(std::string&& Input) { |
| 18 |
std::string Copy(Input); // Oops - forgot to std::move |
| 19 |
} |
| 20 |
|
| 21 |
Note that parameters that are unused and marked as such will not be diagnosed. |
| 22 |
|
| 23 |
Example: |
| 24 |
|
| 25 |
.. code-block:: c++ |
| 26 |
|
| 27 |
void conditional_use([[maybe_unused]] std::string&& Input) { |
| 28 |
// No diagnostic here since Input is unused and marked as such |
| 29 |
} |
| 30 |
|
| 31 |
Options |
| 32 |
------- |
| 33 |
|
| 34 |
.. option:: AllowPartialMove |
| 35 |
|
| 36 |
If set to `true`, the check accepts ``std::move`` calls containing any |
| 37 |
subexpression containing the parameter. CppCoreGuideline F.18 officially |
| 38 |
mandates that the parameter itself must be moved. Default is `false`. |
| 39 |
|
| 40 |
.. code-block:: c++ |
| 41 |
|
| 42 |
// 'p' is flagged by this check if and only if AllowPartialMove is false |
| 43 |
void move_members_of(pair<Obj, Obj>&& p) { |
| 44 |
pair<Obj, Obj> other; |
| 45 |
other.first = std::move(p.first); |
| 46 |
other.second = std::move(p.second); |
| 47 |
} |
| 48 |
|
| 49 |
// 'p' is never flagged by this check |
| 50 |
void move_whole_pair(pair<Obj, Obj>&& p) { |
| 51 |
pair<Obj, Obj> other = std::move(p); |
| 52 |
} |
| 53 |
|
| 54 |
.. option:: IgnoreUnnamedParams |
| 55 |
|
| 56 |
If set to `true`, the check ignores unnamed rvalue reference parameters. |
| 57 |
Default is `false`. |
| 58 |
|
| 59 |
.. option:: IgnoreNonDeducedTemplateTypes |
| 60 |
|
| 61 |
If set to `true`, the check ignores non-deduced template type rvalue |
| 62 |
reference parameters. Default is `false`. |
| 63 |
|
| 64 |
.. code-block:: c++ |
| 65 |
|
| 66 |
template <class T> |
| 67 |
struct SomeClass { |
| 68 |
// Below, 'T' is not deduced and 'T&&' is an rvalue reference type. |
| 69 |
// This will be flagged if and only if IgnoreNonDeducedTemplateTypes is |
| 70 |
// false. One suggested fix would be to specialize the class for 'T' and |
| 71 |
// 'T&' separately (e.g., see std::future), or allow only one of 'T' or |
| 72 |
// 'T&' instantiations of SomeClass (e.g., see std::optional). |
| 73 |
SomeClass(T&& t) { } |
| 74 |
}; |
| 75 |
|
| 76 |
// Never flagged, since 'T' is a forwarding reference in a deduced context |
| 77 |
template <class T> |
| 78 |
void forwarding_ref(T&& t) { |
| 79 |
T other = std::forward<T>(t); |
| 80 |
} |
| 81 |
|
| 82 |
This check implements `F.18 |
| 83 |
<http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#f18-for-will-move-from-parameters-pass-by-x-and-stdmove-the-parameter>`_ |
| 84 |
from the C++ Core Guidelines. |
| 85 |
|