RS.CLIPPY.UNNECESSARY_MAP_OR

Reduce unnecessary calls to `.map_or(bool, …)`

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

What it does

Converts some constructs mapping an Enum value for equality comparison.

Why is this bad?

Calls such as opt.map_or(false, |val| val == 5) are needlessly long and cumbersome, and can be reduced to, for example, opt == Some(5) assuming opt implements PartialEq. Also, calls such as opt.map_or(true, |val| val == 5) can be reduced to opt.is_none_or(|val| val == 5). This lint offers readability and conciseness improvements.

Example

pub fn a(x: Option<i32>) -> (bool, bool) {
    (
        x.map_or(false, |n| n == 5),
        x.map_or(true, |n| n > 5),
    )
}

Use instead:

pub fn a(x: Option<i32>) -> (bool, bool) {
    (
        x == Some(5),
        x.is_none_or(|n| n > 5),
    )
}