Closed
Description
Summary
In the attached scenario unnecessary_lazy_evaluations
is triggered, but it gives a wrong suggestion because the lazy expression relies on the fact that's called only when the number is greater than 0 because otherwise always subtracting 1 would result in an underflow.
Lint Name
unnecessary_lazy_evaluations
Reproducer
I tried this code:
pub fn sub(n: u8) -> Option<u8> {
(n > 0).then(|| n - 1)
}
I saw this happen:
Checking playground v0.0.1 (/playground)
warning: unnecessary closure used with `bool::then`
--> src/lib.rs:4:5
|
4 | (n > 0).then(|| n - 1)
| ^^^^^^^^--------------
| |
| help: use `then_some(..)` instead: `then_some(n - 1)`
|
= 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
warning: `playground` (lib) generated 1 warning (run `cargo clippy --fix --lib -p playground` to apply 1 suggestion)
Finished dev [unoptimized + debuginfo] target(s) in 0.61s
I expected to see this happen:
No lint should be emitted
Version
rustc 1.73.0 (cc66ad468 2023-10-03)
binary: rustc
commit-hash: cc66ad468955717ab92600c770da8c1601a4ff33
commit-date: 2023-10-03
host: x86_64-unknown-linux-gnu
release: 1.73.0
LLVM version: 17.0.2
Additional Labels
No response