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 suggestion cannot be applied: if let.. else if let .. chain #6847

Closed
matthiaskrgr opened this issue Mar 4, 2021 · 1 comment · Fixed by #6856
Closed

manual_map suggestion cannot be applied: if let.. else if let .. chain #6847

matthiaskrgr opened this issue Mar 4, 2021 · 1 comment · Fixed by #6856
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@matthiaskrgr
Copy link
Member

Lint name: manual_map

I tried this code:

fn fun(a: Option<i32>) -> Option<i32> {
    if let Some(i) = a {
        Some(1 + i)
    } else if let Some(j) = a {
        Some(3 + j)
    } else {
        None
    }
}

Clippy suggests this:

warning: manual implementation of `Option::map`
  --> src/main.rs:8:12
   |
8  |       } else if let Some(j) = a {
   |  ____________^
9  | |         Some(3 + j)
10 | |     } else {
11 | |         None
12 | |     }
   | |_____^ help: try this: `a.map(|j| 3 + j)`
   |
   = 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: 2 warnings emitted

but it fails to apply because we have an if .. else if .. else chain which is not taken into account and the suggestion causes syntax errors.

The following errors were reported:
error: expected `{`, found `a`
 --> src/main.rs:8:12
  |
8 |     } else a.map(|j| 3 + j)
  |            ^---------------
  |            |
  |            expected `{`
  |            help: try placing this code inside a block: `{ a.map(|j| 3 + j) }`

error: aborting due to previous error

cc @Jarcho ?

@matthiaskrgr matthiaskrgr added C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied I-false-positive Issue: The lint was triggered on code it shouldn't have labels Mar 4, 2021
@camsteffen
Copy link
Contributor

I think it should still suggest with a block else { a.map(|j| 3 + j) }, so not a false positive.

@matthiaskrgr matthiaskrgr removed the I-false-positive Issue: The lint was triggered on code it shouldn't have label Mar 5, 2021
@bors bors closed this as completed in ba13311 Mar 17, 2021
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 I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants