Clone_from should have been sugared

While I understand the reason for clone_from been forced on users by clippy, I definitely feel that its something that should have been sugared away.
I think retaining more intuitive way of assigning variables will go a long way for someone who comes from different languages.

Or the whole ecosystems will grow with one more clippy warning to shush.

// this is intuitive
*b = a;

// this is not
b.clone_from(a)

This works if either a is not used anymore, or it is Copy; in both cases, clone is redundant. If you need clone, it will always be explicit, either as *b = a.clone(), or as b.clone_from(a).

Note also that the lint you're likely referring to is in the pedantic group - therefore, it's designed to be opt-in, when the project's maintainer feels it's necessary, not to be imposed on anyone. If you like a.clone() more, very well, just don't enable this lint.

5 Likes
  1. What's "not intuitive" about a single method call?
  2. What you want can't work, because assignment already means moving, not cloning.
  3. Cloning shouldn't be implicit.
4 Likes
// this is intuitive
*b = a;

// this is not
b.clone_from(a)

I am not sure about that statement. Other languages are full of operator overloading. While short and convenient I like to see what is done at the actual line of source code.
Assigning an egg to a dog still does not seem intuitive at all to me.
Also in Rust things (still) confuse me: you can write the traits From, Or, And, OrAssign, AndAssign.
From is not done the 'intuitive' way I think. The last ones are mapped to | & statements.
I really should study on these little details...

1 Like

Remember also that a = b.clone(); has a different panic-safety behaviour from a.clone_from(b); (or b.clone_into(&mut a);).

If you ever care about catching panics, they're not the same.

4 Likes

Oh, different panic behavior indeed sounds like the reason not to I guess.

Thanks for your thoughts everyone!

As for pedantic... I need to check it, but I'm pretty sure there were no clippy overrides before this warning appeared in ci after rustc version bump

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.