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 report of needless collect with the sequence from_fn().collect() follow by vec.into_iter().rev().collect() #8132

Open
galop1n opened this issue Dec 16, 2021 · 2 comments
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

@galop1n
Copy link

galop1n commented Dec 16, 2021

Summary

The iterator in front of the first collect is not bi-directional. clippy fail at identify that the subsequent rev() would not accept it if the operation were chained as suggested.

Lint Name

needless_collect

Reproducer

I tried this code:

fn main() {
    let mut i = 0;
    let a = std::iter::from_fn(move || {
        i += 1;
        if i < 10 {
            Some(i)
        } else {
            None
        }
    })
    .collect::<Vec<usize>>();
    println!("{:?}", a.into_iter().rev().collect::<Vec<usize>>());
}

I saw this happen:

warning: avoid using `collect()` when not needed
  --> src/main.rs:11:6
   |
11 |     .collect::<Vec<usize>>();
   |      ^^^^^^^
12 |     println!("{:?}", a.into_iter().rev().collect::<Vec<usize>>());
   |                      ------------- 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
   |
3  ~     
4  ~     println!("{:?}", std::iter::from_fn(move || {
5  +         i += 1;
6  +         if i < 10 {
7  +             Some(i)
8  +         } else {
 ...

warning: `playground` (bin "playground") generated 1 warning

I expected to see this happen:

Version

No response

Additional Labels

l-suggestion-causes-error

@galop1n galop1n 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 Dec 16, 2021
@Testare
Copy link

Testare commented Mar 2, 2022

Same issue here

@kyp44
Copy link

kyp44 commented Jun 18, 2022

Having the same issue in commit 34a576b2be14289d6b95928508e557dcf217594c of my project here: https://github.com/kyp44/advent-of-code.

It seems that this lint should probably not flag if .rev() is used and the original iterator does not implement DoubleEndedIterator.

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

No branches or pull requests

3 participants