Skip to content

False-positive map_clone #6299

@iddm

Description

@iddm

I tried this code:

fn f2() {
    let mut a = 5u64;
    let mut b = 3u64;
    let items = vec![&mut a, &mut b];
    let items2 = items.into_iter().map(|i| i.clone()).collect::<Vec<u64>>();
}

fn main() {
}

playground

I expected to see this happen: a doable suggestion.

Instead, this happened: the suggestion isn't possible to satisfy: the cloned() method on an iterator of mutable references can't produce a collection of cloned/owned objects:

fn f2() {
    let mut a = 5u64;
    let mut b = 3u64;
    let items = vec![&mut a, &mut b];
    let items2 = items.into_iter().cloned().collect::<Vec<u64>>();
}

fn main() {
}

playground

Because:

error[E0271]: type mismatch resolving `<std::vec::IntoIter<&mut u64> as std::iter::Iterator>::Item == &_`
 --> src/main.rs:5:36
  |
5 |     let items2 = items.into_iter().cloned().collect::<Vec<u64>>();
  |                                    ^^^^^^ types differ in mutability
  |
  = note:   expected type `&mut u64`
          found reference `&_`

Meta

  • cargo clippy -V: clippy 0.0.212 (18bf6b4 2020-10-07)
  • rustc -Vv:
rustc 1.47.0 (18bf6b4f0 2020-10-07)
binary: rustc
commit-hash: 18bf6b4f01a6feaf7259ba7cdae58031af1b7b39
commit-date: 2020-10-07
host: x86_64-unknown-linux-gnu
release: 1.47.0
LLVM version: 11.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions