1 #![warn(clippy::borrow_interior_mutable_const)]
2 #![allow(clippy::declare_interior_mutable_const, clippy::ref_in_deref)]
3 #![allow(const_item_mutation)]
6 use std::cell::{Cell, UnsafeCell};
8 use std::sync::atomic::{AtomicUsize, Ordering};
11 const ATOMIC: AtomicUsize = AtomicUsize::new(5);
12 const CELL: Cell<usize> = Cell::new(6);
13 const ATOMIC_TUPLE: ([AtomicUsize; 1], Vec<AtomicUsize>, u8) = ([ATOMIC], Vec::new(), 7);
14 const INTEGER: u8 = 8;
15 const STRING: String = String::new();
16 const STR: &str = "012345";
17 const COW: Cow<str> = Cow::Borrowed("abcdef");
18 const NO_ANN: &dyn Display = &70;
19 static STATIC_TUPLE: (AtomicUsize, String) = (ATOMIC, STRING);
20 const ONCE_INIT: Once = Once::new();
25 const ATOMIC: AtomicUsize;
27 const ASSOC: Self::AssocType;
35 impl Trait<u32> for u64 {
36 type AssocType = AtomicUsize;
38 const ATOMIC: AtomicUsize = AtomicUsize::new(9);
39 const INPUT: u32 = 10;
40 const ASSOC: Self::AssocType = AtomicUsize::new(11);
44 let _ = &Self::ASSOC; //~ ERROR interior mutability
48 // This is just a pointer that can be safely dereferended,
49 // it's semantically the same as `&'static T`;
50 // but it isn't allowed to make a static reference from an arbitrary integer value at the moment.
51 // For more information, please see the issue #5918.
52 pub struct StaticRef<T> {
56 impl<T> StaticRef<T> {
57 /// Create a new `StaticRef` from a raw pointer
61 /// Callers must pass in a reference to statically allocated memory which
62 /// does not overlap with other values.
63 pub const unsafe fn new(ptr: *const T) -> StaticRef<T> {
68 impl<T> std::ops::Deref for StaticRef<T> {
71 fn deref(&self) -> &'static T {
76 // use a tuple to make sure referencing a field behind a pointer isn't linted.
77 const CELL_REF: StaticRef<(UnsafeCell<u32>,)> = unsafe { StaticRef::new(std::ptr::null()) };
80 ATOMIC.store(1, Ordering::SeqCst); //~ ERROR interior mutability
81 assert_eq!(ATOMIC.load(Ordering::SeqCst), 5); //~ ERROR interior mutability
83 let _once = ONCE_INIT;
84 let _once_ref = &ONCE_INIT; //~ ERROR interior mutability
85 let _once_ref_2 = &&ONCE_INIT; //~ ERROR interior mutability
86 let _once_ref_4 = &&&&ONCE_INIT; //~ ERROR interior mutability
87 let _once_mut = &mut ONCE_INIT; //~ ERROR interior mutability
88 let _atomic_into_inner = ATOMIC.into_inner();
89 // these should be all fine.
90 let _twice = (ONCE_INIT, ONCE_INIT);
91 let _ref_twice = &(ONCE_INIT, ONCE_INIT);
92 let _ref_once = &(ONCE_INIT, ONCE_INIT).0;
93 let _array_twice = [ONCE_INIT, ONCE_INIT];
94 let _ref_array_twice = &[ONCE_INIT, ONCE_INIT];
95 let _ref_array_once = &[ONCE_INIT, ONCE_INIT][0];
97 // referencing projection is still bad.
98 let _ = &ATOMIC_TUPLE; //~ ERROR interior mutability
99 let _ = &ATOMIC_TUPLE.0; //~ ERROR interior mutability
100 let _ = &(&&&&ATOMIC_TUPLE).0; //~ ERROR interior mutability
101 let _ = &ATOMIC_TUPLE.0[0]; //~ ERROR interior mutability
102 let _ = ATOMIC_TUPLE.0[0].load(Ordering::SeqCst); //~ ERROR interior mutability
103 let _ = &*ATOMIC_TUPLE.1; //~ ERROR interior mutability
104 let _ = &ATOMIC_TUPLE.2;
105 let _ = (&&&&ATOMIC_TUPLE).0;
106 let _ = (&&&&ATOMIC_TUPLE).2;
107 let _ = ATOMIC_TUPLE.0;
108 let _ = ATOMIC_TUPLE.0[0]; //~ ERROR interior mutability
109 let _ = ATOMIC_TUPLE.1.into_iter();
110 let _ = ATOMIC_TUPLE.2;
111 let _ = &{ ATOMIC_TUPLE };
113 CELL.set(2); //~ ERROR interior mutability
114 assert_eq!(CELL.get(), 6); //~ ERROR interior mutability
116 assert_eq!(INTEGER, 8);
117 assert!(STRING.is_empty());
120 a.store(4, Ordering::SeqCst);
121 assert_eq!(a.load(Ordering::SeqCst), 4);
123 STATIC_TUPLE.0.store(3, Ordering::SeqCst);
124 assert_eq!(STATIC_TUPLE.0.load(Ordering::SeqCst), 3);
125 assert!(STATIC_TUPLE.1.is_empty());
127 u64::ATOMIC.store(5, Ordering::SeqCst); //~ ERROR interior mutability
128 assert_eq!(u64::ATOMIC.load(Ordering::SeqCst), 9); //~ ERROR interior mutability
130 assert_eq!(NO_ANN.to_string(), "70"); // should never lint this.