| 1 |
.. title:: clang-tidy - performance-unnecessary-value-param |
| 2 |
|
| 3 |
performance-unnecessary-value-param |
| 4 |
=================================== |
| 5 |
|
| 6 |
Flags value parameter declarations of expensive to copy types that are copied |
| 7 |
for each invocation but it would suffice to pass them by const reference. |
| 8 |
|
| 9 |
The check is only applied to parameters of types that are expensive to copy |
| 10 |
which means they are not trivially copyable or have a non-trivial copy |
| 11 |
constructor or destructor. |
| 12 |
|
| 13 |
To ensure that it is safe to replace the value parameter with a const reference |
| 14 |
the following heuristic is employed: |
| 15 |
|
| 16 |
1. the parameter is const qualified; |
| 17 |
2. the parameter is not const, but only const methods or operators are invoked |
| 18 |
on it, or it is used as const reference or value argument in constructors or |
| 19 |
function calls. |
| 20 |
|
| 21 |
Example: |
| 22 |
|
| 23 |
.. code-block:: c++ |
| 24 |
|
| 25 |
void f(const string Value) { |
| 26 |
// The warning will suggest making Value a reference. |
| 27 |
} |
| 28 |
|
| 29 |
void g(ExpensiveToCopy Value) { |
| 30 |
// The warning will suggest making Value a const reference. |
| 31 |
Value.ConstMethd(); |
| 32 |
ExpensiveToCopy Copy(Value); |
| 33 |
} |
| 34 |
|
| 35 |
If the parameter is not const, only copied or assigned once and has a |
| 36 |
non-trivial move-constructor or move-assignment operator respectively the check |
| 37 |
will suggest to move it. |
| 38 |
|
| 39 |
Example: |
| 40 |
|
| 41 |
.. code-block:: c++ |
| 42 |
|
| 43 |
void setValue(string Value) { |
| 44 |
Field = Value; |
| 45 |
} |
| 46 |
|
| 47 |
Will become: |
| 48 |
|
| 49 |
.. code-block:: c++ |
| 50 |
|
| 51 |
#include <utility> |
| 52 |
|
| 53 |
void setValue(string Value) { |
| 54 |
Field = std::move(Value); |
| 55 |
} |
| 56 |
|
| 57 |
Options |
| 58 |
------- |
| 59 |
|
| 60 |
.. option:: IncludeStyle |
| 61 |
|
| 62 |
A string specifying which include-style is used, `llvm` or `google`. Default |
| 63 |
is `llvm`. |
| 64 |
|
| 65 |
.. option:: AllowedTypes |
| 66 |
|
| 67 |
A semicolon-separated list of names of types allowed to be passed by value. |
| 68 |
Regular expressions are accepted, e.g. `[Rr]ef(erence)?$` matches every type |
| 69 |
with suffix `Ref`, `ref`, `Reference` and `reference`. The default is |
| 70 |
empty. If a name in the list contains the sequence `::` it is matched against |
| 71 |
the qualified typename (i.e. `namespace::Type`, otherwise it is matched |
| 72 |
against only the type name (i.e. `Type`). |