Skip to content

manual_filter suggestion broken on nested if let #10088

Closed
@niklasf

Description

@niklasf

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions