about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorJ-ZhengLi <lizheng135@huawei.com>2024-06-24 09:52:09 +0800
committerPeter Jaszkowiak <p.jaszkow@gmail.com>2025-01-24 14:06:46 -0700
commit2671e4a5c19492dba76905cc1e27b5dfba5fb34f (patch)
tree36671870958a25c54fdd54dd13d38ee5ac347d6f /tests
parentd7e20a9c163ad00ebc93671fd123dc11ee0dae8b (diff)
add new lint `non_std_lazy_statics`
detect usage of `once_cell::sync::Lazy` and `lazy_static!`,
recommending usage of `std::sync::LazyLock` instead
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/non_std_lazy_static/auxiliary/lazy_static.rs20
-rw-r--r--tests/ui/non_std_lazy_static/auxiliary/once_cell.rs55
-rw-r--r--tests/ui/non_std_lazy_static/non_std_lazy_static_fixable.fixed72
-rw-r--r--tests/ui/non_std_lazy_static/non_std_lazy_static_fixable.rs72
-rw-r--r--tests/ui/non_std_lazy_static/non_std_lazy_static_fixable.stderr100
-rw-r--r--tests/ui/non_std_lazy_static/non_std_lazy_static_no_std.rs20
-rw-r--r--tests/ui/non_std_lazy_static/non_std_lazy_static_other_once_cell.rs12
-rw-r--r--tests/ui/non_std_lazy_static/non_std_lazy_static_unfixable.rs43
-rw-r--r--tests/ui/non_std_lazy_static/non_std_lazy_static_unfixable.stderr66
9 files changed, 460 insertions, 0 deletions
diff --git a/tests/ui/non_std_lazy_static/auxiliary/lazy_static.rs b/tests/ui/non_std_lazy_static/auxiliary/lazy_static.rs
new file mode 100644
index 00000000000..85fb4e66079
--- /dev/null
+++ b/tests/ui/non_std_lazy_static/auxiliary/lazy_static.rs
@@ -0,0 +1,20 @@
+//! **FAKE** lazy_static crate.
+
+#[macro_export]
+macro_rules! lazy_static {
+    (static ref $N:ident : $T:ty = $e:expr; $($t:tt)*) => {
+        static $N : &::core::marker::PhantomData<$T> = &::core::marker::PhantomData;
+
+        $crate::lazy_static! { $($t)* }
+    };
+    () => ()
+}
+
+#[macro_export]
+macro_rules! external {
+    () => {
+        $crate::lazy_static! {
+            static ref LZ_DERP: u32 = 12;
+        }
+    };
+}
diff --git a/tests/ui/non_std_lazy_static/auxiliary/once_cell.rs b/tests/ui/non_std_lazy_static/auxiliary/once_cell.rs
new file mode 100644
index 00000000000..e860a0f7572
--- /dev/null
+++ b/tests/ui/non_std_lazy_static/auxiliary/once_cell.rs
@@ -0,0 +1,55 @@
+//! **FAKE** once_cell crate.
+
+pub mod sync {
+    use std::marker::PhantomData;
+
+    pub struct Lazy<T, F = fn() -> T> {
+        cell: PhantomData<T>,
+        init: F,
+    }
+    unsafe impl<T, F: Send> Sync for Lazy<T, F> {}
+    impl<T, F> Lazy<T, F> {
+        pub const fn new(f: F) -> Lazy<T, F> {
+            Lazy {
+                cell: PhantomData,
+                init: f,
+            }
+        }
+
+        pub fn into_value(this: Lazy<T, F>) -> Result<T, F> {
+            unimplemented!()
+        }
+
+        pub fn force(_this: &Lazy<T, F>) -> &T {
+            unimplemented!()
+        }
+
+        pub fn force_mut(_this: &mut Lazy<T, F>) -> &mut T {
+            unimplemented!()
+        }
+
+        pub fn get(_this: &Lazy<T, F>) -> Option<&T> {
+            unimplemented!()
+        }
+
+        pub fn get_mut(_this: &mut Lazy<T, F>) -> Option<&mut T> {
+            unimplemented!()
+        }
+    }
+}
+pub mod race {
+    pub struct OnceBox<T>(T);
+
+    impl<T> OnceBox<T> {
+        pub fn get(&self) -> Option<&T> {
+            Some(&self.0)
+        }
+    }
+}
+
+#[macro_export]
+macro_rules! external {
+    () => {
+        static OC_DERP: $crate::sync::Lazy<u32> = $crate::sync::Lazy::new(|| 12);
+    };
+}
diff --git a/tests/ui/non_std_lazy_static/non_std_lazy_static_fixable.fixed b/tests/ui/non_std_lazy_static/non_std_lazy_static_fixable.fixed
new file mode 100644
index 00000000000..f7c56b6fffe
--- /dev/null
+++ b/tests/ui/non_std_lazy_static/non_std_lazy_static_fixable.fixed
@@ -0,0 +1,72 @@
+//@aux-build:once_cell.rs
+//@aux-build:lazy_static.rs
+
+#![warn(clippy::non_std_lazy_statics)]
+#![allow(static_mut_refs)]
+
+use once_cell::sync::Lazy;
+
+fn main() {}
+
+static LAZY_FOO: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| "foo".to_uppercase());
+//~^ ERROR: this type has been superceded by `LazyLock` in the standard library
+static LAZY_BAR: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| {
+    //~^ ERROR: this type has been superceded by `LazyLock` in the standard library
+    let x = "bar";
+    x.to_uppercase()
+});
+static LAZY_BAZ: std::sync::LazyLock<String> = { std::sync::LazyLock::new(|| "baz".to_uppercase()) };
+//~^ ERROR: this type has been superceded by `LazyLock` in the standard library
+static LAZY_QUX: std::sync::LazyLock<String> = {
+    //~^ ERROR: this type has been superceded by `LazyLock` in the standard library
+    if "qux".len() == 3 {
+        std::sync::LazyLock::new(|| "qux".to_uppercase())
+    } else if "qux".is_ascii() {
+        std::sync::LazyLock::new(|| "qux".to_lowercase())
+    } else {
+        std::sync::LazyLock::new(|| "qux".to_string())
+    }
+};
+
+fn non_static() {
+    let _: Lazy<i32> = Lazy::new(|| 1);
+    let _: Lazy<String> = Lazy::new(|| String::from("hello"));
+    #[allow(clippy::declare_interior_mutable_const)]
+    const DONT_DO_THIS: Lazy<i32> = Lazy::new(|| 1);
+}
+
+mod once_cell_lazy_with_fns {
+    use once_cell::sync::Lazy;
+
+    static LAZY_FOO: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| "foo".to_uppercase());
+    //~^ ERROR: this type has been superceded by `LazyLock` in the standard library
+    static LAZY_BAR: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| "bar".to_uppercase());
+    //~^ ERROR: this type has been superceded by `LazyLock` in the standard library
+    static mut LAZY_BAZ: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| "baz".to_uppercase());
+    //~^ ERROR: this type has been superceded by `LazyLock` in the standard library
+
+    fn calling_replaceable_fns() {
+        let _ = std::sync::LazyLock::force(&LAZY_FOO);
+        let _ = std::sync::LazyLock::force(&LAZY_BAR);
+        unsafe {
+            let _ = std::sync::LazyLock::force(&LAZY_BAZ);
+        }
+    }
+}
+
+#[clippy::msrv = "1.79"]
+mod msrv_not_meet {
+    use lazy_static::lazy_static;
+    use once_cell::sync::Lazy;
+
+    static LAZY_FOO: Lazy<String> = Lazy::new(|| "foo".to_uppercase());
+
+    lazy_static! {
+        static ref LAZY_BAZ: f64 = 12.159 * 548;
+    }
+}
+
+mod external_macros {
+    once_cell::external!();
+    lazy_static::external!();
+}
diff --git a/tests/ui/non_std_lazy_static/non_std_lazy_static_fixable.rs b/tests/ui/non_std_lazy_static/non_std_lazy_static_fixable.rs
new file mode 100644
index 00000000000..90bc428137c
--- /dev/null
+++ b/tests/ui/non_std_lazy_static/non_std_lazy_static_fixable.rs
@@ -0,0 +1,72 @@
+//@aux-build:once_cell.rs
+//@aux-build:lazy_static.rs
+
+#![warn(clippy::non_std_lazy_statics)]
+#![allow(static_mut_refs)]
+
+use once_cell::sync::Lazy;
+
+fn main() {}
+
+static LAZY_FOO: Lazy<String> = Lazy::new(|| "foo".to_uppercase());
+//~^ ERROR: this type has been superceded by `LazyLock` in the standard library
+static LAZY_BAR: Lazy<String> = Lazy::new(|| {
+    //~^ ERROR: this type has been superceded by `LazyLock` in the standard library
+    let x = "bar";
+    x.to_uppercase()
+});
+static LAZY_BAZ: Lazy<String> = { Lazy::new(|| "baz".to_uppercase()) };
+//~^ ERROR: this type has been superceded by `LazyLock` in the standard library
+static LAZY_QUX: Lazy<String> = {
+    //~^ ERROR: this type has been superceded by `LazyLock` in the standard library
+    if "qux".len() == 3 {
+        Lazy::new(|| "qux".to_uppercase())
+    } else if "qux".is_ascii() {
+        Lazy::new(|| "qux".to_lowercase())
+    } else {
+        Lazy::new(|| "qux".to_string())
+    }
+};
+
+fn non_static() {
+    let _: Lazy<i32> = Lazy::new(|| 1);
+    let _: Lazy<String> = Lazy::new(|| String::from("hello"));
+    #[allow(clippy::declare_interior_mutable_const)]
+    const DONT_DO_THIS: Lazy<i32> = Lazy::new(|| 1);
+}
+
+mod once_cell_lazy_with_fns {
+    use once_cell::sync::Lazy;
+
+    static LAZY_FOO: Lazy<String> = Lazy::new(|| "foo".to_uppercase());
+    //~^ ERROR: this type has been superceded by `LazyLock` in the standard library
+    static LAZY_BAR: Lazy<String> = Lazy::new(|| "bar".to_uppercase());
+    //~^ ERROR: this type has been superceded by `LazyLock` in the standard library
+    static mut LAZY_BAZ: Lazy<String> = Lazy::new(|| "baz".to_uppercase());
+    //~^ ERROR: this type has been superceded by `LazyLock` in the standard library
+
+    fn calling_replaceable_fns() {
+        let _ = Lazy::force(&LAZY_FOO);
+        let _ = Lazy::force(&LAZY_BAR);
+        unsafe {
+            let _ = Lazy::force(&LAZY_BAZ);
+        }
+    }
+}
+
+#[clippy::msrv = "1.79"]
+mod msrv_not_meet {
+    use lazy_static::lazy_static;
+    use once_cell::sync::Lazy;
+
+    static LAZY_FOO: Lazy<String> = Lazy::new(|| "foo".to_uppercase());
+
+    lazy_static! {
+        static ref LAZY_BAZ: f64 = 12.159 * 548;
+    }
+}
+
+mod external_macros {
+    once_cell::external!();
+    lazy_static::external!();
+}
diff --git a/tests/ui/non_std_lazy_static/non_std_lazy_static_fixable.stderr b/tests/ui/non_std_lazy_static/non_std_lazy_static_fixable.stderr
new file mode 100644
index 00000000000..f956f4b8d52
--- /dev/null
+++ b/tests/ui/non_std_lazy_static/non_std_lazy_static_fixable.stderr
@@ -0,0 +1,100 @@
+error: this type has been superceded by `LazyLock` in the standard library
+  --> tests/ui/non_std_lazy_static/non_std_lazy_static_fixable.rs:11:18
+   |
+LL | static LAZY_FOO: Lazy<String> = Lazy::new(|| "foo".to_uppercase());
+   |                  ^^^^
+   |
+   = note: `-D clippy::non-std-lazy-statics` implied by `-D warnings`
+   = help: to override `-D warnings` add `#[allow(clippy::non_std_lazy_statics)]`
+help: use `std::sync::LazyLock` instead
+   |
+LL | static LAZY_FOO: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| "foo".to_uppercase());
+   |                  ~~~~~~~~~~~~~~~~~~~           ~~~~~~~~~~~~~~~~~~~~~~~~
+
+error: this type has been superceded by `LazyLock` in the standard library
+  --> tests/ui/non_std_lazy_static/non_std_lazy_static_fixable.rs:13:18
+   |
+LL | static LAZY_BAR: Lazy<String> = Lazy::new(|| {
+   |                  ^^^^
+   |
+help: use `std::sync::LazyLock` instead
+   |
+LL | static LAZY_BAR: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| {
+   |                  ~~~~~~~~~~~~~~~~~~~           ~~~~~~~~~~~~~~~~~~~~~~~~
+
+error: this type has been superceded by `LazyLock` in the standard library
+  --> tests/ui/non_std_lazy_static/non_std_lazy_static_fixable.rs:18:18
+   |
+LL | static LAZY_BAZ: Lazy<String> = { Lazy::new(|| "baz".to_uppercase()) };
+   |                  ^^^^
+   |
+help: use `std::sync::LazyLock` instead
+   |
+LL | static LAZY_BAZ: std::sync::LazyLock<String> = { std::sync::LazyLock::new(|| "baz".to_uppercase()) };
+   |                  ~~~~~~~~~~~~~~~~~~~             ~~~~~~~~~~~~~~~~~~~~~~~~
+
+error: this type has been superceded by `LazyLock` in the standard library
+  --> tests/ui/non_std_lazy_static/non_std_lazy_static_fixable.rs:20:18
+   |
+LL | static LAZY_QUX: Lazy<String> = {
+   |                  ^^^^
+   |
+help: use `std::sync::LazyLock` instead
+   |
+LL ~ static LAZY_QUX: std::sync::LazyLock<String> = {
+LL |
+LL |     if "qux".len() == 3 {
+LL ~         std::sync::LazyLock::new(|| "qux".to_uppercase())
+LL |     } else if "qux".is_ascii() {
+LL ~         std::sync::LazyLock::new(|| "qux".to_lowercase())
+LL |     } else {
+LL ~         std::sync::LazyLock::new(|| "qux".to_string())
+   |
+
+error: this type has been superceded by `LazyLock` in the standard library
+  --> tests/ui/non_std_lazy_static/non_std_lazy_static_fixable.rs:41:22
+   |
+LL |     static LAZY_FOO: Lazy<String> = Lazy::new(|| "foo".to_uppercase());
+   |                      ^^^^
+   |
+help: use `std::sync::LazyLock` instead
+   |
+LL ~     static LAZY_FOO: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| "foo".to_uppercase());
+LL |
+...
+LL |     fn calling_replaceable_fns() {
+LL ~         let _ = std::sync::LazyLock::force(&LAZY_FOO);
+   |
+
+error: this type has been superceded by `LazyLock` in the standard library
+  --> tests/ui/non_std_lazy_static/non_std_lazy_static_fixable.rs:43:22
+   |
+LL |     static LAZY_BAR: Lazy<String> = Lazy::new(|| "bar".to_uppercase());
+   |                      ^^^^
+   |
+help: use `std::sync::LazyLock` instead
+   |
+LL ~     static LAZY_BAR: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| "bar".to_uppercase());
+LL |
+...
+LL |         let _ = Lazy::force(&LAZY_FOO);
+LL ~         let _ = std::sync::LazyLock::force(&LAZY_BAR);
+   |
+
+error: this type has been superceded by `LazyLock` in the standard library
+  --> tests/ui/non_std_lazy_static/non_std_lazy_static_fixable.rs:45:26
+   |
+LL |     static mut LAZY_BAZ: Lazy<String> = Lazy::new(|| "baz".to_uppercase());
+   |                          ^^^^
+   |
+help: use `std::sync::LazyLock` instead
+   |
+LL ~     static mut LAZY_BAZ: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| "baz".to_uppercase());
+LL |
+...
+LL |         unsafe {
+LL ~             let _ = std::sync::LazyLock::force(&LAZY_BAZ);
+   |
+
+error: aborting due to 7 previous errors
+
diff --git a/tests/ui/non_std_lazy_static/non_std_lazy_static_no_std.rs b/tests/ui/non_std_lazy_static/non_std_lazy_static_no_std.rs
new file mode 100644
index 00000000000..6208612c698
--- /dev/null
+++ b/tests/ui/non_std_lazy_static/non_std_lazy_static_no_std.rs
@@ -0,0 +1,20 @@
+//@aux-build:once_cell.rs
+//@aux-build:lazy_static.rs
+
+#![warn(clippy::non_std_lazy_statics)]
+#![no_std]
+
+use lazy_static::lazy_static;
+use once_cell::sync::Lazy;
+
+fn main() {}
+
+static LAZY_FOO: Lazy<usize> = Lazy::new(|| 42);
+static LAZY_BAR: Lazy<i32> = Lazy::new(|| {
+    let x: i32 = 0;
+    x.saturating_add(100)
+});
+
+lazy_static! {
+    static ref LAZY_BAZ: f64 = 12.159 * 548;
+}
diff --git a/tests/ui/non_std_lazy_static/non_std_lazy_static_other_once_cell.rs b/tests/ui/non_std_lazy_static/non_std_lazy_static_other_once_cell.rs
new file mode 100644
index 00000000000..8701a4b7729
--- /dev/null
+++ b/tests/ui/non_std_lazy_static/non_std_lazy_static_other_once_cell.rs
@@ -0,0 +1,12 @@
+//@aux-build:once_cell.rs
+
+#![warn(clippy::non_std_lazy_statics)]
+
+// Should not error, since we used a type besides `sync::Lazy`
+fn use_once_cell_race(x: once_cell::race::OnceBox<String>) {
+    let _foo = x.get();
+}
+
+use once_cell::sync::Lazy;
+
+static LAZY_BAZ: Lazy<String> = Lazy::new(|| "baz".to_uppercase());
diff --git a/tests/ui/non_std_lazy_static/non_std_lazy_static_unfixable.rs b/tests/ui/non_std_lazy_static/non_std_lazy_static_unfixable.rs
new file mode 100644
index 00000000000..34f8dd1ccb2
--- /dev/null
+++ b/tests/ui/non_std_lazy_static/non_std_lazy_static_unfixable.rs
@@ -0,0 +1,43 @@
+//@aux-build:once_cell.rs
+//@aux-build:lazy_static.rs
+//@no-rustfix
+
+#![warn(clippy::non_std_lazy_statics)]
+#![allow(static_mut_refs)]
+
+mod once_cell_lazy {
+    use once_cell::sync::Lazy;
+
+    static LAZY_FOO: Lazy<String> = Lazy::new(|| "foo".to_uppercase());
+    //~^ ERROR: this type has been superceded by `LazyLock` in the standard library
+    static mut LAZY_BAR: Lazy<String> = Lazy::new(|| "bar".to_uppercase());
+    //~^ ERROR: this type has been superceded by `LazyLock` in the standard library
+    static mut LAZY_BAZ: Lazy<String> = Lazy::new(|| "baz".to_uppercase());
+    //~^ ERROR: this type has been superceded by `LazyLock` in the standard library
+
+    fn calling_irreplaceable_fns() {
+        let _ = Lazy::get(&LAZY_FOO);
+
+        unsafe {
+            let _ = Lazy::get_mut(&mut LAZY_BAR);
+            let _ = Lazy::force_mut(&mut LAZY_BAZ);
+        }
+    }
+}
+
+mod lazy_static_lazy_static {
+    use lazy_static::lazy_static;
+
+    lazy_static! {
+        static ref LAZY_FOO: String = "foo".to_uppercase();
+    }
+    //~^^^ ERROR: this macro has been superceded by `std::sync::LazyLock`
+    lazy_static! {
+        static ref LAZY_BAR: String = "bar".to_uppercase();
+        static ref LAZY_BAZ: String = "baz".to_uppercase();
+    }
+    //~^^^^ ERROR: this macro has been superceded by `std::sync::LazyLock`
+    //~| ERROR: this macro has been superceded by `std::sync::LazyLock`
+}
+
+fn main() {}
diff --git a/tests/ui/non_std_lazy_static/non_std_lazy_static_unfixable.stderr b/tests/ui/non_std_lazy_static/non_std_lazy_static_unfixable.stderr
new file mode 100644
index 00000000000..66dc435f982
--- /dev/null
+++ b/tests/ui/non_std_lazy_static/non_std_lazy_static_unfixable.stderr
@@ -0,0 +1,66 @@
+error: this macro has been superceded by `std::sync::LazyLock`
+  --> tests/ui/non_std_lazy_static/non_std_lazy_static_unfixable.rs:31:5
+   |
+LL | /     lazy_static! {
+LL | |         static ref LAZY_FOO: String = "foo".to_uppercase();
+LL | |     }
+   | |_____^
+   |
+   = note: `-D clippy::non-std-lazy-statics` implied by `-D warnings`
+   = help: to override `-D warnings` add `#[allow(clippy::non_std_lazy_statics)]`
+
+error: this macro has been superceded by `std::sync::LazyLock`
+  --> tests/ui/non_std_lazy_static/non_std_lazy_static_unfixable.rs:35:5
+   |
+LL | /     lazy_static! {
+LL | |         static ref LAZY_BAR: String = "bar".to_uppercase();
+LL | |         static ref LAZY_BAZ: String = "baz".to_uppercase();
+LL | |     }
+   | |_____^
+
+error: this macro has been superceded by `std::sync::LazyLock`
+  --> tests/ui/non_std_lazy_static/non_std_lazy_static_unfixable.rs:35:5
+   |
+LL | /     lazy_static! {
+LL | |         static ref LAZY_BAR: String = "bar".to_uppercase();
+LL | |         static ref LAZY_BAZ: String = "baz".to_uppercase();
+LL | |     }
+   | |_____^
+   |
+   = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
+
+error: this type has been superceded by `LazyLock` in the standard library
+  --> tests/ui/non_std_lazy_static/non_std_lazy_static_unfixable.rs:11:22
+   |
+LL |     static LAZY_FOO: Lazy<String> = Lazy::new(|| "foo".to_uppercase());
+   |                      ^^^^
+   |
+help: use `std::sync::LazyLock` instead
+   |
+LL |     static LAZY_FOO: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| "foo".to_uppercase());
+   |                      ~~~~~~~~~~~~~~~~~~~           ~~~~~~~~~~~~~~~~~~~~~~~~
+
+error: this type has been superceded by `LazyLock` in the standard library
+  --> tests/ui/non_std_lazy_static/non_std_lazy_static_unfixable.rs:13:26
+   |
+LL |     static mut LAZY_BAR: Lazy<String> = Lazy::new(|| "bar".to_uppercase());
+   |                          ^^^^
+   |
+help: use `std::sync::LazyLock` instead
+   |
+LL |     static mut LAZY_BAR: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| "bar".to_uppercase());
+   |                          ~~~~~~~~~~~~~~~~~~~           ~~~~~~~~~~~~~~~~~~~~~~~~
+
+error: this type has been superceded by `LazyLock` in the standard library
+  --> tests/ui/non_std_lazy_static/non_std_lazy_static_unfixable.rs:15:26
+   |
+LL |     static mut LAZY_BAZ: Lazy<String> = Lazy::new(|| "baz".to_uppercase());
+   |                          ^^^^
+   |
+help: use `std::sync::LazyLock` instead
+   |
+LL |     static mut LAZY_BAZ: std::sync::LazyLock<String> = std::sync::LazyLock::new(|| "baz".to_uppercase());
+   |                          ~~~~~~~~~~~~~~~~~~~           ~~~~~~~~~~~~~~~~~~~~~~~~
+
+error: aborting due to 6 previous errors
+