rust-lang / rust-clippy

A bunch of lints to catch common mistakes and improve your Rust code. Book: https://doc.rust-lang.org/clippy/
https://rust-lang.github.io/rust-clippy/
Other
11.03k stars 1.48k forks source link

Recommend `std::sync::LazyLock` over other lazy static options #12895

Open pitaj opened 1 month ago

pitaj commented 1 month ago

What it does

Warn on usage of the lazy_static! macro or the Lazy type from once_cell in declarations of static variables. Crates with an older MSRB or no_std creates would be excluded.

These solutions have been superceded by the nearly stabilized std::sync::LazyLock type (on track for 1.80.0).

Advantage

Drawbacks

Churn

Example

lazy_static! {
    static ref FOO: String = "foo".to_uppercase();
}
static BAR: Lazy<String> = Lazy::new(|| "BAR".to_lowercase());

Could be written as:

static FOO: LazyLock<String> = LazyLock::new(|| "foo".to_uppercase());
static BAR: LazyLock<String> = LazyLock::new(|| "BAR".to_lowercase());
J-ZhengLi commented 1 month ago

@rustbot claim