FP in collapsible_match on futures select! macro · Issue #7591 · rust-lang/rust-clippy (original) (raw)

I tried this code:

use futures::executor::block_on; use futures::future; use futures::select;

fn main() { let mut a = future::ready(Some(4)); let mut b = future::pending::<()>();

block_on(async {
    let mut res = 0;
    select! {
        a_res = a => if let Some(a_res) = a_res { res = a_res + 1 },
        _ = b => (),
    };
    assert_eq!(res, 5);
})

}

I expected to see no clippy error

Instead, this happened:

warning: unnecessary nested `if let` or `match`
  --> src/main.rs:12:26
   |
12 |             a_res = a => if let Some(a_res) = a_res { res = a_res + 1 },
   |                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: `#[warn(clippy::collapsible_match)]` on by default
help: the outer pattern can be modified to include the inner pattern
  --> src/main.rs:12:13
   |
12 |             a_res = a => if let Some(a_res) = a_res { res = a_res + 1 },
   |             ^^^^^               ^^^^^^^^^^^ with this pattern
   |             |
   |             replace this binding
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#collapsible_match

clippy seems to want a different code which is not valid:

Some(a_res) = a => res = a_res + 1,

leading to

  --> src/main.rs:11:9
   |
11 | /         select! {
12 | |             Some(a_res) = a => res = a_res + 1,
13 | |             _ = b => (),
14 | |         };
   | |          ^
   | |          |
   | |          pattern `_0(None)` not covered
   | |__________`main::{closure#0}::__PrivResult<std::option::Option<i32>, ()>` defined here
   |            not covered

Meta

➜  clippy_test cargo clippy -V
clippy 0.1.56 (0035d9dc 2021-08-16)

It's a recent regression

Notably, I found that with

clippy 0.1.56 (2d2bc94c 2021-08-15)

things work fine

came up in the testsuite of rust-lang/futures-rs#2479