Closed
Description
Summary
In the example below, unnecessary_lazy_evaluations
suggests a fix that is not functionally equivalent to the original code and that panics (the original code works normally).
Lint Name
unnecessary_lazy_evaluations
Reproducer
I tried this code:
fn try_sub(a: u32, b: u32) -> Option<u32> {
(a >= b).then(|| a - b)
//(a >= b).then_some(a - b) // Suggested by clippy but panic...
}
fn main() {
assert_eq!(try_sub(10, 4), Some(6));
assert_eq!(try_sub(4, 10), None);
}
I saw this happen:
warning: unnecessary closure used with `bool::then`
--> src/main.rs:2:5
|
2 | (a >= b).then(|| a - b)
| ^^^^^^^^^--------------
| |
| help: use `then_some(..)` instead: `then_some(a - b)`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_lazy_evaluations
= note: `#[warn(clippy::unnecessary_lazy_evaluations)]` on by default
I expected to see this happen: No suggestion.
Version
rustc 1.74.0 (79e9716c9 2023-11-13)
binary: rustc
commit-hash: 79e9716c980570bfd1f666e3b16ac583f0168962
commit-date: 2023-11-13
host: x86_64-unknown-linux-gnu
release: 1.74.0
LLVM version: 17.0.4
Additional Labels
No response