declare_interior_mutable_const: Ignore pointer types.

This commit is contained in:
Jason Newcomb 2024-08-20 11:52:47 -04:00
parent d0e637da19
commit 687d4e3d16
3 changed files with 23 additions and 6 deletions

View File

@ -184,7 +184,7 @@ impl_lint_pass!(NonCopyConst<'_> => [DECLARE_INTERIOR_MUTABLE_CONST, BORROW_INTE
impl<'tcx> NonCopyConst<'tcx> {
pub fn new(tcx: TyCtxt<'tcx>, conf: &'static Conf) -> Self {
Self {
interior_mut: InteriorMut::new(tcx, &conf.ignore_interior_mutability),
interior_mut: InteriorMut::without_pointers(tcx, &conf.ignore_interior_mutability),
}
}

View File

@ -3,6 +3,7 @@
use std::borrow::Cow;
use std::cell::Cell;
use std::fmt::Display;
use std::ptr;
use std::sync::atomic::AtomicUsize;
use std::sync::Once;
@ -53,4 +54,20 @@ mod issue_8493 {
issue_8493!();
}
#[repr(C, align(8))]
struct NoAtomic(usize);
#[repr(C, align(8))]
struct WithAtomic(AtomicUsize);
const fn with_non_null() -> *const WithAtomic {
const NO_ATOMIC: NoAtomic = NoAtomic(0);
(&NO_ATOMIC as *const NoAtomic).cast()
}
const WITH_ATOMIC: *const WithAtomic = with_non_null();
struct Generic<T>(T);
impl<T> Generic<T> {
const RAW_POINTER: *const Cell<T> = ptr::null();
}
fn main() {}

View File

@ -1,5 +1,5 @@
error: a `const` item should not be interior mutable
--> tests/ui/declare_interior_mutable_const/others.rs:9:1
--> tests/ui/declare_interior_mutable_const/others.rs:10:1
|
LL | const ATOMIC: AtomicUsize = AtomicUsize::new(5);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -9,7 +9,7 @@ LL | const ATOMIC: AtomicUsize = AtomicUsize::new(5);
= help: to override `-D warnings` add `#[allow(clippy::declare_interior_mutable_const)]`
error: a `const` item should not be interior mutable
--> tests/ui/declare_interior_mutable_const/others.rs:10:1
--> tests/ui/declare_interior_mutable_const/others.rs:11:1
|
LL | const CELL: Cell<usize> = Cell::new(6);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -17,7 +17,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`
error: a `const` item should not be interior mutable
--> tests/ui/declare_interior_mutable_const/others.rs:11:1
--> tests/ui/declare_interior_mutable_const/others.rs:12:1
|
LL | const ATOMIC_TUPLE: ([AtomicUsize; 1], Vec<AtomicUsize>, u8) = ([ATOMIC], Vec::new(), 7);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -25,7 +25,7 @@ LL | const ATOMIC_TUPLE: ([AtomicUsize; 1], Vec<AtomicUsize>, u8) = ([ATOMIC], V
= help: consider making this a static item
error: a `const` item should not be interior mutable
--> tests/ui/declare_interior_mutable_const/others.rs:16:9
--> tests/ui/declare_interior_mutable_const/others.rs:17:9
|
LL | const $name: $ty = $e;
| ^^^^^^^^^^^^^^^^^^^^^^
@ -36,7 +36,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 not be interior mutable
--> tests/ui/declare_interior_mutable_const/others.rs:44:13
--> tests/ui/declare_interior_mutable_const/others.rs:45:13
|
LL | const _BAZ: Cell<usize> = Cell::new(0);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^