Skip to content

Suggest use of unwrap_or_default #7250

Closed
@ndmitchell

Description

@ndmitchell

What it does

Suggest using Option::unwrap_or_default when Option::unwrap_or_else is called using a function that creates the default.

Categories (optional)

  • Kind: style

Makes the code shorter and more idiomatic.

Drawbacks

None.

Example

let x : Option<_> = ...
x.unwrap_or_else(Vec::new)

Could be written as:

let x : Option<_> = ...
x.unwrap_or_default()

Requires an understanding of what closures produce default values - e.g. Vec::default, Default::default, Vec::new, || Vec::new().

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-enhancementCategory: Enhancement of lints, like adding more cases or adding help messages

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions