RS.CLIPPY.OPTION_IF_LET_ELSE

Reimplementation of Option::map_or

This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: option_if_let_else. Copyright ©2025 The Rust Team. All rights reserved.

What it does

Lints usage of if let Some(v) = ... { y } else { x } and match .. { Some(v) => y, None/_ => x } which are more idiomatically done with Option::map_or (if the else bit is a pure expression) or Option::map_or_else (if the else bit is an impure expression).

Why is this bad?

Using the dedicated functions of the Option type is clearer and more concise than an if let expression.

Notes

This lint uses a deliberately conservative metric for checking if the inside of either body contains loop control expressions break or continue (which cannot be used within closures). If these are found, this lint will not be raised.

Example

let _ = if let Some(foo) = optional {
    foo
} else {
    5
};
let _ = match optional {
    Some(val) => val + 1,
    None => 5
};
let _ = if let Some(foo) = optional {
    foo
} else {
    let y = do_complicated_function();
    y*y
};

should be

let _ = optional.map_or(5, |foo| foo);
let _ = optional.map_or(5, |val| val + 1);
let _ = optional.map_or_else(||{
    let y = do_complicated_function();
    y*y
}, |foo| foo);