-
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
implicit_clone
shouldn't lint when called on a reference
#11281
Comments
I agree, using basically when I got a reference and I want to return an owned value, the first thing I'd think about was On the other hand, if I convert a borrow data "to owned", does that mean I now took the ownership of the one I borrowed? but in reality, it has became a cloned data of the borrowed one. So yes, both way would work, it's highly up to people's preference of which one should be used, just I'm a little bit leans toward |
Also, from what I see in that first discussion link: Current guidance on &str to String (to_owned vs to_string), maybe this lint could also be branching out to one that lints against @rustbot claim |
I don't know that there's agreement that |
While |
@Centri3 At least for what I was thinking in this issue was just that this link shouldn't complain if you use I fully agree that reasonable people might be fine to use |
I see. I misunderstood then, Makes sense |
Part of #155 Commit messages have a bit more context on the relevant changes. I've decided to drop very few lints from the EDR list like deny-by-default ones, `inconsistent_struct_constructor` (not that useful) or `implicit_clone` (a lot of false-positives/unidiomatic suggestions; let's wait for Rust 1.74 and rust-lang/rust-clippy#11281 to include it again) or irrelevant ones for us like `suboptimal_flops`.
Summary
I agree that for something like https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=bba81815705c82d64c18911576b5244a
This is a good lint, because
x
is already owned, soclone
better describes what's happening.However, today the lint also complains if I'm starting with a
&String
, which definitely isn't owned, and I'd very much like to use.to_owned()
on such things to clarify that what I want is something owned (as I describe in further detail in https://users.rust-lang.org/t/current-guidance-on-str-to-string-to-owned-vs-to-string/75754/7?u=scottmcm or https://users.rust-lang.org/t/what-is-the-difference-between-clone-and-to-owned/54705/3?u=scottmcm).As such, I think this lint is over-aggressive and should be slightly tuned back. I would say that if the call to
to_owned
ends up needing auto-ref, like it does in thedup
example above, it should lint, but if it doesn't need autoref (because the LHS is already a reference) then it should stop linting.(Opened this issue because it came up on discord.)
Lint Name
implicit_clone
Reproducer
I tried this code:
I saw this happen:
I expected to see this happen:
It compiles fine.
Version
Additional Labels
No response
The text was updated successfully, but these errors were encountered: