Closed
Description
fn main() {
let x = Some(&"abcd");
let y = x.map(|&p| p);
}
warning: You are using an explicit closure for cloning elements
--> src/main.rs:3:13
|
3 | let y = x.map(|&p| p);
| ^^^^^^^^^^^^^ help: Consider calling the dedicated `cloned` method: `x.cloned()`
|
= note: #[warn(clippy::map_clone)] on by default
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#map_clone
So map_clone
triggers even for copies, which makes sense, since cloned()
works on copies. Here it specificially is triggering on copies of a double reference.
However, we also have clone_double_ref
which warns about cloning &&T
since that may not do what one expected.
Also .map(|&x| x)
is much clearer than .cloned()
in such cases, it's not at all obvious that the .cloned()
is there to unwrap a reference
I feel that this is a false positive, but I can see others disagreeing. Thoughts?
h/t @hawkw