clippy
occasionally yells at me:
warning: unnecessary closure used to substitute value for `Option::None`
--> src/bin/example.rs:324:19
|
324 | let cert_data = secret
| ___________________^
325 | | .data
326 | | .as_ref()
327 | | .ok_or_else(|| CertReportError::SecretDataMissing)?
328 | | .get("tls.crt")
329 | | .ok_or_else(|| CertReportError::SecretTlsCrtMissing)?
| |________________________________________________________^
|
= note: `#[warn(clippy::unnecessary_lazy_evaluations)]` on by default
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_lazy_evaluations
help: use `ok_or` instead
I.e., ditch the closure, and eagerly construct the variant. I do this if the underlying type is small (a machine word or less), but when it's large I would normally reach for the ok_or_else(|| …)
. I actually thought there was a clippy
for the "you're constructing a large type, do it lazily!" but I can't get it to trigger at the moment.
The help leaves me unfulfilled:
Why is this bad?
Using eager evaluation is shorter and simpler in some cases.
"in some cases" … [which?], and I don't think this is one of those cases. What's the criteria for this lint?
Putting this in Godbolt, it actually seems like the same code gets generated either way. (A test for the failure case, followed by a jump to a "return error" stub, and the error value is constructed after the jump.)