1 #![warn(clippy::borrow_interior_mutable_const)]
2 #![allow(clippy::declare_interior_mutable_const, clippy::needless_borrow)]
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();
22 // This is just a pointer that can be safely dereferenced,
23 // it's semantically the same as `&'static T`;
24 // but it isn't allowed to make a static reference from an arbitrary integer value at the moment.
25 // For more information, please see the issue #5918.
26 pub struct StaticRef<T> {
30 impl<T> StaticRef<T> {
31 /// Create a new `StaticRef` from a raw pointer
35 /// Callers must pass in a reference to statically allocated memory which
36 /// does not overlap with other values.
37 pub const unsafe fn new(ptr: *const T) -> StaticRef<T> {
42 impl<T> std::ops::Deref for StaticRef<T> {
45 fn deref(&self) -> &'static T {
50 // use a tuple to make sure referencing a field behind a pointer isn't linted.
51 const CELL_REF: StaticRef<(UnsafeCell<u32>,)> = unsafe { StaticRef::new(std::ptr::null()) };
54 ATOMIC.store(1, Ordering::SeqCst); //~ ERROR interior mutability
55 assert_eq!(ATOMIC.load(Ordering::SeqCst), 5); //~ ERROR interior mutability
57 let _once = ONCE_INIT;
58 let _once_ref = &ONCE_INIT; //~ ERROR interior mutability
59 let _once_ref_2 = &&ONCE_INIT; //~ ERROR interior mutability
60 let _once_ref_4 = &&&&ONCE_INIT; //~ ERROR interior mutability
61 let _once_mut = &mut ONCE_INIT; //~ ERROR interior mutability
62 let _atomic_into_inner = ATOMIC.into_inner();
63 // these should be all fine.
64 let _twice = (ONCE_INIT, ONCE_INIT);
65 let _ref_twice = &(ONCE_INIT, ONCE_INIT);
66 let _ref_once = &(ONCE_INIT, ONCE_INIT).0;
67 let _array_twice = [ONCE_INIT, ONCE_INIT];
68 let _ref_array_twice = &[ONCE_INIT, ONCE_INIT];
69 let _ref_array_once = &[ONCE_INIT, ONCE_INIT][0];
71 // referencing projection is still bad.
72 let _ = &ATOMIC_TUPLE; //~ ERROR interior mutability
73 let _ = &ATOMIC_TUPLE.0; //~ ERROR interior mutability
74 let _ = &(&&&&ATOMIC_TUPLE).0; //~ ERROR interior mutability
75 let _ = &ATOMIC_TUPLE.0[0]; //~ ERROR interior mutability
76 let _ = ATOMIC_TUPLE.0[0].load(Ordering::SeqCst); //~ ERROR interior mutability
77 let _ = &*ATOMIC_TUPLE.1;
78 let _ = &ATOMIC_TUPLE.2;
79 let _ = (&&&&ATOMIC_TUPLE).0;
80 let _ = (&&&&ATOMIC_TUPLE).2;
81 let _ = ATOMIC_TUPLE.0;
82 let _ = ATOMIC_TUPLE.0[0]; //~ ERROR interior mutability
83 let _ = ATOMIC_TUPLE.1.into_iter();
84 let _ = ATOMIC_TUPLE.2;
85 let _ = &{ ATOMIC_TUPLE };
87 CELL.set(2); //~ ERROR interior mutability
88 assert_eq!(CELL.get(), 6); //~ ERROR interior mutability
90 assert_eq!(INTEGER, 8);
91 assert!(STRING.is_empty());
94 a.store(4, Ordering::SeqCst);
95 assert_eq!(a.load(Ordering::SeqCst), 4);
97 STATIC_TUPLE.0.store(3, Ordering::SeqCst);
98 assert_eq!(STATIC_TUPLE.0.load(Ordering::SeqCst), 3);
99 assert!(STATIC_TUPLE.1.is_empty());
101 assert_eq!(NO_ANN.to_string(), "70"); // should never lint this.