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

manual_map suggests a snippet that would not compile for chained if #6969

Closed
magurotuna opened this issue Mar 25, 2021 · 3 comments
Closed
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing

Comments

@magurotuna
Copy link
Contributor

magurotuna commented Mar 25, 2021

I tried this code:

fn main() {
    let a = Some(42);
    let b = Some(0);

    let _c = if let Some(val) = b {
        Some(val + 99)
    } else if let Some(val) = a {
        Some(val * 2)
    } else {
        None
    };
}

Running clippy on the above code, clippy says:

warning: manual implementation of `Option::map`
  --> src/main.rs:7:12
   |
7  |       } else if let Some(val) = a {
   |  ____________^
8  | |         Some(val * 2)
9  | |     } else {
10 | |         None
11 | |     };
   | |_____^ help: try this: `a.map(|val| val * 2)`
   |
   = note: `#[warn(clippy::manual_map)]` on by default
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_map

warning: 1 warning emitted

Following the help message, the code changes to:

let _c = if let Some(val) = b {
    Some(val + 99)
} else a.map(|val| val * 2);

Of course, it doesn't compile:

error: expected `{`, found `a`
  --> src/main.rs:14:12
   |
14 |     } else a.map(|val| val * 2);
   |            ^-------------------
   |            |
   |            expected `{`
   |            help: try placing this code inside a block: `{ a.map(|val| val * 2) }`

error: aborting due to previous error

Meta

  • cargo clippy -V: clippy 0.1.52 (07e0e2e 2021-03-24)
@magurotuna magurotuna added the C-bug Category: Clippy is not doing the correct thing label Mar 25, 2021
@magurotuna
Copy link
Contributor Author

I think the help message should be help: try this: `{ a.map(|val| val * 2) }` in this case.
I'll try to address the issue with this plan.

@rustbot claim

@Jarcho
Copy link
Contributor

Jarcho commented Mar 25, 2021

This was fixed in #6856

@magurotuna
Copy link
Contributor Author

Ooops, I missed that. Thanks for pointing out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing
Projects
None yet
Development

No branches or pull requests

2 participants