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: catch x: Vec<_> = iter.collect(); x.into_iter() ... #5627

Closed
matthiaskrgr opened this issue May 20, 2020 · 2 comments
Closed
Labels
E-medium Call for participation: Medium difficulty level problem and requires some initial experience. L-perf Lint: Belongs in the perf lint group

Comments

@matthiaskrgr
Copy link
Member

pub fn main() {
    // we start with an iterator
    let iter = vec![1, 2, 3].into_iter();
    // we do something with the items and collect into a vec
    let v: Vec<i32> = iter.map(|x| x + 2).collect();
    // but the first and only use of the vec uses .iter() or .into_iter(), which means we could skip collecting entirely.
    v.into_iter().for_each(|x| println!("{}", x));
}

Would be awesome if clippy could catch such patterns :)

@matthiaskrgr matthiaskrgr added the L-perf Lint: Belongs in the perf lint group label May 21, 2020
@flip1995 flip1995 added the E-medium Call for participation: Medium difficulty level problem and requires some initial experience. label May 21, 2020
@JarredAllen
Copy link
Contributor

I can implement this

flip1995 added a commit to flip1995/rust-clippy that referenced this issue Aug 4, 2020
needless_collect: catch x: Vec<_> = iter.collect(); x.into_iter() ...

changelog: Expand the needless_collect lint as suggested in rust-lang#5627 (WIP).

This PR is WIP because I can't figure out how to make the multi-part suggestion include its changes in the source code (the fixed is identical to the source, despite the lint making suggestions). Aside from that one issue, I think this should be good.
flip1995 added a commit to flip1995/rust-clippy that referenced this issue Aug 4, 2020
needless_collect: catch x: Vec<_> = iter.collect(); x.into_iter() ...

changelog: Expand the needless_collect lint as suggested in rust-lang#5627 (WIP).

This PR is WIP because I can't figure out how to make the multi-part suggestion include its changes in the source code (the fixed is identical to the source, despite the lint making suggestions). Aside from that one issue, I think this should be good.
@ebroto
Copy link
Member

ebroto commented Aug 26, 2020

Fixed in #5837

@ebroto ebroto closed this as completed Aug 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E-medium Call for participation: Medium difficulty level problem and requires some initial experience. L-perf Lint: Belongs in the perf lint group
Projects
None yet
Development

No branches or pull requests

4 participants