-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Why prefer dereference to clone on Copy types? #2870
Comments
To me, an explicit use of clone always suggests there's something nontrivial going on. If you call a method on a copy type which takes it by value, you also get a copy without first cloning. A dereference shows you that a cheap operation is happening |
In addition to reasons given above, the
|
In this very contrived code:
Clippy gives a warning:
I'm not sure this is a good recommendation. Using
.clone()
makes it obvious that the work is happening, whereas the mental jumps of "I'm dereferencing, which means that my type can be copied, and that copy will be moved" are less obvious. In particular, when I explained to someone just starting out with Rust that these were the two ways of fixing the compile error they ran into, their conclusion was that calling.clone()
is better self-documenting code than calling*
.I'm in general fine with the
clone_on_copy
lint, but I feel like it should specifically be disabled for cases where the suggestion is to dereference. Is there a compelling reason that the above code is worse than the suggested alternative? What do others think?The text was updated successfully, but these errors were encountered: