Closed
Description
Lint name: manual_flatten
I tried this code:
Code is simplified so workers
are never set, but to trigger the incorrect sugestion it is enough.
The full code starts same with all workers doing nothing, and later in the loop assign work to them, and here early in the loop let them finish the work if it is the right time.
fn main() {
let mut time = 0;
let mut workers : [Option<(u8, i32)>; 5] = [None, None, None, None, None];
loop {
for w in &mut workers {
if let Some((_to, when)) = w {
if time == *when {
// ...
*w = None;
}
}
}
// ...
time += 1;
}
}
I expected to see this happen: No suggestion probably.
Instead, this happened:
warning: unnecessary `if let` since only the `Some` variant of the iterator element is used
--> src\main.rs:6:9
|
6 | for w in &mut workers {
| ^ ------------ help: try: `workers.iter_mut().flatten()`
| _________|
| |
7 | | if let Some((_to, when)) = w {
8 | | if time == *when {
9 | | *w = None;
10 | | }
11 | | }
12 | | }
| |_________^
|
= note: `#[warn(clippy::manual_flatten)]` on by default
help: ...and remove the `if let` statement in the for loop
--> src\main.rs:7:13
|
7 | / if let Some((_to, when)) = w {
8 | | if time == *when {
9 | | *w = None;
10 | | }
11 | | }
| |_____________^
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_flatten
warning: 1 warning emitted
In this simplified example line 9 *w = None;
is visible.
Am I missing a way how to handle this line? I asked on Rust community server and no one find a way how to make this work, so I think it must be false positive.
Meta
cargo clippy -V
: clippy 0.1.52 (fe1bf8e 2021-02-23)rustc -Vv
:
rustc 1.52.0-nightly (fe1bf8e05 2021-02-23)
binary: rustc
commit-hash: fe1bf8e05c39bdcc73fc09e246b7209444e389bc
commit-date: 2021-02-23
host: x86_64-pc-windows-msvc
release: 1.52.0-nightly
LLVM version: 11.0.1