Skip to content

Why does Clippy not suggest removing an Option::map call with an "identity" #3236

@mandx

Description

@mandx

Playing around with the following code, I accidentally left an map() call on the Option type (originally, the closure was dereferencing the parameter, or calling clone() on it, something like that). But I noticed that Clippy doesn't suggests that using map() like this is unnecessary:

fn fib(n: usize, memo: &mut [Option<usize>]) -> usize {
    memo[n].map(|v| v).unwrap_or_else(|| {
        let result = {
            if n > 1 {
                fib(n - 1, memo) + fib(n - 2, memo)
            } else {
                1
            }
        };
        memo[n] = Some(result);
        result
    })
}

fn main() {
    let number = 46;
    let mut memo: Vec<Option<usize>> = vec![None; number + 1];
    println!("{}", fib(number, &mut memo));
}

And if I turn on "pedantic" mode, the suggestion I get is to use map_or_else.

Play link

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions