rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

`Option::and_then(|v| ...(&v).then_some(v))` to `Option::filter(|v| ...(v))`

Open A4-Tacks opened this issue 1 year ago • 1 comments

What it does

Improve the code to be more concise

Advantage

  • Less and cleaner

Drawbacks

No response

Example

fn foo() {
    let x = Some("foo".to_owned());

    let _y = x.and_then(|v| v.starts_with('f')
        .then_some(v));
}

Could be written as:

fn foo() {
    let x = Some("foo".to_owned());

    let _y = x.filter(|v| v.starts_with('f'));
}

A4-Tacks avatar Jun 22 '24 11:06 A4-Tacks

@rustbot claim

lolbinarycat avatar Jun 22 '24 20:06 lolbinarycat