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
This commit is contained in:
commit
0ea88b90d8
@ -7,7 +7,7 @@
|
||||
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::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 @@
|
||||
}
|
||||
|
||||
#[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 @@ fn lint(&self) -> (&'static Lint, &'static str, Span) {
|
||||
}
|
||||
}
|
||||
|
||||
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 @@ fn check_item(&mut self, cx: &LateContext<'tcx>, it: &'tcx Item<'_>) {
|
||||
&& 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 });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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() {}
|
||||
|
@ -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)]`
|
||||
|
||||
|
@ -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];
|
||||
}
|
@ -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());
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user