Skip to content

Should map_clone trigger on copies of double references? #3958

Closed
@Manishearth

Description

@Manishearth
fn main() {
    let x = Some(&"abcd");
    let y = x.map(|&p| p);
}

playpen

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingS-needs-discussionStatus: Needs further discussion before merging or work can be started

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions