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

can't allow a single needless_collect #7202

Closed
matthiaskrgr opened this issue May 9, 2021 · 0 comments · Fixed by #7282
Closed

can't allow a single needless_collect #7202

matthiaskrgr opened this issue May 9, 2021 · 0 comments · Fixed by #7282
Labels
C-bug Category: Clippy is not doing the correct thing

Comments

@matthiaskrgr
Copy link
Member

I tried this code:

fn main() {
    let v = vec![1, 2, 3, 2, 4];
    #[allow(clippy::needless_collect)]
    let v: Vec<i32> = v.into_iter().map(|x| x * 2).collect();
}

This code should not warn but it does.

warning: unused variable: `v`
 --> src/main.rs:4:9
  |
4 |     let v: Vec<i32> = v.into_iter().map(|x| x * 2).collect();
  |         ^ help: if this is intentional, prefix it with an underscore: `_v`
  |
  = note: `#[warn(unused_variables)]` on by default

warning: avoid using `collect()` when not needed
 --> src/main.rs:4:52
  |
4 |     let v: Vec<i32> = v.into_iter().map(|x| x * 2).collect();
  |                       -------------                ^^^^^^^
  |                       |
  |                       the iterator could be used here instead
  |
  = note: `#[warn(clippy::needless_collect)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_collect
help: use the original Iterator instead of collecting it and then producing a new one
  |
4 |     v.into_iter().map(|x| x * 2).map(|x| x * 2).collect();
  |    --                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

warning: 2 warnings emitted

    Finished dev [unoptimized + debuginfo] target(s) in 0.68s

A global #![allow(clippy::needless_collect)] works though.. 🤔

Playground

Meta

clippy 0.1.54 (881c1ac 2021-05-08)

@matthiaskrgr matthiaskrgr added the C-bug Category: Clippy is not doing the correct thing label May 9, 2021
@bors bors closed this as completed in 8066f83 May 27, 2021
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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant