warning: unnecessary closure used with `bool::then`
--> src/percentage.rs:8:3
|
8 | / (min..=max)
9 | | .contains(&percentage)
10 | | .then(|| Self((percentage - min) / (max - min)))
| |______________----------------------------------------------^
| |
| help: use `then_some(..)` instead: `then_some(Self((percentage - min) / (max - min)))`
|
= 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
This seems odd to me. I see the minus, plus and division operations as "functions", and I thought using closures here would delay the execution of these functions until they are needed (lazy evaluation). Am I misunderstanding something?
Because you're only doing "cheap" operations, clippy's preference is to avoid the closure. Doing arithmetic on primitive number types is so well understood by the compiler/optimizer that if you don't actually use the result the compiled program can easily avoid computing it, and also in the other direction, that even if the operation isn't performed in the source it might still be computed speculatively because that's easier than not.
If the operations could overflow, though, those side effects are properly deferred, and this would be a false positive on the lint.
Clippy warns on foo_lazy, and suggests converting it to be equivalent to foo_eager, but the tests show that one returns None where wanted and the other panics.