Skip to content

needless_collect: catch x: Vec<_> = iter.collect(); x.into_iter() ... #5627

Closed
@matthiaskrgr

Description

@matthiaskrgr
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 :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    E-mediumCall for participation: Medium difficulty level problem and requires some initial experience.L-perfLint: Belongs in the perf lint group

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions