Skip to content

False-positive map_clone #6299

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
iddm opened this issue Nov 5, 2020 · 1 comment · Fixed by #6301
Closed

False-positive map_clone #6299

iddm opened this issue Nov 5, 2020 · 1 comment · Fixed by #6301
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@iddm
Copy link
Contributor

iddm commented Nov 5, 2020

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
@iddm iddm added the C-bug Category: Clippy is not doing the correct thing label Nov 5, 2020
@giraffate
Copy link
Contributor

@rustbot modify labels: +L-suggestion-causes-error

@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Nov 5, 2020
@bors bors closed this as completed in 96d5f45 Nov 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants