Skip to content

Suggest to replace Option.into_iter().filter_map(func).next() with Option.and_then(func) #13488

Open

Description

What it does

Somehow I always want to use filter_map with Option, and I always forget that it's equivalent to Option::and_then, so I end up doing this contrived way that uses a few functions. This lint would remind me that and_then exists and could and should be used in this case.

Advantage

Remove two intermediate function calls, with the complexity they entail, and use a more idiomatic Rust way.

Drawbacks

Maybe too targeted to my brain's dysfunctions 🤪

Example

let a_value = Some(42);
let x = a_value.into_iter().filter_map(|value| if value < 30 { Some(value + 13) } else { None }).next();

Could be written as:

let a_value = Some(42);
let x = a_value.and_then(|value| if value < 30 { Some(value + 13) } else { None });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions