Rust-clippy: Incorrect unnecessary_lazy_evaluations suggestion

Created on 16 Sep 2020  路  3Comments  路  Source: rust-lang/rust-clippy

It seems like clippy has problems detecting if a lazy evaluation is unnecessary when calling a function inside the closure and accessing fields on that returned value. I'm pretty sure this is a new issue since CI hasn't caught it previously.

This example should show what's wrong:

fn main() {
    let _ = Some(3).unwrap_or_else(|| foo().0);
    let _ = Some(3).unwrap_or_else(|| bar().a);
}

fn foo() -> (usize, usize) {
    (0, 0)
}

struct X { a: usize }
fn bar() -> X {
    X { a: 0 }
}

This shouldn't be allowed since the foo and bar function could obviously be very expensive. So unwrap_or isn't the correct function to use.

L-bug

All 3 comments

Maybe this is fixed with #5937. cc @montrivo

The pr seems to correct this case. I checked by running uitest on the example.

Thanks for checking. Closing this, since #5937 doesn't auto-close it.

Was this page helpful?
0 / 5 - 0 ratings