Skip to content

Lint Suggestion: suggest Option::map instead of Option::and_then returning Option #5936

@NoraCodes

Description

@NoraCodes

What it does

Suggests that code using .and_then() to take an Option and return an Option should use .map instead, which is meant for this purpose.

Categories (optional)

  • Kind: clippy::style

Advantages

Using .map in this instance is less verbose and more semantically clear than the alternative. This is what .map is for; .and_then is more general and thus it is less obvious what's happening.

Drawbacks

None.

Example

let vector = vec![1, 2, 3]; let maybe: Option<i32> = vector.get(1).and_then(|x| Some(x - 1));

Could be written as:

let vector = vec![1, 2, 3]; let maybe: Option<i32> = vector.get(1).map(|x| x - 1);

Metadata

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