84 lines · plain
1.. title:: clang-tidy - performance-unnecessary-value-param2 3performance-unnecessary-value-param4===================================5 6Flags value parameter declarations of expensive to copy types that are copied7for each invocation but it would suffice to pass them by const reference.8 9The check is only applied to parameters of types that are expensive to copy10which means they are not trivially copyable or have a non-trivial copy11constructor or destructor.12 13To ensure that it is safe to replace the value parameter with a const reference14the following heuristic is employed:15 161. the parameter is const qualified;172. the parameter is not const, but only const methods or operators are invoked18 on it, or it is used as const reference or value argument in constructors or19 function calls.20 21Example: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 35If the parameter is not const, only copied or assigned once and has a36non-trivial move-constructor or move-assignment operator respectively the check37will suggest to move it.38 39Example:40 41.. code-block:: c++42 43 void setValue(string Value) {44 Field = Value;45 }46 47Will become:48 49.. code-block:: c++50 51 #include <utility>52 53 void setValue(string Value) {54 Field = std::move(Value);55 }56 57Because the fix-it needs to change the signature of the function, it may break58builds if the function is used in multiple translation units or some codes59depends on function signatures.60 61Options62-------63 64.. option:: IncludeStyle65 66 A string specifying which include-style is used, `llvm` or `google`. Default67 is `llvm`.68 69.. option:: AllowedTypes70 71 A semicolon-separated list of names of types allowed to be passed by value.72 Regular expressions are accepted, e.g. ``[Rr]ef(erence)?$`` matches every73 type with suffix ``Ref``, ``ref``, ``Reference`` and ``reference``. The74 default is empty. If a name in the list contains the sequence `::`, it is75 matched against the qualified type name (i.e. ``namespace::Type``),76 otherwise it is matched against only the type name (i.e. ``Type``).77 78.. option:: IgnoreCoroutines79 80 A boolean specifying whether the check should suggest passing parameters by81 reference in coroutines. Passing parameters by reference in coroutines may82 not be safe, please see :doc:`cppcoreguidelines-avoid-reference-coroutine-parameters <../cppcoreguidelines/avoid-reference-coroutine-parameters>`83 for more information. Default is `true`.84