about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJason Newcomb <jsnewcomb@pm.me>2024-08-06 21:02:36 -0400
committerJason Newcomb <jsnewcomb@pm.me>2025-05-16 06:08:40 -0400
commit7c41ec73951429a98a6ae550093393905547da88 (patch)
tree881e07be1556e1b85b3d637d788bf60b87521c17
parent57782e0ff1e9833e121187f1c33b72bec6a8a61a (diff)
downloadrust-7c41ec73951429a98a6ae550093393905547da88.tar.gz
rust-7c41ec73951429a98a6ae550093393905547da88.zip
Reword `declare_interior_mutable_const` and
`borrow_interior_mutable_const` messages to be less assertive.
Update documentation for both lints.
-rw-r--r--clippy_lints/src/non_copy_const.rs112
-rw-r--r--tests/ui/borrow_interior_mutable_const/enums.stderr24
-rw-r--r--tests/ui/borrow_interior_mutable_const/others.stderr60
-rw-r--r--tests/ui/borrow_interior_mutable_const/projections.stderr16
-rw-r--r--tests/ui/borrow_interior_mutable_const/traits.stderr68
-rw-r--r--tests/ui/declare_interior_mutable_const/enums.stderr26
-rw-r--r--tests/ui/declare_interior_mutable_const/others.stderr20
-rw-r--r--tests/ui/declare_interior_mutable_const/traits.stderr20
8 files changed, 188 insertions, 158 deletions
diff --git a/clippy_lints/src/non_copy_const.rs b/clippy_lints/src/non_copy_const.rs
index f57ab0e6781..a27c6aa75e3 100644
--- a/clippy_lints/src/non_copy_const.rs
+++ b/clippy_lints/src/non_copy_const.rs
@@ -43,35 +43,36 @@ use std::collections::hash_map::Entry;
 
 declare_clippy_lint! {
     /// ### What it does
-    /// Checks for declaration of `const` items which is interior
-    /// mutable (e.g., contains a `Cell`, `Mutex`, `AtomicXxxx`, etc.).
+    /// Checks for the declaration of named constant which contain interior mutability.
     ///
     /// ### Why is this bad?
-    /// Consts are copied everywhere they are referenced, i.e.,
-    /// every time you refer to the const a fresh instance of the `Cell` or `Mutex`
-    /// or `AtomicXxxx` will be created, which defeats the whole purpose of using
-    /// these types in the first place.
+    /// Named constants are copied at every use site which means any change to their value
+    /// will be lost after the newly created value is dropped. e.g.
     ///
-    /// The `const` should better be replaced by a `static` item if a global
-    /// variable is wanted, or replaced by a `const fn` if a constructor is wanted.
+    /// ```rust
+    /// use core::sync::atomic::{AtomicUsize, Ordering};
+    /// const ATOMIC: AtomicUsize = AtomicUsize::new(0);
+    /// fn add_one() -> usize {
+    ///     // This will always return `0` since `ATOMIC` is copied before it's used.
+    ///     ATOMIC.fetch_add(1, Ordering::AcqRel)
+    /// }
+    /// ```
     ///
-    /// ### Known problems
-    /// A "non-constant" const item is a legacy way to supply an
-    /// initialized value to downstream `static` items (e.g., the
-    /// `std::sync::ONCE_INIT` constant). In this case the use of `const` is legit,
-    /// and this lint should be suppressed.
+    /// If shared modification of the value is desired, a `static` item is needed instead.
+    /// If that is not desired, a `const fn` constructor should be used to make it obvious
+    /// at the use site that a new value is created.
     ///
-    /// Even though the lint avoids triggering on a constant whose type has enums that have variants
-    /// with interior mutability, and its value uses non interior mutable variants (see
-    /// [#3962](https://github.com/rust-lang/rust-clippy/issues/3962) and
-    /// [#3825](https://github.com/rust-lang/rust-clippy/issues/3825) for examples);
-    /// it complains about associated constants without default values only based on its types;
-    /// which might not be preferable.
-    /// There're other enums plus associated constants cases that the lint cannot handle.
+    /// ### Known problems
+    /// Prior to `const fn` stabilization this was the only way to provide a value which
+    /// could initialize a `static` item (e.g. the `std::sync::ONCE_INIT` constant). In
+    /// this case the use of `const` is required and this lint should be suppressed.
     ///
-    /// Types that have underlying or potential interior mutability trigger the lint whether
-    /// the interior mutable field is used or not. See issue
-    /// [#5812](https://github.com/rust-lang/rust-clippy/issues/5812)
+    /// There also exists types which contain private fields with interior mutability, but
+    /// no way to both create a value as a constant and modify any mutable field using the
+    /// type's public interface (e.g. `bytes::Bytes`). As there is no reasonable way to
+    /// scan a crate's interface to see if this is the case, all such types will be linted.
+    /// If this happens use the `ignore-interior-mutability` configuration option to allow
+    /// the type.
     ///
     /// ### Example
     /// ```no_run
@@ -97,16 +98,42 @@ declare_clippy_lint! {
 
 declare_clippy_lint! {
     /// ### What it does
-    /// Checks if `const` items which is interior mutable (e.g.,
-    /// contains a `Cell`, `Mutex`, `AtomicXxxx`, etc.) has been borrowed directly.
+    /// Checks for a borrow of a named constant with interior mutability.
     ///
     /// ### Why is this bad?
-    /// Consts are copied everywhere they are referenced, i.e.,
-    /// every time you refer to the const a fresh instance of the `Cell` or `Mutex`
-    /// or `AtomicXxxx` will be created, which defeats the whole purpose of using
-    /// these types in the first place.
+    /// Named constants are copied at every use site which means any change to their value
+    /// will be lost after the newly created value is dropped. e.g.
+    ///
+    /// ```rust
+    /// use core::sync::atomic::{AtomicUsize, Ordering};
+    /// const ATOMIC: AtomicUsize = AtomicUsize::new(0);
+    /// fn add_one() -> usize {
+    ///     // This will always return `0` since `ATOMIC` is copied before it's borrowed
+    ///     // for use by `fetch_add`.
+    ///     ATOMIC.fetch_add(1, Ordering::AcqRel)
+    /// }
+    /// ```
     ///
-    /// The `const` value should be stored inside a `static` item.
+    /// ### Known problems
+    /// This lint does not, and cannot in general, determine if the borrow of the constant
+    /// is used in a way which causes a mutation. e.g.
+    ///
+    /// ```rust
+    /// use core::cell::Cell;
+    /// const CELL: Cell<usize> = Cell::new(0);
+    /// fn get_cell() -> Cell<usize> {
+    ///     // This is fine. It borrows a copy of `CELL`, but never mutates it through the
+    ///     // borrow.
+    ///     CELL.clone()
+    /// }
+    /// ```
+    ///
+    /// There also exists types which contain private fields with interior mutability, but
+    /// no way to both create a value as a constant and modify any mutable field using the
+    /// type's public interface (e.g. `bytes::Bytes`). As there is no reasonable way to
+    /// scan a crate's interface to see if this is the case, all such types will be linted.
+    /// If this happens use the `ignore-interior-mutability` configuration option to allow
+    /// the type.
     ///
     /// ### Example
     /// ```no_run
@@ -180,6 +207,7 @@ enum BorrowCause {
     Index,
     AutoDeref,
     AutoBorrow,
+    AutoDerefField,
 }
 impl BorrowCause {
     fn note(self) -> Option<&'static str> {
@@ -189,6 +217,9 @@ impl BorrowCause {
             Self::Index => Some("this index expression is a call to `Index::index`"),
             Self::AutoDeref => Some("there is a compiler inserted call to `Deref::deref` here"),
             Self::AutoBorrow => Some("there is a compiler inserted borrow here"),
+            Self::AutoDerefField => {
+                Some("there is a compiler inserted call to `Deref::deref` when accessing this field")
+            },
         }
     }
 }
@@ -208,6 +239,7 @@ impl<'tcx> BorrowSource<'tcx> {
             match parent.kind {
                 ExprKind::Unary(UnOp::Deref, _) => (parent, BorrowCause::Deref),
                 ExprKind::Index(..) => (parent, BorrowCause::Index),
+                ExprKind::Field(..) => (parent, BorrowCause::AutoDerefField),
                 _ => (expr, cause),
             }
         } else {
@@ -688,17 +720,15 @@ impl<'tcx> LateLintPass<'tcx> for NonCopyConst<'tcx> {
                 cx,
                 DECLARE_INTERIOR_MUTABLE_CONST,
                 ident.span,
-                "a `const` item should not be interior mutable",
+                "named constant with interior mutability",
                 |diag| {
                     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");
+                        diag.help("did you mean to make 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`",
-                        );
+                        diag.help("did you mean to make this a `thread_local!` item");
                     }
                 },
             );
@@ -732,7 +762,7 @@ impl<'tcx> LateLintPass<'tcx> for NonCopyConst<'tcx> {
                 cx,
                 DECLARE_INTERIOR_MUTABLE_CONST,
                 item.ident.span,
-                "a `const` item should not be interior mutable",
+                "named constant with interior mutability",
             );
         }
     }
@@ -784,7 +814,7 @@ impl<'tcx> LateLintPass<'tcx> for NonCopyConst<'tcx> {
                 cx,
                 DECLARE_INTERIOR_MUTABLE_CONST,
                 item.ident.span,
-                "a `const` item should not be interior mutable",
+                "named constant with interior mutability",
             );
         }
     }
@@ -819,12 +849,12 @@ impl<'tcx> LateLintPass<'tcx> for NonCopyConst<'tcx> {
                 cx,
                 BORROW_INTERIOR_MUTABLE_CONST,
                 borrow_src.expr.span,
-                "a `const` item with interior mutability should not be borrowed",
+                "borrow of a named constant with interior mutability",
                 |diag| {
-                    if let Some(msg) = borrow_src.cause.note() {
-                        diag.note(msg);
+                    if let Some(note) = borrow_src.cause.note() {
+                        diag.note(note);
                     }
-                    diag.help("assign this const to a local or static variable, and use the variable here");
+                    diag.help("this lint can be silenced by assigning the value to a local variable before borrowing");
                 },
             );
         }
diff --git a/tests/ui/borrow_interior_mutable_const/enums.stderr b/tests/ui/borrow_interior_mutable_const/enums.stderr
index 7a3cb7d6aa9..80c2deb4b3a 100644
--- a/tests/ui/borrow_interior_mutable_const/enums.stderr
+++ b/tests/ui/borrow_interior_mutable_const/enums.stderr
@@ -1,55 +1,55 @@
-error: a `const` item with interior mutability should not be borrowed
+error: borrow of a named constant with interior mutability
   --> tests/ui/borrow_interior_mutable_const/enums.rs:22:13
    |
 LL |     let _ = &UNFROZEN_VARIANT;
    |             ^^^^^^^^^^^^^^^^^
    |
-   = help: assign this const to a local or static variable, and use the variable here
+   = help: this lint can be silenced by assigning the value to a local variable before borrowing
 note: the lint level is defined here
   --> tests/ui/borrow_interior_mutable_const/enums.rs:3:9
    |
 LL | #![deny(clippy::borrow_interior_mutable_const)]
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
-error: a `const` item with interior mutability should not be borrowed
+error: borrow of a named constant with interior mutability
   --> tests/ui/borrow_interior_mutable_const/enums.rs:50:17
    |
 LL |         let _ = &<Self as AssocConsts>::TO_BE_UNFROZEN_VARIANT;
    |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
-   = help: assign this const to a local or static variable, and use the variable here
+   = help: this lint can be silenced by assigning the value to a local variable before borrowing
 
-error: a `const` item with interior mutability should not be borrowed
+error: borrow of a named constant with interior mutability
   --> tests/ui/borrow_interior_mutable_const/enums.rs:52:17
    |
 LL |         let _ = &Self::DEFAULTED_ON_UNFROZEN_VARIANT;
    |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
-   = help: assign this const to a local or static variable, and use the variable here
+   = help: this lint can be silenced by assigning the value to a local variable before borrowing
 
-error: a `const` item with interior mutability should not be borrowed
+error: borrow of a named constant with interior mutability
   --> tests/ui/borrow_interior_mutable_const/enums.rs:74:17
    |
 LL |         let _ = &<Self as AssocTypes>::TO_BE_UNFROZEN_VARIANT;
    |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
-   = help: assign this const to a local or static variable, and use the variable here
+   = help: this lint can be silenced by assigning the value to a local variable before borrowing
 
-error: a `const` item with interior mutability should not be borrowed
+error: borrow of a named constant with interior mutability
   --> tests/ui/borrow_interior_mutable_const/enums.rs:91:17
    |
 LL |         let _ = &Self::UNFROZEN_VARIANT;
    |                 ^^^^^^^^^^^^^^^^^^^^^^^
    |
-   = help: assign this const to a local or static variable, and use the variable here
+   = help: this lint can be silenced by assigning the value to a local variable before borrowing
 
-error: a `const` item with interior mutability should not be borrowed
+error: borrow of a named constant with interior mutability
   --> tests/ui/borrow_interior_mutable_const/enums.rs:99:13
    |
 LL |     let _ = &helper::WRAPPED_PRIVATE_UNFROZEN_VARIANT;
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
-   = help: assign this const to a local or static variable, and use the variable here
+   = help: this lint can be silenced by assigning the value to a local variable before borrowing
 
 error: aborting due to 6 previous errors
 
diff --git a/tests/ui/borrow_interior_mutable_const/others.stderr b/tests/ui/borrow_interior_mutable_const/others.stderr
index 6e887406dcd..67b9907f8e3 100644
--- a/tests/ui/borrow_interior_mutable_const/others.stderr
+++ b/tests/ui/borrow_interior_mutable_const/others.stderr
@@ -1,132 +1,132 @@
-error: a `const` item with interior mutability should not be borrowed
+error: borrow of a named constant with interior mutability
   --> tests/ui/borrow_interior_mutable_const/others.rs:65:5
    |
 LL |     ATOMIC.store(1, Ordering::SeqCst);
    |     ^^^^^^
    |
    = note: there is a compiler inserted borrow here
-   = help: assign this const to a local or static variable, and use the variable here
+   = help: this lint can be silenced by assigning the value to a local variable before borrowing
 note: the lint level is defined here
   --> tests/ui/borrow_interior_mutable_const/others.rs:1:9
    |
 LL | #![deny(clippy::borrow_interior_mutable_const)]
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
-error: a `const` item with interior mutability should not be borrowed
+error: borrow of a named constant with interior mutability
   --> tests/ui/borrow_interior_mutable_const/others.rs:66:16
    |
 LL |     assert_eq!(ATOMIC.load(Ordering::SeqCst), 5);
    |                ^^^^^^
    |
    = note: there is a compiler inserted borrow here
-   = help: assign this const to a local or static variable, and use the variable here
+   = help: this lint can be silenced by assigning the value to a local variable before borrowing
 
-error: a `const` item with interior mutability should not be borrowed
+error: borrow of a named constant with interior mutability
   --> tests/ui/borrow_interior_mutable_const/others.rs:69:21
    |
 LL |     let _once_ref = &ONCE_INIT;
    |                     ^^^^^^^^^^
    |
-   = help: assign this const to a local or static variable, and use the variable here
+   = help: this lint can be silenced by assigning the value to a local variable before borrowing
 
-error: a `const` item with interior mutability should not be borrowed
+error: borrow of a named constant with interior mutability
   --> tests/ui/borrow_interior_mutable_const/others.rs:70:24
    |
 LL |     let _once_ref_2 = &&ONCE_INIT;
    |                        ^^^^^^^^^^
    |
-   = help: assign this const to a local or static variable, and use the variable here
+   = help: this lint can be silenced by assigning the value to a local variable before borrowing
 
-error: a `const` item with interior mutability should not be borrowed
+error: borrow of a named constant with interior mutability
   --> tests/ui/borrow_interior_mutable_const/others.rs:71:26
    |
 LL |     let _once_ref_4 = &&&&ONCE_INIT;
    |                          ^^^^^^^^^^
    |
-   = help: assign this const to a local or static variable, and use the variable here
+   = help: this lint can be silenced by assigning the value to a local variable before borrowing
 
-error: a `const` item with interior mutability should not be borrowed
+error: borrow of a named constant with interior mutability
   --> tests/ui/borrow_interior_mutable_const/others.rs:72:21
    |
 LL |     let _once_mut = &mut ONCE_INIT;
    |                     ^^^^^^^^^^^^^^
    |
-   = help: assign this const to a local or static variable, and use the variable here
+   = help: this lint can be silenced by assigning the value to a local variable before borrowing
 
-error: a `const` item with interior mutability should not be borrowed
+error: borrow of a named constant with interior mutability
   --> tests/ui/borrow_interior_mutable_const/others.rs:83:13
    |
 LL |     let _ = &ATOMIC_TUPLE;
    |             ^^^^^^^^^^^^^
    |
-   = help: assign this const to a local or static variable, and use the variable here
+   = help: this lint can be silenced by assigning the value to a local variable before borrowing
 
-error: a `const` item with interior mutability should not be borrowed
+error: borrow of a named constant with interior mutability
   --> tests/ui/borrow_interior_mutable_const/others.rs:84:13
    |
 LL |     let _ = &ATOMIC_TUPLE.0;
    |             ^^^^^^^^^^^^^^^
    |
-   = help: assign this const to a local or static variable, and use the variable here
+   = help: this lint can be silenced by assigning the value to a local variable before borrowing
 
-error: a `const` item with interior mutability should not be borrowed
+error: borrow of a named constant with interior mutability
   --> tests/ui/borrow_interior_mutable_const/others.rs:85:18
    |
 LL |     let _ = &(&&&&ATOMIC_TUPLE).0;
    |                  ^^^^^^^^^^^^^
    |
-   = help: assign this const to a local or static variable, and use the variable here
+   = help: this lint can be silenced by assigning the value to a local variable before borrowing
 
-error: a `const` item with interior mutability should not be borrowed
+error: borrow of a named constant with interior mutability
   --> tests/ui/borrow_interior_mutable_const/others.rs:86:13
    |
 LL |     let _ = &ATOMIC_TUPLE.0[0];
    |             ^^^^^^^^^^^^^^^^^^
    |
-   = help: assign this const to a local or static variable, and use the variable here
+   = help: this lint can be silenced by assigning the value to a local variable before borrowing
 
-error: a `const` item with interior mutability should not be borrowed
+error: borrow of a named constant with interior mutability
   --> tests/ui/borrow_interior_mutable_const/others.rs:87:13
    |
 LL |     let _ = ATOMIC_TUPLE.0[0].load(Ordering::SeqCst);
    |             ^^^^^^^^^^^^^^^^^
    |
    = note: there is a compiler inserted borrow here
-   = help: assign this const to a local or static variable, and use the variable here
+   = help: this lint can be silenced by assigning the value to a local variable before borrowing
 
-error: a `const` item with interior mutability should not be borrowed
+error: borrow of a named constant with interior mutability
   --> tests/ui/borrow_interior_mutable_const/others.rs:89:17
    |
 LL |     let _ = (&&&&ATOMIC_TUPLE).0;
    |                 ^^^^^^^^^^^^^
    |
-   = help: assign this const to a local or static variable, and use the variable here
+   = help: this lint can be silenced by assigning the value to a local variable before borrowing
 
-error: a `const` item with interior mutability should not be borrowed
+error: borrow of a named constant with interior mutability
   --> tests/ui/borrow_interior_mutable_const/others.rs:90:17
    |
 LL |     let _ = (&&&&ATOMIC_TUPLE).2;
    |                 ^^^^^^^^^^^^^
    |
-   = help: assign this const to a local or static variable, and use the variable here
+   = help: this lint can be silenced by assigning the value to a local variable before borrowing
 
-error: a `const` item with interior mutability should not be borrowed
+error: borrow of a named constant with interior mutability
   --> tests/ui/borrow_interior_mutable_const/others.rs:97:5
    |
 LL |     CELL.set(2);
    |     ^^^^
    |
    = note: there is a compiler inserted borrow here
-   = help: assign this const to a local or static variable, and use the variable here
+   = help: this lint can be silenced by assigning the value to a local variable before borrowing
 
-error: a `const` item with interior mutability should not be borrowed
+error: borrow of a named constant with interior mutability
   --> tests/ui/borrow_interior_mutable_const/others.rs:98:16
    |
 LL |     assert_eq!(CELL.get(), 6);
    |                ^^^^
    |
    = note: there is a compiler inserted borrow here
-   = help: assign this const to a local or static variable, and use the variable here
+   = help: this lint can be silenced by assigning the value to a local variable before borrowing
 
 error: aborting due to 15 previous errors
 
diff --git a/tests/ui/borrow_interior_mutable_const/projections.stderr b/tests/ui/borrow_interior_mutable_const/projections.stderr
index b0e1883f8bf..114fd66651a 100644
--- a/tests/ui/borrow_interior_mutable_const/projections.stderr
+++ b/tests/ui/borrow_interior_mutable_const/projections.stderr
@@ -1,44 +1,44 @@
-error: a `const` item should not be interior mutable
+error: named constant with interior mutability
   --> tests/ui/borrow_interior_mutable_const/projections.rs:27:7
    |
 LL | const CELL: Assoc<u8> = UnsafeCell::new(0);
    |       ^^^^
    |
-   = help: consider making this `Sync` so that it can go in a static item or using a `thread_local`
+   = help: did you mean to make this a `thread_local!` item
 note: the lint level is defined here
   --> tests/ui/borrow_interior_mutable_const/projections.rs:2:9
    |
 LL | #![deny(clippy::declare_interior_mutable_const)]
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
-error: a `const` item should not be interior mutable
+error: named constant with interior mutability
   --> tests/ui/borrow_interior_mutable_const/projections.rs:29:7
    |
 LL | const MUTABLE: MaybeMutable = MaybeMutable::Mutable(CELL);
    |       ^^^^^^^
    |
-   = help: consider making this `Sync` so that it can go in a static item or using a `thread_local`
+   = help: did you mean to make this a `thread_local!` item
 
-error: a `const` item with interior mutability should not be borrowed
+error: borrow of a named constant with interior mutability
   --> tests/ui/borrow_interior_mutable_const/projections.rs:38:15
    |
 LL |     print_ref(&CELL);
    |               ^^^^^
    |
-   = help: assign this const to a local or static variable, and use the variable here
+   = help: this lint can be silenced by assigning the value to a local variable before borrowing
 note: the lint level is defined here
   --> tests/ui/borrow_interior_mutable_const/projections.rs:1:9
    |
 LL | #![deny(clippy::borrow_interior_mutable_const)]
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
-error: a `const` item with interior mutability should not be borrowed
+error: borrow of a named constant with interior mutability
   --> tests/ui/borrow_interior_mutable_const/projections.rs:40:15
    |
 LL |     print_ref(&MUTABLE);
    |               ^^^^^^^^
    |
-   = help: assign this const to a local or static variable, and use the variable here
+   = help: this lint can be silenced by assigning the value to a local variable before borrowing
 
 error: aborting due to 4 previous errors
 
diff --git a/tests/ui/borrow_interior_mutable_const/traits.stderr b/tests/ui/borrow_interior_mutable_const/traits.stderr
index 233ec6dad67..1d84ebf2ac9 100644
--- a/tests/ui/borrow_interior_mutable_const/traits.stderr
+++ b/tests/ui/borrow_interior_mutable_const/traits.stderr
@@ -1,145 +1,145 @@
-error: a `const` item with interior mutability should not be borrowed
+error: borrow of a named constant with interior mutability
   --> tests/ui/borrow_interior_mutable_const/traits.rs:15:17
    |
 LL |         let _ = &Self::ATOMIC;
    |                 ^^^^^^^^^^^^^
    |
-   = help: assign this const to a local or static variable, and use the variable here
+   = help: this lint can be silenced by assigning the value to a local variable before borrowing
 note: the lint level is defined here
   --> tests/ui/borrow_interior_mutable_const/traits.rs:1:9
    |
 LL | #![deny(clippy::borrow_interior_mutable_const)]
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
-error: a `const` item with interior mutability should not be borrowed
+error: borrow of a named constant with interior mutability
   --> tests/ui/borrow_interior_mutable_const/traits.rs:26:17
    |
 LL |         let _ = &Self::ATOMIC;
    |                 ^^^^^^^^^^^^^
    |
-   = help: assign this const to a local or static variable, and use the variable here
+   = help: this lint can be silenced by assigning the value to a local variable before borrowing
 
-error: a `const` item with interior mutability should not be borrowed
+error: borrow of a named constant with interior mutability
   --> tests/ui/borrow_interior_mutable_const/traits.rs:51:17
    |
 LL |         let _ = &Self::TO_BE_CONCRETE;
    |                 ^^^^^^^^^^^^^^^^^^^^^
    |
-   = help: assign this const to a local or static variable, and use the variable here
+   = help: this lint can be silenced by assigning the value to a local variable before borrowing
 
-error: a `const` item with interior mutability should not be borrowed
+error: borrow of a named constant with interior mutability
   --> tests/ui/borrow_interior_mutable_const/traits.rs:86:17
    |
 LL |         let _ = &Self::TO_BE_UNFROZEN;
    |                 ^^^^^^^^^^^^^^^^^^^^^
    |
-   = help: assign this const to a local or static variable, and use the variable here
+   = help: this lint can be silenced by assigning the value to a local variable before borrowing
 
-error: a `const` item with interior mutability should not be borrowed
+error: borrow of a named constant with interior mutability
   --> tests/ui/borrow_interior_mutable_const/traits.rs:87:17
    |
 LL |         let _ = &Self::WRAPPED_TO_BE_UNFROZEN;
    |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
-   = help: assign this const to a local or static variable, and use the variable here
+   = help: this lint can be silenced by assigning the value to a local variable before borrowing
 
-error: a `const` item with interior mutability should not be borrowed
+error: borrow of a named constant with interior mutability
   --> tests/ui/borrow_interior_mutable_const/traits.rs:109:17
    |
 LL |         let _ = &Self::BOUNDED;
    |                 ^^^^^^^^^^^^^^
    |
-   = help: assign this const to a local or static variable, and use the variable here
+   = help: this lint can be silenced by assigning the value to a local variable before borrowing
 
-error: a `const` item with interior mutability should not be borrowed
+error: borrow of a named constant with interior mutability
   --> tests/ui/borrow_interior_mutable_const/traits.rs:122:17
    |
 LL |         let _ = &Self::BOUNDED;
    |                 ^^^^^^^^^^^^^^
    |
-   = help: assign this const to a local or static variable, and use the variable here
+   = help: this lint can be silenced by assigning the value to a local variable before borrowing
 
-error: a `const` item with interior mutability should not be borrowed
+error: borrow of a named constant with interior mutability
   --> tests/ui/borrow_interior_mutable_const/traits.rs:151:17
    |
 LL |         let _ = &Self::SELF;
    |                 ^^^^^^^^^^^
    |
-   = help: assign this const to a local or static variable, and use the variable here
+   = help: this lint can be silenced by assigning the value to a local variable before borrowing
 
-error: a `const` item with interior mutability should not be borrowed
+error: borrow of a named constant with interior mutability
   --> tests/ui/borrow_interior_mutable_const/traits.rs:152:17
    |
 LL |         let _ = &Self::WRAPPED_SELF;
    |                 ^^^^^^^^^^^^^^^^^^^
    |
-   = help: assign this const to a local or static variable, and use the variable here
+   = help: this lint can be silenced by assigning the value to a local variable before borrowing
 
-error: a `const` item with interior mutability should not be borrowed
+error: borrow of a named constant with interior mutability
   --> tests/ui/borrow_interior_mutable_const/traits.rs:161:17
    |
 LL |         let _ = &Self::DIRECT;
    |                 ^^^^^^^^^^^^^
    |
-   = help: assign this const to a local or static variable, and use the variable here
+   = help: this lint can be silenced by assigning the value to a local variable before borrowing
 
-error: a `const` item with interior mutability should not be borrowed
+error: borrow of a named constant with interior mutability
   --> tests/ui/borrow_interior_mutable_const/traits.rs:162:17
    |
 LL |         let _ = &Self::INDIRECT;
    |                 ^^^^^^^^^^^^^^^
    |
-   = help: assign this const to a local or static variable, and use the variable here
+   = help: this lint can be silenced by assigning the value to a local variable before borrowing
 
-error: a `const` item with interior mutability should not be borrowed
+error: borrow of a named constant with interior mutability
   --> tests/ui/borrow_interior_mutable_const/traits.rs:171:17
    |
 LL |         let _ = &Self::DIRECT;
    |                 ^^^^^^^^^^^^^
    |
-   = help: assign this const to a local or static variable, and use the variable here
+   = help: this lint can be silenced by assigning the value to a local variable before borrowing
 
-error: a `const` item with interior mutability should not be borrowed
+error: borrow of a named constant with interior mutability
   --> tests/ui/borrow_interior_mutable_const/traits.rs:172:17
    |
 LL |         let _ = &Self::INDIRECT;
    |                 ^^^^^^^^^^^^^^^
    |
-   = help: assign this const to a local or static variable, and use the variable here
+   = help: this lint can be silenced by assigning the value to a local variable before borrowing
 
-error: a `const` item with interior mutability should not be borrowed
+error: borrow of a named constant with interior mutability
   --> tests/ui/borrow_interior_mutable_const/traits.rs:191:17
    |
 LL |         let _ = &Self::ATOMIC;
    |                 ^^^^^^^^^^^^^
    |
-   = help: assign this const to a local or static variable, and use the variable here
+   = help: this lint can be silenced by assigning the value to a local variable before borrowing
 
-error: a `const` item with interior mutability should not be borrowed
+error: borrow of a named constant with interior mutability
   --> tests/ui/borrow_interior_mutable_const/traits.rs:195:17
    |
 LL |         let _ = &Self::BOUNDED_ASSOC_TYPE;
    |                 ^^^^^^^^^^^^^^^^^^^^^^^^^
    |
-   = help: assign this const to a local or static variable, and use the variable here
+   = help: this lint can be silenced by assigning the value to a local variable before borrowing
 
-error: a `const` item with interior mutability should not be borrowed
+error: borrow of a named constant with interior mutability
   --> tests/ui/borrow_interior_mutable_const/traits.rs:200:5
    |
 LL |     u64::ATOMIC.store(5, Ordering::SeqCst);
    |     ^^^^^^^^^^^
    |
    = note: there is a compiler inserted borrow here
-   = help: assign this const to a local or static variable, and use the variable here
+   = help: this lint can be silenced by assigning the value to a local variable before borrowing
 
-error: a `const` item with interior mutability should not be borrowed
+error: borrow of a named constant with interior mutability
   --> tests/ui/borrow_interior_mutable_const/traits.rs:201:16
    |
 LL |     assert_eq!(u64::ATOMIC.load(Ordering::SeqCst), 9);
    |                ^^^^^^^^^^^
    |
    = note: there is a compiler inserted borrow here
-   = help: assign this const to a local or static variable, and use the variable here
+   = help: this lint can be silenced by assigning the value to a local variable before borrowing
 
 error: aborting due to 17 previous errors
 
diff --git a/tests/ui/declare_interior_mutable_const/enums.stderr b/tests/ui/declare_interior_mutable_const/enums.stderr
index 25f39f243e0..4eca533e5ad 100644
--- a/tests/ui/declare_interior_mutable_const/enums.stderr
+++ b/tests/ui/declare_interior_mutable_const/enums.stderr
@@ -1,66 +1,66 @@
-error: a `const` item should not be interior mutable
+error: named constant with interior mutability
   --> tests/ui/declare_interior_mutable_const/enums.rs:12:7
    |
 LL | const UNFROZEN_VARIANT: OptionalCell = OptionalCell::Unfrozen(Cell::new(true));
    |       ^^^^^^^^^^^^^^^^
    |
-   = help: consider making this `Sync` so that it can go in a static item or using a `thread_local`
+   = help: did you mean to make this a `thread_local!` 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 not be interior mutable
+error: named constant with interior mutability
   --> tests/ui/declare_interior_mutable_const/enums.rs:23:7
    |
 LL | const UNFROZEN_VARIANT_FROM_FN: OptionalCell = unfrozen_variant();
    |       ^^^^^^^^^^^^^^^^^^^^^^^^
    |
-   = help: consider making this `Sync` so that it can go in a static item or using a `thread_local`
+   = help: did you mean to make this a `thread_local!` item
 
-error: a `const` item should not be interior mutable
+error: named constant with interior mutability
   --> tests/ui/declare_interior_mutable_const/enums.rs:45:7
    |
 LL | const NESTED_UNFROZEN_VARIANT: NestedOutermost = NestedOutermost {
    |       ^^^^^^^^^^^^^^^^^^^^^^^
    |
-   = help: consider making this a static item
+   = help: did you mean to make this a `static` item
 
-error: a `const` item should not be interior mutable
+error: named constant with interior mutability
   --> tests/ui/declare_interior_mutable_const/enums.rs:64:11
    |
 LL |     const DEFAULTED_ON_UNFROZEN_VARIANT: OptionalCell = OptionalCell::Unfrozen(Cell::new(false));
    |           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
-error: a `const` item should not be interior mutable
+error: named constant with interior mutability
   --> tests/ui/declare_interior_mutable_const/enums.rs:71:11
    |
 LL |     const TO_BE_UNFROZEN_VARIANT: OptionalCell = OptionalCell::Unfrozen(Cell::new(false));
    |           ^^^^^^^^^^^^^^^^^^^^^^
 
-error: a `const` item should not be interior mutable
+error: named constant with interior mutability
   --> tests/ui/declare_interior_mutable_const/enums.rs:75:11
    |
 LL |     const DEFAULTED_ON_FROZEN_VARIANT: OptionalCell = OptionalCell::Unfrozen(Cell::new(false));
    |           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
-error: a `const` item should not be interior mutable
+error: named constant with interior mutability
   --> tests/ui/declare_interior_mutable_const/enums.rs:90:11
    |
 LL |     const TO_BE_UNFROZEN_VARIANT: Option<Self::ToBeUnfrozen> = Some(Self::ToBeUnfrozen::new(4));
    |           ^^^^^^^^^^^^^^^^^^^^^^
 
-error: a `const` item should not be interior mutable
+error: named constant with interior mutability
   --> tests/ui/declare_interior_mutable_const/enums.rs:102:11
    |
 LL |     const UNFROZEN_VARIANT: BothOfCellAndGeneric<T> = BothOfCellAndGeneric::Unfrozen(Cell::new(std::ptr::null()));
    |           ^^^^^^^^^^^^^^^^
 
-error: a `const` item should not be interior mutable
+error: named constant with interior mutability
   --> tests/ui/declare_interior_mutable_const/enums.rs:111:11
    |
 LL |     const NO_ENUM: Cell<*const T> = Cell::new(std::ptr::null());
    |           ^^^^^^^
 
-error: a `const` item should not be interior mutable
+error: named constant with interior mutability
   --> tests/ui/declare_interior_mutable_const/enums.rs:118:11
    |
 LL |     const UNFROZEN_VARIANT: BothOfCellAndGeneric<Self::AssocType> =
diff --git a/tests/ui/declare_interior_mutable_const/others.stderr b/tests/ui/declare_interior_mutable_const/others.stderr
index 243c6b0cc5f..67fb4d046a6 100644
--- a/tests/ui/declare_interior_mutable_const/others.stderr
+++ b/tests/ui/declare_interior_mutable_const/others.stderr
@@ -1,38 +1,38 @@
-error: a `const` item should not be interior mutable
+error: named constant with interior mutability
   --> tests/ui/declare_interior_mutable_const/others.rs:10:7
    |
 LL | const ATOMIC: AtomicUsize = AtomicUsize::new(5);
    |       ^^^^^^
    |
-   = help: consider making this a static item
+   = help: did you mean to make 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 not be interior mutable
+error: named constant with interior mutability
   --> tests/ui/declare_interior_mutable_const/others.rs:11:7
    |
 LL | const CELL: Cell<usize> = Cell::new(6);
    |       ^^^^
    |
-   = help: consider making this `Sync` so that it can go in a static item or using a `thread_local`
+   = help: did you mean to make this a `thread_local!` item
 
-error: a `const` item should not be interior mutable
+error: named constant with interior mutability
   --> tests/ui/declare_interior_mutable_const/others.rs:12:7
    |
 LL | const ATOMIC_TUPLE: ([AtomicUsize; 1], Vec<AtomicUsize>, u8) = ([ATOMIC], Vec::new(), 7);
    |       ^^^^^^^^^^^^
    |
-   = help: consider making this a static item
+   = help: did you mean to make this a `static` item
 
-error: a `const` item should not be interior mutable
+error: named constant with interior mutability
   --> tests/ui/declare_interior_mutable_const/others.rs:20:16
    |
 LL | declare_const!(_ONCE: Once = Once::new());
    |                ^^^^^
    |
-   = help: consider making this a static item
+   = help: did you mean to make this a `static` item
 
-error: a `const` item should not be interior mutable
+error: named constant with interior mutability
   --> tests/ui/declare_interior_mutable_const/others.rs:44:19
    |
 LL |             const _BAZ: Cell<usize> = Cell::new(0);
@@ -41,7 +41,7 @@ LL |             const _BAZ: Cell<usize> = Cell::new(0);
 LL |     issue_8493!();
    |     ------------- in this macro invocation
    |
-   = help: consider making this `Sync` so that it can go in a static item or using a `thread_local`
+   = help: did you mean to make this a `thread_local!` item
    = note: this error originates in the macro `issue_8493` (in Nightly builds, run with -Z macro-backtrace for more info)
 
 error: aborting due to 5 previous errors
diff --git a/tests/ui/declare_interior_mutable_const/traits.stderr b/tests/ui/declare_interior_mutable_const/traits.stderr
index 9b9d9ddeef9..a5c08871ba7 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 not be interior mutable
+error: named constant with interior mutability
   --> tests/ui/declare_interior_mutable_const/traits.rs:15:11
    |
 LL |     const ATOMIC: AtomicUsize;
@@ -7,55 +7,55 @@ 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 not be interior mutable
+error: named constant with interior mutability
   --> tests/ui/declare_interior_mutable_const/traits.rs:18:20
    |
 LL |     declare_const!(ANOTHER_ATOMIC: AtomicUsize = Self::ATOMIC);
    |                    ^^^^^^^^^^^^^^
 
-error: a `const` item should not be interior mutable
+error: named constant with interior mutability
   --> tests/ui/declare_interior_mutable_const/traits.rs:68:11
    |
 LL |     const TO_BE_UNFROZEN: Self::ToBeUnfrozen = AtomicUsize::new(13);
    |           ^^^^^^^^^^^^^^
 
-error: a `const` item should not be interior mutable
+error: named constant with interior mutability
   --> tests/ui/declare_interior_mutable_const/traits.rs:69:11
    |
 LL |     const WRAPPED_TO_BE_UNFROZEN: Wrapper<Self::ToBeUnfrozen> = Wrapper(AtomicUsize::new(14));
    |           ^^^^^^^^^^^^^^^^^^^^^^
 
-error: a `const` item should not be interior mutable
+error: named constant with interior mutability
   --> tests/ui/declare_interior_mutable_const/traits.rs:88:11
    |
 LL |     const BOUNDED: T::ToBeBounded;
    |           ^^^^^^^
 
-error: a `const` item should not be interior mutable
+error: named constant with interior mutability
   --> tests/ui/declare_interior_mutable_const/traits.rs:117:11
    |
 LL |     const WRAPPED_SELF: Option<Self> = Some(AtomicUsize::new(21));
    |           ^^^^^^^^^^^^
 
-error: a `const` item should not be interior mutable
+error: named constant with interior mutability
   --> tests/ui/declare_interior_mutable_const/traits.rs:123:11
    |
 LL |     const DIRECT: Cell<T>;
    |           ^^^^^^
 
-error: a `const` item should not be interior mutable
+error: named constant with interior mutability
   --> tests/ui/declare_interior_mutable_const/traits.rs:124:11
    |
 LL |     const INDIRECT: Cell<*const T>;
    |           ^^^^^^^^
 
-error: a `const` item should not be interior mutable
+error: named constant with interior mutability
   --> tests/ui/declare_interior_mutable_const/traits.rs:140:11
    |
 LL |     const ATOMIC: AtomicUsize = AtomicUsize::new(18);
    |           ^^^^^^
 
-error: a `const` item should not be interior mutable
+error: named constant with interior mutability
   --> tests/ui/declare_interior_mutable_const/traits.rs:146:11
    |
 LL |     const BOUNDED_ASSOC_TYPE: T::ToBeBounded = AtomicUsize::new(19);