rust-lang / rust-clippy

A bunch of lints to catch common mistakes and improve your Rust code. Book: https://doc.rust-lang.org/clippy/
https://rust-lang.github.io/rust-clippy/
Other
11.28k stars 1.52k forks source link

New lint: clone on arguments taken by reference #2074

Open mathstuf opened 7 years ago

mathstuf commented 7 years ago

If a function takes an argument by reference, but later calls .clone() on it (or otherwise requires an impl Clone or impl Copy from it), lint that taking it by ownership allows the caller to pass in its memory rather than forcing a clone on the data.

I don't know if this is possible, but can it default to allow on public functions and deny on crate-private functions (due to the API change)?

oli-obk commented 7 years ago

Should be a simple extension of our lints on to_owned

This should only lint if the clone is triggered unconditionally

llogiq commented 7 years ago

Also note that ptr_arg already lints some instances.