Skip to content

Commit

Permalink
Auto merge of #12900 - Alexendoo:no-lazy-static, r=llogiq
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed Jun 7, 2024
2 parents d553ebe + 65af5d7 commit 0ea88b9
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 101 deletions.
32 changes: 20 additions & 12 deletions clippy_lints/src/non_copy_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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
Expand Down Expand Up @@ -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 } => (
Expand All @@ -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 { .. } => {
Expand Down Expand Up @@ -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 });
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions tests/ui/crashes/ice-9445.rs
Original file line number Diff line number Diff line change
@@ -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() {}
7 changes: 3 additions & 4 deletions tests/ui/crashes/ice-9445.stderr
Original file line number Diff line number Diff line change
@@ -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)]`

Expand Down
33 changes: 0 additions & 33 deletions tests/ui/crashes/mut_mut_macro.rs

This file was deleted.

43 changes: 20 additions & 23 deletions tests/ui/declare_interior_mutable_const/enums.stderr
Original file line number Diff line number Diff line change
@@ -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());
Expand Down
27 changes: 13 additions & 14 deletions tests/ui/declare_interior_mutable_const/others.stderr
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand Down
26 changes: 13 additions & 13 deletions tests/ui/declare_interior_mutable_const/traits.stderr
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit 0ea88b9

Please sign in to comment.