New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
assigning_clones: makes code less readable and harder to maintain #12778
Comments
There are some false positives but they're from manual implementations in wrapper types (#12709). Directly at least types without a manual implementation of |
Ah, that's cool. I didn't know that. But I still think clippy is suggesting to do a premature optimization. In code where this level of performance difference is truly important, the difference should be bench-marked. I don't think this trade-off between absolute maximum possible performance and code readability is a proper one for the default configuration of clippy. I got this warning from a |
Note that there's a catch-22 here: using |
That's true. But even if this gives a runtime performance improvement, I don't think this lint should be enabled by default. It favors a small performance gain over a big readability loss. This kind of optimization may save a call to alloc and free in practise. If this is important enough, the compiler should try to figure this out and re-use the allocation behind the scenes (I think many C++ compilers do this nowadays?). I really feel that telling people not to use the /edit: Reading the zulip conversation the consensus also seems to be to put it in |
Description
Following the
assigning_clones
lint makes code less readable.There is a strong semantic hint in the
=
operator. It gives a visual indication that a value is being assigned to.The
clone_from()
function does not have this visual hint. Usingclone_from()
makes code harder to read and as a consequence, harder to maintain.Consider the two snippets:
The second snippet is much less clear. In isolation this may not seem like much, but in a real codebase this is bad.
Lints should not favor performance at the cost of maintainability. Especially considering that
#[derive(Clone)]
doesn't even implementclone_from()
and the defaultclone_from()
is implemented as*self = source.clone()
: https://github.com/rust-lang/rust/blob/5ce96b1d0f6b5093955e7b6a70dfd877395c1d73/library/core/src/clone.rs#L169-L171I suggest the lint is changed to be turned off by default.
Version
Additional Labels
No response
The text was updated successfully, but these errors were encountered: