Skip to content

New lint: inspect-then-for_each on iterators should be simplified #5209

@scottmcm

Description

@scottmcm

Inspired by the change made in https://github.com/rust-lang/rust/pull/69022/files#diff-d1ff3aad2daeb77432493d91c41b722bL147

When an inspect is followed by for_each, that's the same as just doing the thing in the inspect at the beginning of the closure in the for_each.

It's possible that this should only be suggested when a brace-bodied closure is already being passed to for_each, like

    .inspect(|x| do_something(x, 2))
    .for_each(|x| {
        do_more(x);
        update_something(x);
    });

which can just be

    .for_each(|x| {
        do_something(x, 2);
        do_more(x);
        update_something(x);
    });

But not for something like .inspect(foo).for_each(bar), where it's not obvious that the closure is better. (It's not bad, but not an obvious improvement.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lintsL-complexityLint: Belongs in the complexity lint groupL-suggestionLint: Improving, adding or fixing lint suggestionsgood first issueThese issues are a good way to get started with Clippy

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions