about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-06-07 22:54:02 +0000
committerbors <bors@rust-lang.org>2024-06-07 22:54:02 +0000
commit0ea88b90d8fbcb140314db4e689f8c78cc428907 (patch)
tree3f4b1a9480400069d29bb64a846f3bc2dcf3f8b3
parentd553ebef573712d66470fa85e08d97a67375c243 (diff)
parent65af5d7b22ecd29a5aad8b6849659c9eaf62a96a (diff)
downloadrust-0ea88b90d8fbcb140314db4e689f8c78cc428907.tar.gz
rust-0ea88b90d8fbcb140314db4e689f8c78cc428907.zip
Auto merge of #12900 - Alexendoo:no-lazy-static, r=llogiq
Remove `lazy_static` mention

I planned to replace any mention with `LazyLock` but I think `thread_local` is more appropriate here - `const`s that aren't `Sync` wouldn't be able to go in a `lazy_static`/`static LazyLock` either

Also removed a test file that was mostly commented out so wasn't testing anything

changelog: none
-rw-r--r--clippy_lints/src/non_copy_const.rs32
-rw-r--r--tests/ui/crashes/ice-9445.rs2
-rw-r--r--tests/ui/crashes/ice-9445.stderr7
-rw-r--r--tests/ui/crashes/mut_mut_macro.rs33
-rw-r--r--tests/ui/declare_interior_mutable_const/enums.stderr43
-rw-r--r--tests/ui/declare_interior_mutable_const/others.stderr27
-rw-r--r--tests/ui/declare_interior_mutable_const/traits.stderr26
7 files changed, 69 insertions, 101 deletions
diff --git a/clippy_lints/src/non_copy_const.rs b/clippy_lints/src/non_copy_const.rs
index 76d9cee18aa..464c658b2b4 100644
--- a/clippy_lints/src/non_copy_const.rs
+++ b/clippy_lints/src/non_copy_const.rs
@@ -7,7 +7,7 @@ use std::ptr;
 use clippy_utils::diagnostics::span_lint_and_then;
 use clippy_utils::in_constant;
 use clippy_utils::macros::macro_backtrace;
-use clippy_utils::ty::InteriorMut;
+use clippy_utils::ty::{implements_trait, InteriorMut};
 use rustc_hir::def::{DefKind, Res};
 use rustc_hir::def_id::DefId;
 use rustc_hir::{
@@ -18,7 +18,7 @@ use rustc_middle::mir::interpret::{ErrorHandled, EvalToValTreeResult, GlobalId};
 use rustc_middle::ty::adjustment::Adjust;
 use rustc_middle::ty::{self, Ty, TyCtxt};
 use rustc_session::impl_lint_pass;
-use rustc_span::{sym, InnerSpan, Span, DUMMY_SP};
+use rustc_span::{sym, Span, DUMMY_SP};
 use rustc_target::abi::VariantIdx;
 
 // FIXME: this is a correctness problem but there's no suitable
@@ -127,19 +127,19 @@ declare_clippy_lint! {
 }
 
 #[derive(Copy, Clone)]
-enum Source {
-    Item { item: Span },
+enum Source<'tcx> {
+    Item { item: Span, ty: Ty<'tcx> },
     Assoc { item: Span },
     Expr { expr: Span },
 }
 
-impl Source {
+impl Source<'_> {
     #[must_use]
     fn lint(&self) -> (&'static Lint, &'static str, Span) {
         match self {
-            Self::Item { item } | Self::Assoc { item, .. } => (
+            Self::Item { item, .. } | Self::Assoc { item, .. } => (
                 DECLARE_INTERIOR_MUTABLE_CONST,
-                "a `const` item should never be interior mutable",
+                "a `const` item should not be interior mutable",
                 *item,
             ),
             Self::Expr { expr } => (
@@ -151,16 +151,24 @@ impl Source {
     }
 }
 
-fn lint(cx: &LateContext<'_>, source: Source) {
+fn lint<'tcx>(cx: &LateContext<'tcx>, source: Source<'tcx>) {
     let (lint, msg, span) = source.lint();
     span_lint_and_then(cx, lint, span, msg, |diag| {
         if span.from_expansion() {
             return; // Don't give suggestions into macros.
         }
         match source {
-            Source::Item { .. } => {
-                let const_kw_span = span.from_inner(InnerSpan::new(0, 5));
-                diag.span_label(const_kw_span, "make this a static item (maybe with lazy_static)");
+            Source::Item { ty, .. } => {
+                let Some(sync_trait) = cx.tcx.lang_items().sync_trait() else {
+                    return;
+                };
+                if implements_trait(cx, ty, sync_trait, &[]) {
+                    diag.help("consider making this a static item");
+                } else {
+                    diag.help(
+                        "consider making this `Sync` so that it can go in a static item or using a `thread_local`",
+                    );
+                }
             },
             Source::Assoc { .. } => (),
             Source::Expr { .. } => {
@@ -311,7 +319,7 @@ impl<'tcx> LateLintPass<'tcx> for NonCopyConst<'tcx> {
                 && self.interior_mut.is_interior_mut_ty(cx, ty)
                 && Self::is_value_unfrozen_poly(cx, body_id, ty)
             {
-                lint(cx, Source::Item { item: it.span });
+                lint(cx, Source::Item { item: it.span, ty });
             }
         }
     }
diff --git a/tests/ui/crashes/ice-9445.rs b/tests/ui/crashes/ice-9445.rs
index b6afbd33c79..c67b22f6f8c 100644
--- a/tests/ui/crashes/ice-9445.rs
+++ b/tests/ui/crashes/ice-9445.rs
@@ -1,5 +1,3 @@
 const UNINIT: core::mem::MaybeUninit<core::cell::Cell<&'static ()>> = core::mem::MaybeUninit::uninit();
-//~^ ERROR: a `const` item should never be interior mutable
-//~| NOTE: `-D clippy::declare-interior-mutable-const` implied by `-D warnings`
 
 fn main() {}
diff --git a/tests/ui/crashes/ice-9445.stderr b/tests/ui/crashes/ice-9445.stderr
index d6957e9549d..76689cd6f5c 100644
--- a/tests/ui/crashes/ice-9445.stderr
+++ b/tests/ui/crashes/ice-9445.stderr
@@ -1,11 +1,10 @@
-error: a `const` item should never be interior mutable
+error: a `const` item should not be interior mutable
   --> tests/ui/crashes/ice-9445.rs:1:1
    |
 LL | const UNINIT: core::mem::MaybeUninit<core::cell::Cell<&'static ()>> = core::mem::MaybeUninit::uninit();
-   | -----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-   | |
-   | make this a static item (maybe with lazy_static)
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
+   = help: consider making this `Sync` so that it can go in a static item or using a `thread_local`
    = note: `-D clippy::declare-interior-mutable-const` implied by `-D warnings`
    = help: to override `-D warnings` add `#[allow(clippy::declare_interior_mutable_const)]`
 
diff --git a/tests/ui/crashes/mut_mut_macro.rs b/tests/ui/crashes/mut_mut_macro.rs
deleted file mode 100644
index 92821b6ecbb..00000000000
--- a/tests/ui/crashes/mut_mut_macro.rs
+++ /dev/null
@@ -1,33 +0,0 @@
-#![deny(clippy::mut_mut, clippy::zero_ptr)]
-#![allow(dead_code)]
-
-// FIXME: compiletest + extern crates doesn't work together. To make this test work, it would need
-// the following three lines and the lazy_static crate.
-//
-//     #[macro_use]
-//     extern crate lazy_static;
-//     use std::collections::HashMap;
-
-/// ensure that we don't suggest `is_null` inside constants
-/// FIXME: once const fn is stable, suggest these functions again in constants
-
-const BAA: *const i32 = 0 as *const i32;
-static mut BAR: *const i32 = BAA;
-static mut FOO: *const i32 = 0 as *const i32;
-
-#[allow(unused_variables, unused_mut)]
-fn main() {
-    /*
-    lazy_static! {
-        static ref MUT_MAP : HashMap<usize, &'static str> = {
-            let mut m = HashMap::new();
-            m.insert(0, "zero");
-            m
-        };
-        static ref MUT_COUNT : usize = MUT_MAP.len();
-    }
-    assert_eq!(*MUT_COUNT, 1);
-    */
-    // FIXME: don't lint in array length, requires `check_body`
-    //let _ = [""; (42.0 < f32::NAN) as usize];
-}
diff --git a/tests/ui/declare_interior_mutable_const/enums.stderr b/tests/ui/declare_interior_mutable_const/enums.stderr
index 6c0dce6b5ea..22329172c3a 100644
--- a/tests/ui/declare_interior_mutable_const/enums.stderr
+++ b/tests/ui/declare_interior_mutable_const/enums.stderr
@@ -1,87 +1,84 @@
-error: a `const` item should never be interior mutable
+error: a `const` item should not be interior mutable
   --> tests/ui/declare_interior_mutable_const/enums.rs:12:1
    |
 LL | const UNFROZEN_VARIANT: OptionalCell = OptionalCell::Unfrozen(Cell::new(true));
-   | -----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-   | |
-   | make this a static item (maybe with lazy_static)
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
+   = help: consider making this `Sync` so that it can go in a static item or using a `thread_local`
    = note: `-D clippy::declare-interior-mutable-const` implied by `-D warnings`
    = help: to override `-D warnings` add `#[allow(clippy::declare_interior_mutable_const)]`
 
-error: a `const` item should never be interior mutable
+error: a `const` item should not be interior mutable
   --> tests/ui/declare_interior_mutable_const/enums.rs:23:1
    |
 LL | const UNFROZEN_VARIANT_FROM_FN: OptionalCell = unfrozen_variant();
-   | -----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-   | |
-   | make this a static item (maybe with lazy_static)
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |
+   = help: consider making this `Sync` so that it can go in a static item or using a `thread_local`
 
-error: a `const` item should never be interior mutable
+error: a `const` item should not be interior mutable
   --> tests/ui/declare_interior_mutable_const/enums.rs:45:1
    |
-LL |   const NESTED_UNFROZEN_VARIANT: NestedOutermost = NestedOutermost {
-   |   ^----
-   |   |
-   |  _make this a static item (maybe with lazy_static)
-   | |
+LL | / const NESTED_UNFROZEN_VARIANT: NestedOutermost = NestedOutermost {
 LL | |
 LL | |     outer: NestedOuter::NestedInner(NestedInner {
 LL | |         inner: NestedInnermost::Unfrozen(AtomicUsize::new(2)),
 LL | |     }),
 LL | | };
    | |__^
+   |
+   = help: consider making this a static item
 
-error: a `const` item should never be interior mutable
+error: a `const` item should not be interior mutable
   --> tests/ui/declare_interior_mutable_const/enums.rs:60:5
    |
 LL |     const TO_BE_UNFROZEN_VARIANT: OptionalCell;
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
-error: a `const` item should never be interior mutable
+error: a `const` item should not be interior mutable
   --> tests/ui/declare_interior_mutable_const/enums.rs:61:5
    |
 LL |     const TO_BE_FROZEN_VARIANT: OptionalCell;
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
-error: a `const` item should never be interior mutable
+error: a `const` item should not be interior mutable
   --> tests/ui/declare_interior_mutable_const/enums.rs:64:5
    |
 LL |     const DEFAULTED_ON_UNFROZEN_VARIANT: OptionalCell = OptionalCell::Unfrozen(Cell::new(false));
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
-error: a `const` item should never be interior mutable
+error: a `const` item should not be interior mutable
   --> tests/ui/declare_interior_mutable_const/enums.rs:90:5
    |
 LL |     const TO_BE_UNFROZEN_VARIANT: Option<Self::ToBeUnfrozen> = Some(Self::ToBeUnfrozen::new(4));
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
-error: a `const` item should never be interior mutable
+error: a `const` item should not be interior mutable
   --> tests/ui/declare_interior_mutable_const/enums.rs:102:5
    |
 LL |     const UNFROZEN_VARIANT: BothOfCellAndGeneric<T> = BothOfCellAndGeneric::Unfrozen(Cell::new(std::ptr::null()));
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
-error: a `const` item should never be interior mutable
+error: a `const` item should not be interior mutable
   --> tests/ui/declare_interior_mutable_const/enums.rs:105:5
    |
 LL |     const GENERIC_VARIANT: BothOfCellAndGeneric<T> = BothOfCellAndGeneric::Generic(std::ptr::null());
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
-error: a `const` item should never be interior mutable
+error: a `const` item should not be interior mutable
   --> tests/ui/declare_interior_mutable_const/enums.rs:111:5
    |
 LL |     const NO_ENUM: Cell<*const T> = Cell::new(std::ptr::null());
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
-error: a `const` item should never be interior mutable
+error: a `const` item should not be interior mutable
   --> tests/ui/declare_interior_mutable_const/enums.rs:118:5
    |
 LL | /     const UNFROZEN_VARIANT: BothOfCellAndGeneric<Self::AssocType> =
 LL | |         BothOfCellAndGeneric::Unfrozen(Cell::new(std::ptr::null()));
    | |____________________________________________________________________^
 
-error: a `const` item should never be interior mutable
+error: a `const` item should not be interior mutable
   --> tests/ui/declare_interior_mutable_const/enums.rs:120:5
    |
 LL |     const GENERIC_VARIANT: BothOfCellAndGeneric<Self::AssocType> = BothOfCellAndGeneric::Generic(std::ptr::null());
diff --git a/tests/ui/declare_interior_mutable_const/others.stderr b/tests/ui/declare_interior_mutable_const/others.stderr
index 9dba0c95221..1f2b9561ce5 100644
--- a/tests/ui/declare_interior_mutable_const/others.stderr
+++ b/tests/ui/declare_interior_mutable_const/others.stderr
@@ -1,31 +1,30 @@
-error: a `const` item should never be interior mutable
+error: a `const` item should not be interior mutable
   --> tests/ui/declare_interior_mutable_const/others.rs:9:1
    |
 LL | const ATOMIC: AtomicUsize = AtomicUsize::new(5);
-   | -----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-   | |
-   | make this a static item (maybe with lazy_static)
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
+   = help: consider making this a static item
    = note: `-D clippy::declare-interior-mutable-const` implied by `-D warnings`
    = help: to override `-D warnings` add `#[allow(clippy::declare_interior_mutable_const)]`
 
-error: a `const` item should never be interior mutable
+error: a `const` item should not be interior mutable
   --> tests/ui/declare_interior_mutable_const/others.rs:10:1
    |
 LL | const CELL: Cell<usize> = Cell::new(6);
-   | -----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-   | |
-   | make this a static item (maybe with lazy_static)
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |
+   = help: consider making this `Sync` so that it can go in a static item or using a `thread_local`
 
-error: a `const` item should never be interior mutable
+error: a `const` item should not be interior mutable
   --> tests/ui/declare_interior_mutable_const/others.rs:11:1
    |
 LL | const ATOMIC_TUPLE: ([AtomicUsize; 1], Vec<AtomicUsize>, u8) = ([ATOMIC], Vec::new(), 7);
-   | -----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-   | |
-   | make this a static item (maybe with lazy_static)
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |
+   = help: consider making this a static item
 
-error: a `const` item should never be interior mutable
+error: a `const` item should not be interior mutable
   --> tests/ui/declare_interior_mutable_const/others.rs:16:9
    |
 LL |         const $name: $ty = $e;
@@ -36,7 +35,7 @@ LL | declare_const!(_ONCE: Once = Once::new());
    |
    = note: this error originates in the macro `declare_const` (in Nightly builds, run with -Z macro-backtrace for more info)
 
-error: a `const` item should never be interior mutable
+error: a `const` item should not be interior mutable
   --> tests/ui/declare_interior_mutable_const/others.rs:44:13
    |
 LL |             const _BAZ: Cell<usize> = Cell::new(0);
diff --git a/tests/ui/declare_interior_mutable_const/traits.stderr b/tests/ui/declare_interior_mutable_const/traits.stderr
index 1d1e9e2002f..4a793d985e5 100644
--- a/tests/ui/declare_interior_mutable_const/traits.stderr
+++ b/tests/ui/declare_interior_mutable_const/traits.stderr
@@ -1,4 +1,4 @@
-error: a `const` item should never be interior mutable
+error: a `const` item should not be interior mutable
   --> tests/ui/declare_interior_mutable_const/traits.rs:16:5
    |
 LL |     const ATOMIC: AtomicUsize;
@@ -7,7 +7,7 @@ LL |     const ATOMIC: AtomicUsize;
    = note: `-D clippy::declare-interior-mutable-const` implied by `-D warnings`
    = help: to override `-D warnings` add `#[allow(clippy::declare_interior_mutable_const)]`
 
-error: a `const` item should never be interior mutable
+error: a `const` item should not be interior mutable
   --> tests/ui/declare_interior_mutable_const/traits.rs:9:9
    |
 LL |         const $name: $ty = $e;
@@ -18,67 +18,67 @@ LL |     declare_const!(ANOTHER_ATOMIC: AtomicUsize = Self::ATOMIC);
    |
    = note: this error originates in the macro `declare_const` (in Nightly builds, run with -Z macro-backtrace for more info)
 
-error: a `const` item should never be interior mutable
+error: a `const` item should not be interior mutable
   --> tests/ui/declare_interior_mutable_const/traits.rs:44:5
    |
 LL |     const TO_BE_CONCRETE: AtomicUsize = AtomicUsize::new(11);
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
-error: a `const` item should never be interior mutable
+error: a `const` item should not be interior mutable
   --> tests/ui/declare_interior_mutable_const/traits.rs:69:5
    |
 LL |     const TO_BE_UNFROZEN: Self::ToBeUnfrozen = AtomicUsize::new(13);
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
-error: a `const` item should never be interior mutable
+error: a `const` item should not be interior mutable
   --> tests/ui/declare_interior_mutable_const/traits.rs:70:5
    |
 LL |     const WRAPPED_TO_BE_UNFROZEN: Wrapper<Self::ToBeUnfrozen> = Wrapper(AtomicUsize::new(14));
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
-error: a `const` item should never be interior mutable
+error: a `const` item should not be interior mutable
   --> tests/ui/declare_interior_mutable_const/traits.rs:89:5
    |
 LL |     const BOUNDED: T::ToBeBounded;
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
-error: a `const` item should never be interior mutable
+error: a `const` item should not be interior mutable
   --> tests/ui/declare_interior_mutable_const/traits.rs:117:5
    |
 LL |     const SELF: Self = AtomicUsize::new(17);
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
-error: a `const` item should never be interior mutable
+error: a `const` item should not be interior mutable
   --> tests/ui/declare_interior_mutable_const/traits.rs:118:5
    |
 LL |     const WRAPPED_SELF: Option<Self> = Some(AtomicUsize::new(21));
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
-error: a `const` item should never be interior mutable
+error: a `const` item should not be interior mutable
   --> tests/ui/declare_interior_mutable_const/traits.rs:124:5
    |
 LL |     const DIRECT: Cell<T>;
    |     ^^^^^^^^^^^^^^^^^^^^^^
 
-error: a `const` item should never be interior mutable
+error: a `const` item should not be interior mutable
   --> tests/ui/declare_interior_mutable_const/traits.rs:125:5
    |
 LL |     const INDIRECT: Cell<*const T>;
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
-error: a `const` item should never be interior mutable
+error: a `const` item should not be interior mutable
   --> tests/ui/declare_interior_mutable_const/traits.rs:129:5
    |
 LL |     const DIRECT: Cell<T> = Cell::new(T::DEFAULT);
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
-error: a `const` item should never be interior mutable
+error: a `const` item should not be interior mutable
   --> tests/ui/declare_interior_mutable_const/traits.rs:141:5
    |
 LL |     const ATOMIC: AtomicUsize = AtomicUsize::new(18);
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
-error: a `const` item should never be interior mutable
+error: a `const` item should not be interior mutable
   --> tests/ui/declare_interior_mutable_const/traits.rs:147:5
    |
 LL |     const BOUNDED_ASSOC_TYPE: T::ToBeBounded = AtomicUsize::new(19);