rust-lang / rust-clippy

A bunch of lints to catch common mistakes and improve your Rust code. Book: https://doc.rust-lang.org/clippy/
https://rust-lang.github.io/rust-clippy/
Other
11.02k stars 1.48k forks source link

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

Open A4-Tacks opened 1 week ago

A4-Tacks commented 1 week ago

What it does

Improve the code to be more concise

Advantage

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'));
}
lolbinarycat commented 1 week ago

@rustbot claim