Skip to content
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

Incorrect suggestion to use copied() method on an iterator of mutable references #5524

Closed
DomWilliams0 opened this issue Apr 25, 2020 · 0 comments · Fixed by #5530
Closed

Comments

@DomWilliams0
Copy link

When deferencing an iterator of references to a Copy with .map(|a_ref| *a_ref) clippy suggests using .copied(), which works for immutable references but doesn't for mutables, as shown in the example below.

$ cat src/main.rs
fn main() {
    let mut three = 3;
    let three_ref = &mut three;
    let v: Vec<&mut i32> = vec![three_ref];

    let _iter: Vec<i32> = v.into_iter().map(|i| *i).collect();
}

$ cargo clippy
    Checking clippy v0.1.0 (/tmp/clippy)
warning: You are using an explicit closure for copying elements
 --> src/main.rs:6:26
  |
6 |     let _iter: Vec<i32> = v.into_iter().map(|i| *i).collect();
  |                          ^^^^^^^^^^^^^^^^^^^^^^^^^ help: Consider calling the dedicated `copied` method: `v.into_iter().copied()`
  |
  = 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
$ cat src/main.rs
...
    //let _iter: Vec<i32> = v.into_iter().map(|i| *i).collect();
    let _iter: Vec<i32> = v.into_iter().copied().collect();

$ cargo clippy   
    Checking clippy v0.1.0 (/tmp/clippy)
error[E0271]: type mismatch resolving `<std::vec::IntoIter<&mut i32> as std::iter::Iterator>::Item == &_`
 --> src/main.rs:7:41
  |
7 |     let _iter: Vec<i32> = v.into_iter().copied().collect();
  |                                         ^^^^^^ types differ in mutability
  |
  = note:   expected type `&mut i32`
          found reference `&_`

error[E0599]: no method named `collect` found for struct `std::iter::Copied<std::vec::IntoIter<&mut i32>>` in the current scope
    --> src/main.rs:7:50
     |
7    |       let _iter: Vec<i32> = v.into_iter().copied().collect();
     |                                                    ^^^^^^^ method not found in `std::iter::Copied<std::vec::IntoIter<&mut i32>>`
     |
     = note: the method `collect` exists but the following trait bounds were not satisfied:
             `<std::vec::IntoIter<&mut i32> as std::iter::Iterator>::Item = &_`
             which is required by `std::iter::Copied<std::vec::IntoIter<&mut i32>>: std::iter::Iterator`
             `std::iter::Copied<std::vec::IntoIter<&mut i32>>: std::iter::Iterator`
             which is required by `&mut std::iter::Copied<std::vec::IntoIter<&mut i32>>: std::iter::Iterator`

Clippy version:

$ cargo clippy -V
clippy 0.0.212 (204bb9b 2020-03-17)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant