Skip to content

Commit 0ea88b9

Browse files
committed
Auto merge of rust-lang#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
2 parents d553ebe + 65af5d7 commit 0ea88b9

File tree

7 files changed

+69
-101
lines changed

7 files changed

+69
-101
lines changed

clippy_lints/src/non_copy_const.rs

+20-12
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::ptr;
77
use clippy_utils::diagnostics::span_lint_and_then;
88
use clippy_utils::in_constant;
99
use clippy_utils::macros::macro_backtrace;
10-
use clippy_utils::ty::InteriorMut;
10+
use clippy_utils::ty::{implements_trait, InteriorMut};
1111
use rustc_hir::def::{DefKind, Res};
1212
use rustc_hir::def_id::DefId;
1313
use rustc_hir::{
@@ -18,7 +18,7 @@ use rustc_middle::mir::interpret::{ErrorHandled, EvalToValTreeResult, GlobalId};
1818
use rustc_middle::ty::adjustment::Adjust;
1919
use rustc_middle::ty::{self, Ty, TyCtxt};
2020
use rustc_session::impl_lint_pass;
21-
use rustc_span::{sym, InnerSpan, Span, DUMMY_SP};
21+
use rustc_span::{sym, Span, DUMMY_SP};
2222
use rustc_target::abi::VariantIdx;
2323

2424
// FIXME: this is a correctness problem but there's no suitable
@@ -127,19 +127,19 @@ declare_clippy_lint! {
127127
}
128128

129129
#[derive(Copy, Clone)]
130-
enum Source {
131-
Item { item: Span },
130+
enum Source<'tcx> {
131+
Item { item: Span, ty: Ty<'tcx> },
132132
Assoc { item: Span },
133133
Expr { expr: Span },
134134
}
135135

136-
impl Source {
136+
impl Source<'_> {
137137
#[must_use]
138138
fn lint(&self) -> (&'static Lint, &'static str, Span) {
139139
match self {
140-
Self::Item { item } | Self::Assoc { item, .. } => (
140+
Self::Item { item, .. } | Self::Assoc { item, .. } => (
141141
DECLARE_INTERIOR_MUTABLE_CONST,
142-
"a `const` item should never be interior mutable",
142+
"a `const` item should not be interior mutable",
143143
*item,
144144
),
145145
Self::Expr { expr } => (
@@ -151,16 +151,24 @@ impl Source {
151151
}
152152
}
153153

154-
fn lint(cx: &LateContext<'_>, source: Source) {
154+
fn lint<'tcx>(cx: &LateContext<'tcx>, source: Source<'tcx>) {
155155
let (lint, msg, span) = source.lint();
156156
span_lint_and_then(cx, lint, span, msg, |diag| {
157157
if span.from_expansion() {
158158
return; // Don't give suggestions into macros.
159159
}
160160
match source {
161-
Source::Item { .. } => {
162-
let const_kw_span = span.from_inner(InnerSpan::new(0, 5));
163-
diag.span_label(const_kw_span, "make this a static item (maybe with lazy_static)");
161+
Source::Item { ty, .. } => {
162+
let Some(sync_trait) = cx.tcx.lang_items().sync_trait() else {
163+
return;
164+
};
165+
if implements_trait(cx, ty, sync_trait, &[]) {
166+
diag.help("consider making this a static item");
167+
} else {
168+
diag.help(
169+
"consider making this `Sync` so that it can go in a static item or using a `thread_local`",
170+
);
171+
}
164172
},
165173
Source::Assoc { .. } => (),
166174
Source::Expr { .. } => {
@@ -311,7 +319,7 @@ impl<'tcx> LateLintPass<'tcx> for NonCopyConst<'tcx> {
311319
&& self.interior_mut.is_interior_mut_ty(cx, ty)
312320
&& Self::is_value_unfrozen_poly(cx, body_id, ty)
313321
{
314-
lint(cx, Source::Item { item: it.span });
322+
lint(cx, Source::Item { item: it.span, ty });
315323
}
316324
}
317325
}

tests/ui/crashes/ice-9445.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
const UNINIT: core::mem::MaybeUninit<core::cell::Cell<&'static ()>> = core::mem::MaybeUninit::uninit();
2-
//~^ ERROR: a `const` item should never be interior mutable
3-
//~| NOTE: `-D clippy::declare-interior-mutable-const` implied by `-D warnings`
42

53
fn main() {}

tests/ui/crashes/ice-9445.stderr

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
error: a `const` item should never be interior mutable
1+
error: a `const` item should not be interior mutable
22
--> tests/ui/crashes/ice-9445.rs:1:1
33
|
44
LL | const UNINIT: core::mem::MaybeUninit<core::cell::Cell<&'static ()>> = core::mem::MaybeUninit::uninit();
5-
| -----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6-
| |
7-
| make this a static item (maybe with lazy_static)
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
86
|
7+
= help: consider making this `Sync` so that it can go in a static item or using a `thread_local`
98
= note: `-D clippy::declare-interior-mutable-const` implied by `-D warnings`
109
= help: to override `-D warnings` add `#[allow(clippy::declare_interior_mutable_const)]`
1110

tests/ui/crashes/mut_mut_macro.rs

-33
This file was deleted.

tests/ui/declare_interior_mutable_const/enums.stderr

+20-23
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,84 @@
1-
error: a `const` item should never be interior mutable
1+
error: a `const` item should not be interior mutable
22
--> tests/ui/declare_interior_mutable_const/enums.rs:12:1
33
|
44
LL | const UNFROZEN_VARIANT: OptionalCell = OptionalCell::Unfrozen(Cell::new(true));
5-
| -----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6-
| |
7-
| make this a static item (maybe with lazy_static)
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
86
|
7+
= help: consider making this `Sync` so that it can go in a static item or using a `thread_local`
98
= note: `-D clippy::declare-interior-mutable-const` implied by `-D warnings`
109
= help: to override `-D warnings` add `#[allow(clippy::declare_interior_mutable_const)]`
1110

12-
error: a `const` item should never be interior mutable
11+
error: a `const` item should not be interior mutable
1312
--> tests/ui/declare_interior_mutable_const/enums.rs:23:1
1413
|
1514
LL | const UNFROZEN_VARIANT_FROM_FN: OptionalCell = unfrozen_variant();
16-
| -----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
17-
| |
18-
| make this a static item (maybe with lazy_static)
15+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
16+
|
17+
= help: consider making this `Sync` so that it can go in a static item or using a `thread_local`
1918

20-
error: a `const` item should never be interior mutable
19+
error: a `const` item should not be interior mutable
2120
--> tests/ui/declare_interior_mutable_const/enums.rs:45:1
2221
|
23-
LL | const NESTED_UNFROZEN_VARIANT: NestedOutermost = NestedOutermost {
24-
| ^----
25-
| |
26-
| _make this a static item (maybe with lazy_static)
27-
| |
22+
LL | / const NESTED_UNFROZEN_VARIANT: NestedOutermost = NestedOutermost {
2823
LL | |
2924
LL | | outer: NestedOuter::NestedInner(NestedInner {
3025
LL | | inner: NestedInnermost::Unfrozen(AtomicUsize::new(2)),
3126
LL | | }),
3227
LL | | };
3328
| |__^
29+
|
30+
= help: consider making this a static item
3431

35-
error: a `const` item should never be interior mutable
32+
error: a `const` item should not be interior mutable
3633
--> tests/ui/declare_interior_mutable_const/enums.rs:60:5
3734
|
3835
LL | const TO_BE_UNFROZEN_VARIANT: OptionalCell;
3936
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4037

41-
error: a `const` item should never be interior mutable
38+
error: a `const` item should not be interior mutable
4239
--> tests/ui/declare_interior_mutable_const/enums.rs:61:5
4340
|
4441
LL | const TO_BE_FROZEN_VARIANT: OptionalCell;
4542
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4643

47-
error: a `const` item should never be interior mutable
44+
error: a `const` item should not be interior mutable
4845
--> tests/ui/declare_interior_mutable_const/enums.rs:64:5
4946
|
5047
LL | const DEFAULTED_ON_UNFROZEN_VARIANT: OptionalCell = OptionalCell::Unfrozen(Cell::new(false));
5148
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5249

53-
error: a `const` item should never be interior mutable
50+
error: a `const` item should not be interior mutable
5451
--> tests/ui/declare_interior_mutable_const/enums.rs:90:5
5552
|
5653
LL | const TO_BE_UNFROZEN_VARIANT: Option<Self::ToBeUnfrozen> = Some(Self::ToBeUnfrozen::new(4));
5754
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5855

59-
error: a `const` item should never be interior mutable
56+
error: a `const` item should not be interior mutable
6057
--> tests/ui/declare_interior_mutable_const/enums.rs:102:5
6158
|
6259
LL | const UNFROZEN_VARIANT: BothOfCellAndGeneric<T> = BothOfCellAndGeneric::Unfrozen(Cell::new(std::ptr::null()));
6360
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6461

65-
error: a `const` item should never be interior mutable
62+
error: a `const` item should not be interior mutable
6663
--> tests/ui/declare_interior_mutable_const/enums.rs:105:5
6764
|
6865
LL | const GENERIC_VARIANT: BothOfCellAndGeneric<T> = BothOfCellAndGeneric::Generic(std::ptr::null());
6966
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7067

71-
error: a `const` item should never be interior mutable
68+
error: a `const` item should not be interior mutable
7269
--> tests/ui/declare_interior_mutable_const/enums.rs:111:5
7370
|
7471
LL | const NO_ENUM: Cell<*const T> = Cell::new(std::ptr::null());
7572
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7673

77-
error: a `const` item should never be interior mutable
74+
error: a `const` item should not be interior mutable
7875
--> tests/ui/declare_interior_mutable_const/enums.rs:118:5
7976
|
8077
LL | / const UNFROZEN_VARIANT: BothOfCellAndGeneric<Self::AssocType> =
8178
LL | | BothOfCellAndGeneric::Unfrozen(Cell::new(std::ptr::null()));
8279
| |____________________________________________________________________^
8380

84-
error: a `const` item should never be interior mutable
81+
error: a `const` item should not be interior mutable
8582
--> tests/ui/declare_interior_mutable_const/enums.rs:120:5
8683
|
8784
LL | const GENERIC_VARIANT: BothOfCellAndGeneric<Self::AssocType> = BothOfCellAndGeneric::Generic(std::ptr::null());

tests/ui/declare_interior_mutable_const/others.stderr

+13-14
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
1-
error: a `const` item should never be interior mutable
1+
error: a `const` item should not be interior mutable
22
--> tests/ui/declare_interior_mutable_const/others.rs:9:1
33
|
44
LL | const ATOMIC: AtomicUsize = AtomicUsize::new(5);
5-
| -----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6-
| |
7-
| make this a static item (maybe with lazy_static)
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
86
|
7+
= help: consider making this a static item
98
= note: `-D clippy::declare-interior-mutable-const` implied by `-D warnings`
109
= help: to override `-D warnings` add `#[allow(clippy::declare_interior_mutable_const)]`
1110

12-
error: a `const` item should never be interior mutable
11+
error: a `const` item should not be interior mutable
1312
--> tests/ui/declare_interior_mutable_const/others.rs:10:1
1413
|
1514
LL | const CELL: Cell<usize> = Cell::new(6);
16-
| -----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
17-
| |
18-
| make this a static item (maybe with lazy_static)
15+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
16+
|
17+
= help: consider making this `Sync` so that it can go in a static item or using a `thread_local`
1918

20-
error: a `const` item should never be interior mutable
19+
error: a `const` item should not be interior mutable
2120
--> tests/ui/declare_interior_mutable_const/others.rs:11:1
2221
|
2322
LL | const ATOMIC_TUPLE: ([AtomicUsize; 1], Vec<AtomicUsize>, u8) = ([ATOMIC], Vec::new(), 7);
24-
| -----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
25-
| |
26-
| make this a static item (maybe with lazy_static)
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
24+
|
25+
= help: consider making this a static item
2726

28-
error: a `const` item should never be interior mutable
27+
error: a `const` item should not be interior mutable
2928
--> tests/ui/declare_interior_mutable_const/others.rs:16:9
3029
|
3130
LL | const $name: $ty = $e;
@@ -36,7 +35,7 @@ LL | declare_const!(_ONCE: Once = Once::new());
3635
|
3736
= note: this error originates in the macro `declare_const` (in Nightly builds, run with -Z macro-backtrace for more info)
3837

39-
error: a `const` item should never be interior mutable
38+
error: a `const` item should not be interior mutable
4039
--> tests/ui/declare_interior_mutable_const/others.rs:44:13
4140
|
4241
LL | const _BAZ: Cell<usize> = Cell::new(0);

tests/ui/declare_interior_mutable_const/traits.stderr

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: a `const` item should never be interior mutable
1+
error: a `const` item should not be interior mutable
22
--> tests/ui/declare_interior_mutable_const/traits.rs:16:5
33
|
44
LL | const ATOMIC: AtomicUsize;
@@ -7,7 +7,7 @@ LL | const ATOMIC: AtomicUsize;
77
= note: `-D clippy::declare-interior-mutable-const` implied by `-D warnings`
88
= help: to override `-D warnings` add `#[allow(clippy::declare_interior_mutable_const)]`
99

10-
error: a `const` item should never be interior mutable
10+
error: a `const` item should not be interior mutable
1111
--> tests/ui/declare_interior_mutable_const/traits.rs:9:9
1212
|
1313
LL | const $name: $ty = $e;
@@ -18,67 +18,67 @@ LL | declare_const!(ANOTHER_ATOMIC: AtomicUsize = Self::ATOMIC);
1818
|
1919
= note: this error originates in the macro `declare_const` (in Nightly builds, run with -Z macro-backtrace for more info)
2020

21-
error: a `const` item should never be interior mutable
21+
error: a `const` item should not be interior mutable
2222
--> tests/ui/declare_interior_mutable_const/traits.rs:44:5
2323
|
2424
LL | const TO_BE_CONCRETE: AtomicUsize = AtomicUsize::new(11);
2525
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2626

27-
error: a `const` item should never be interior mutable
27+
error: a `const` item should not be interior mutable
2828
--> tests/ui/declare_interior_mutable_const/traits.rs:69:5
2929
|
3030
LL | const TO_BE_UNFROZEN: Self::ToBeUnfrozen = AtomicUsize::new(13);
3131
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3232

33-
error: a `const` item should never be interior mutable
33+
error: a `const` item should not be interior mutable
3434
--> tests/ui/declare_interior_mutable_const/traits.rs:70:5
3535
|
3636
LL | const WRAPPED_TO_BE_UNFROZEN: Wrapper<Self::ToBeUnfrozen> = Wrapper(AtomicUsize::new(14));
3737
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3838

39-
error: a `const` item should never be interior mutable
39+
error: a `const` item should not be interior mutable
4040
--> tests/ui/declare_interior_mutable_const/traits.rs:89:5
4141
|
4242
LL | const BOUNDED: T::ToBeBounded;
4343
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4444

45-
error: a `const` item should never be interior mutable
45+
error: a `const` item should not be interior mutable
4646
--> tests/ui/declare_interior_mutable_const/traits.rs:117:5
4747
|
4848
LL | const SELF: Self = AtomicUsize::new(17);
4949
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5050

51-
error: a `const` item should never be interior mutable
51+
error: a `const` item should not be interior mutable
5252
--> tests/ui/declare_interior_mutable_const/traits.rs:118:5
5353
|
5454
LL | const WRAPPED_SELF: Option<Self> = Some(AtomicUsize::new(21));
5555
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5656

57-
error: a `const` item should never be interior mutable
57+
error: a `const` item should not be interior mutable
5858
--> tests/ui/declare_interior_mutable_const/traits.rs:124:5
5959
|
6060
LL | const DIRECT: Cell<T>;
6161
| ^^^^^^^^^^^^^^^^^^^^^^
6262

63-
error: a `const` item should never be interior mutable
63+
error: a `const` item should not be interior mutable
6464
--> tests/ui/declare_interior_mutable_const/traits.rs:125:5
6565
|
6666
LL | const INDIRECT: Cell<*const T>;
6767
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6868

69-
error: a `const` item should never be interior mutable
69+
error: a `const` item should not be interior mutable
7070
--> tests/ui/declare_interior_mutable_const/traits.rs:129:5
7171
|
7272
LL | const DIRECT: Cell<T> = Cell::new(T::DEFAULT);
7373
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7474

75-
error: a `const` item should never be interior mutable
75+
error: a `const` item should not be interior mutable
7676
--> tests/ui/declare_interior_mutable_const/traits.rs:141:5
7777
|
7878
LL | const ATOMIC: AtomicUsize = AtomicUsize::new(18);
7979
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8080

81-
error: a `const` item should never be interior mutable
81+
error: a `const` item should not be interior mutable
8282
--> tests/ui/declare_interior_mutable_const/traits.rs:147:5
8383
|
8484
LL | const BOUNDED_ASSOC_TYPE: T::ToBeBounded = AtomicUsize::new(19);

0 commit comments

Comments
 (0)