Skip to content

Commit

Permalink
Auto merge of rust-lang#13290 - Jarcho:interior_mut_quick, r=Alexendoo
Browse files Browse the repository at this point in the history
`declare_interior_mutable_const`: Ignore pointer types.

fixes rust-lang#12951
fixes rust-lang#13233

changelog: `declare_interior_mutable_const`: Ignore pointer types.
  • Loading branch information
bors authored and Jarcho committed Aug 26, 2024
1 parent 29f5758 commit 926f639
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion clippy_lints/src/non_copy_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ impl<'tcx> NonCopyConst<'tcx> {

impl<'tcx> LateLintPass<'tcx> for NonCopyConst<'tcx> {
fn check_crate(&mut self, cx: &LateContext<'tcx>) {
self.interior_mut = InteriorMut::new(cx, &self.ignore_interior_mutability);
self.interior_mut = InteriorMut::without_pointers(cx, &self.ignore_interior_mutability);
}

fn check_item(&mut self, cx: &LateContext<'tcx>, it: &'tcx Item<'_>) {
Expand Down
17 changes: 17 additions & 0 deletions tests/ui/declare_interior_mutable_const/others.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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() {}
10 changes: 5 additions & 5 deletions tests/ui/declare_interior_mutable_const/others.stderr
Original file line number Diff line number Diff line change
@@ -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);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -9,23 +9,23 @@ 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);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= 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);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= 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;
| ^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -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);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down

0 comments on commit 926f639

Please sign in to comment.