Skip to content

unnecessary_lazy_evaluations and once_cell::sync::Lazy #10462

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

Closed
arifd opened this issue Mar 7, 2023 · 0 comments · Fixed by #10896
Closed

unnecessary_lazy_evaluations and once_cell::sync::Lazy #10462

arifd opened this issue Mar 7, 2023 · 0 comments · Fixed by #10896
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@arifd
Copy link

arifd commented Mar 7, 2023

Summary

I believe this lint should have a specialization for the common case when a Lazy is being de-referenced. e.g. unwrapping lazily constructed defaults

Lint Name

unnecessary_lazy_evaluations

Reproducer

I tried this code:

use once_cell::sync::Lazy;

static FOO: Lazy<i32> = Lazy::new(Default::default);

fn main() {
    let foo = Some(1);
    let foo = foo.as_ref().unwrap_or_else(|| &FOO);
    println!("{foo}");
}

I saw this happen:

warning: unnecessary closure used to substitute value for `Option::None`
 --> src/main.rs:7:15
  |
7 |     let foo = foo.as_ref().unwrap_or_else(|| &FOO);
  |               ^^^^^^^^^^^^^-----------------------
  |                            |
  |                            help: use `unwrap_or(..)` instead: `unwrap_or(&FOO)`
  |
  = 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

I expected to see this happen:

No problimo

Version

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=58c7612aed45b666f3cd499ebe3a60ce

Additional Labels

No response

@arifd arifd added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Mar 7, 2023
@arifd arifd changed the title unnecessary_lazy_evaluations and once_cell::sync::Lazy unnecessary_lazy_evaluations and once_cell::sync::Lazy Mar 7, 2023
@bors bors closed this as completed in 7c34ec8 Jun 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant