Skip to content

manual_filter suggestion broken on nested if let #10088

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

Closed
niklasf opened this issue Dec 15, 2022 · 2 comments · Fixed by #10091
Closed

manual_filter suggestion broken on nested if let #10088

niklasf opened this issue Dec 15, 2022 · 2 comments · Fixed by #10091
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

@niklasf
Copy link
Contributor

niklasf commented Dec 15, 2022

Summary

When using nested if let in a way that triggers clippy::manual_filter, clippy makes an invalid suggestion.

Reproducer

I tried this code:

fn maybe_some() -> Option<u32> {
    Some(0)
}

fn main() {
    let result = if let Some(a) = maybe_some() {
        if let Some(b) = maybe_some() {
            Some(a + b)
        } else {
            Some(a)
        }
    } else {
        None
    };
    
    println!("{result:?}");
}

I expected to see this happen: No lint error, or suggested fix compiles.

Instead, this happened:

warning: manual implementation of `Option::filter`
  --> src/main.rs:6:17
   |
6  |       let outer = if let Some(a) = maybe_some() {
   |  _________________^
7  | |         if let Some(b) = maybe_some() {
8  | |             Some(a + b)
9  | |         } else {
...  |
13 | |         None
14 | |     };
   | |_____^ help: try this: `maybe_some().filter(|&a| !(let Some(b) = maybe_some()))`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_filter
   = note: `#[warn(clippy::manual_filter)]` on by default

Version

0.1.67 (2022-12-14 b70baa4)
@niklasf niklasf added the C-bug Category: Clippy is not doing the correct thing label Dec 15, 2022
@niklasf
Copy link
Contributor Author

niklasf commented Dec 15, 2022

@rustbot label +I-suggestion-causes-error

@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Dec 15, 2022
@niklasf niklasf changed the title manual_filter suggestion broken on nested if let manual_filter suggestion broken on nested if let Dec 15, 2022
@ericwu17
Copy link
Contributor

I'm currently trying to tackle this issue and I realized that the code below also causes clippy to emit a code suggestion that is not correct:

fn main() {
    match Some(0) {
        None => None,
        Some(x) => {
            if x > 0 {
                Some(3*x)
            } else {
                Some(x)
            }
        },
    };
}

Here clippy gives the suggestion:

warning: manual implementation of `Option::filter`
  --> src/main.rs:6:5
   |
6  | /     match Some(0) {
7  | |         None => None,
8  | |         Some(x) => {
9  | |             if x > 0 {
...  |
14 | |         },
15 | |     };
   | |_____^ help: try this: `Some(0).filter(|&x| x <= 0)`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_filter

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.

3 participants