<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/94798>94798</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
FR: clang-tidy (or off-by-default warning) for unconditional copy of const ref param
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang-tidy
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
pkasting
</td>
</tr>
</table>
<pre>
Code like the following is a common source of extra copies in Chromium:
```
struct S {
S(const T& t) : t(t) {}
T t;
};
```
The following is generally better, as callers with lvalues will perform the same, but callers with rvalues will benefit if `T` has a move constructor:
```
struct S {
S(T t) : t(std::move(t)) {}
T t;
};
```
The generalization of this is: I'd like to find functions that that a `const T&` param and unconditionally copy-construct a `T` from it.
One downside I can think of is if `T`'s move constructor is more expensive than a pointer copy, and all callers of this function themselves already had refs. At that point, fixing everyone up the chain to do moves at each call has costs compared to continually passing along refs.
This is sort of akin to -Wpessimizing-move (and so might be appropriate for an off-by-default warning), but it feels less clear-cut than that. I suspect a clang-tidy pass might be better.
I don't know what an escape hatch would look like for this in code. Perhaps seeing some real use cases where the above pattern is intentional would help.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyslEmP6zYMxz-NciESOMriycGHWRBgTi36BuiZlmhbjSwZopxM3qcvaGeWtOihwAMCxRLE7cc_hcyuDUSV2j2p3csCx9zFVA0n5OxCu6ijvVbP0RJ4dyLIHUETvY8XF1pwDAgm9n0MwHFMhiA2QO85yfHgiMEFeO5S7N3Yq82jKl5U8aj2xe03bTmn0WT4Aap8mk8Afij9YGLgDG9K7yErfQC1eZSPh3lTPqny5eP6G2S1uRnL8ef3faR5fftnDS0FSuj9FWrKmZLSz4AMBr2nxHBxuQN_Rj-SbLyHgVITUz_RYOxJDOox31uk7xY1BWpcBteA2hdval9Ah0Kvj2eCqVKBENP_hfR2z4azFQ-bR_F7Y_ULcN0IuZ-YXQzS5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