Skip to content

Suggest the use of bool::then_some #15103

Closed
@leonardo-m

Description

@leonardo-m

What it does

Replace a if-then with the use of bool::then_some.

Advantage

Sometimes it could simplify the code.

Drawbacks

It uses less common methods instead of basic language constructs, so it makes the code a little less simple.

Example

pub fn foo(b: bool, x: u32) -> Option<u32> {
    if b {
        Some(x)
    } else {
        None
    }
}

Could be written as:

pub fn bar(b: bool, x: u32) -> Option<u32> {
    b.then_some(x)
}

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