-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
unnecessary_lazy_evaluations
does not take cost of expression into account
#8522
Comments
Some checking is already done in eager_or_lazy, but match expressions are not considered an expression type that requires lazy evaluation. Should that be changed? |
Given that |
I just tried to create a demo with side effects, but the trivial example of printing a value doesn't trigger the lint. Regardless, the closure can have an arbitrary cost — this should be quite apparent. As such I think |
Looks like the behavior changed in PR #7639 and there's another issue linked. I don't think I'm qualified to make a decision, but given that multiple people have had issues maybe something should be changed. |
|
That bug was fixed in #11002 and the fix for it should be on nightly |
Seems related: warning: unnecessary closure used with `bool::then`
--> src/main.rs:323:34
|
323 | let import_percentages = (import_total > 0.0).then(|| Percentages {
| __________________________________^
324 | | ok: import_ok * 100.0 / import_total,
325 | | skip: import_skip * 100.0 / import_total,
326 | | unrec: import_unrec * 100.0 / import_total,
327 | | meh: import_meh * 100.0 / import_total,
328 | | err: import_err * 100.0 / import_total,
329 | | });
| |__________^
|
= 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
help: use `then_some(..)` instead
| |
Summary
See reproducer.
Lint Name
unnecessary_lazy_evaluations
Reproducer
I tried this code [playground]:
NB: This is actual code from the time crate.
first_match
is only present to ensure compilation succeeds.I saw this happen:
I expected to see this happen:
unnecessary_lazy_evaluations
should not be triggered. The expression inside theor_else
closure is arbitrary and non-trivial. By requesting the user changeor_else
toor
, whatever the closure contains is now eagerly evaluated at an unknown cost. As such I believe the lint should only trigger when the closure contains a literal or value that is statically known (setting aside const eval). This would ensure that only trivial cases are linted against.Version
Additional Labels
No response
The text was updated successfully, but these errors were encountered: