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

FP: needless_collect for HashMap and BTreeMap #7164

Closed
mgacek8 opened this issue May 4, 2021 · 1 comment · Fixed by #7188
Closed

FP: needless_collect for HashMap and BTreeMap #7164

mgacek8 opened this issue May 4, 2021 · 1 comment · Fixed by #7188
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@mgacek8
Copy link
Contributor

mgacek8 commented May 4, 2021

Lint name: needless_collect

I tried this code:

use std::collections::{HashMap, BTreeMap};

fn main() {
    let sample = [1; 5];
    let a = sample.iter().map(|x| (x, x)).count();
    let b = sample.iter().map(|x| (x, x)).collect::<HashMap<_, _>>().len();
    let c = sample.iter().map(|x| (x, x)).collect::<BTreeMap<_, _>>().len();
    println!("{}, {}, {}", a, b, c);
}

I expected to see this happen: No lint triggered.
Instead, this happened:
Warning from clippy:


warning: avoid using `collect()` when not needed
 --> src/main.rs:6:43
  |
6 |     let b = sample.iter().map(|x| (x, x)).collect::<HashMap<_, _>>().len();
  |                                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `count()`
  |
  = 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

warning: avoid using `collect()` when not needed
 --> src/main.rs:7:43
  |
7 |     let c = sample.iter().map(|x| (x, x)).collect::<BTreeMap<_, _>>().len();
  |                                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `count()`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_collect

After applying suggestion from clippy, the results are different.
Before applying the suggestion: 1, after: 5.

playgroud link

Meta

  • clippy: 0.1.53 (2021-05-03 716394d)
  • rustc: 1.54.0-nightly (2021-05-03 716394d)
@mgacek8 mgacek8 added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels May 4, 2021
@mgacek8
Copy link
Contributor Author

mgacek8 commented May 4, 2021

@rustbot claim

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-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant