This is a simplified version of an example that triggers clippy's unnecessary_lazy_evaluations
lint:
#![warn(clippy::unnecessary_lazy_evaluations)]
pub enum Foo {
A(String),
B(Box<Foo>),
}
fn bar(s: &str) -> Option<Foo> {
if s.len() < 42 {
Some(Foo::B(Box::new(Foo::A("hello".to_owned()))))
} else {
None
}
}
pub fn foo(s: String) -> Foo {
bar(&s).unwrap_or_else(|| Foo::A(s))
}
It suggests that the last line be replaced with bar(&s).unwrap_or(Foo::A(s))
. Is that a good idea? Assuming that everything except bar
gets inlined and optimized, does building a value using an enum constructor count as work that ought to be delayed in the case that bar
returns Some(...)
? I tried looking at the MIR for this in the playground but it doesn't look like it's being optimized.