Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mandx opened this issue Sep 29, 2018 · 1 comment
Closed

Comments

@mandx
Copy link

mandx commented Sep 29, 2018

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

@camsteffen
Copy link
Contributor

Today this lints as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants