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

needless_collect warning, when it's actually needed #7900

Open
pornopatsan opened this issue Oct 30, 2021 · 2 comments
Open

needless_collect warning, when it's actually needed #7900

pornopatsan opened this issue Oct 30, 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 I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied L-nursery Lint: Currently in the nursery group

Comments

@pornopatsan
Copy link

pornopatsan commented Oct 30, 2021

Lint name: needless_collect

I tried this code:

pub fn extract<T: IntoIterator>(
    into_iter: T,
    index: usize,
) -> (Option<T::Item>, impl Iterator<Item = T::Item>) {
    let mut iter = into_iter.into_iter();
    let head = iter.by_ref().take(index).collect::<Vec<_>>();
    (iter.next(), head.into_iter().chain(iter))
}

I expected to see no warnings:
Because If I want to read element as index, I need to first read all elements before and store them somewhere.
So I think collect is appropriate here.

Instead, this happened:

warning: avoid using `collect()` when not needed
  --> itertools/src/lib.rs:47:42
   |
47 |     let head = iter.by_ref().take(index).collect::<Vec<_>>();
   |                                          ^^^^^^^
48 |     (iter.next(), head.into_iter().chain(iter))
   |                   ---------------- 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
   |
47 ~     
48 ~     (iter.next(), iter.by_ref().take(index).chain(iter))
   |

What is more, suggested changes do not compile.
@rustbot label +I-suggestion-causes-error

If I write like this

pub fn extract<T: IntoIterator>(
    into_iter: T,
    index: usize,
) -> (Option<T::Item>, impl Iterator<Item = T::Item>) {
    let mut iter = into_iter.into_iter();
    (iter.next(), iter.by_ref().take(index).chain(iter))
}

I, expectedly, get error from rust compiler

   |
45 | ) -> (Option<T::Item>, impl Iterator<Item = T::Item>) {
   |      ------------------------------------------------ opaque type requires that `iter` is borrowed for `'static`
46 |     let mut iter = into_iter.into_iter();
47 |     (iter.next(), iter.by_ref().take(index).chain(iter))
   |                   ^^^^ borrowed value does not live long enough
48 | }
   | - `iter` dropped here while still borrowed
   |

Meta

Rust version (rustc -Vv):

rustc 1.56.0 (09c42c458 2021-10-18)
binary: rustc
commit-hash: 09c42c45858d5f3aedfa670698275303a3d19afa
commit-date: 2021-10-18
host: x86_64-apple-darwin
release: 1.56.0
LLVM version: 13.0.0
@pornopatsan pornopatsan 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 Oct 30, 2021
@rustbot

This comment has been minimized.

@pornopatsan
Copy link
Author

pornopatsan commented Oct 30, 2021

Oh, just created issue, and then found, that it is duplicate, sorry for that
#7512
#7512
#6066

@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Oct 30, 2021
@J-ZhengLi J-ZhengLi added the L-nursery Lint: Currently in the nursery group label Jul 9, 2024
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 I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied L-nursery Lint: Currently in the nursery group
Projects
None yet
Development

No branches or pull requests

3 participants