]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/borrow_interior_mutable_const.rs
Auto merge of #75974 - SkiFire13:peekmut-opt-sift, r=LukasKalbertodt
[rust.git] / src / tools / clippy / tests / ui / borrow_interior_mutable_const.rs
1 #![warn(clippy::borrow_interior_mutable_const)]
2 #![allow(clippy::declare_interior_mutable_const, clippy::ref_in_deref)]
3 #![allow(const_item_mutation)]
4
5 use std::borrow::Cow;
6 use std::cell::{Cell, UnsafeCell};
7 use std::fmt::Display;
8 use std::sync::atomic::{AtomicUsize, Ordering};
9 use std::sync::Once;
10
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();
21
22 trait Trait<T>: Copy {
23     type NonCopyType;
24
25     const ATOMIC: AtomicUsize;
26 }
27
28 impl Trait<u32> for u64 {
29     type NonCopyType = u16;
30
31     const ATOMIC: AtomicUsize = AtomicUsize::new(9);
32 }
33
34 // This is just a pointer that can be safely dereferended,
35 // it's semantically the same as `&'static T`;
36 // but it isn't allowed to make a static reference from an arbitrary integer value at the moment.
37 // For more information, please see the issue #5918.
38 pub struct StaticRef<T> {
39     ptr: *const T,
40 }
41
42 impl<T> StaticRef<T> {
43     /// Create a new `StaticRef` from a raw pointer
44     ///
45     /// ## Safety
46     ///
47     /// Callers must pass in a reference to statically allocated memory which
48     /// does not overlap with other values.
49     pub const unsafe fn new(ptr: *const T) -> StaticRef<T> {
50         StaticRef { ptr }
51     }
52 }
53
54 impl<T> std::ops::Deref for StaticRef<T> {
55     type Target = T;
56
57     fn deref(&self) -> &'static T {
58         unsafe { &*self.ptr }
59     }
60 }
61
62 // use a tuple to make sure referencing a field behind a pointer isn't linted.
63 const CELL_REF: StaticRef<(UnsafeCell<u32>,)> = unsafe { StaticRef::new(std::ptr::null()) };
64
65 fn main() {
66     ATOMIC.store(1, Ordering::SeqCst); //~ ERROR interior mutability
67     assert_eq!(ATOMIC.load(Ordering::SeqCst), 5); //~ ERROR interior mutability
68
69     let _once = ONCE_INIT;
70     let _once_ref = &ONCE_INIT; //~ ERROR interior mutability
71     let _once_ref_2 = &&ONCE_INIT; //~ ERROR interior mutability
72     let _once_ref_4 = &&&&ONCE_INIT; //~ ERROR interior mutability
73     let _once_mut = &mut ONCE_INIT; //~ ERROR interior mutability
74     let _atomic_into_inner = ATOMIC.into_inner();
75     // these should be all fine.
76     let _twice = (ONCE_INIT, ONCE_INIT);
77     let _ref_twice = &(ONCE_INIT, ONCE_INIT);
78     let _ref_once = &(ONCE_INIT, ONCE_INIT).0;
79     let _array_twice = [ONCE_INIT, ONCE_INIT];
80     let _ref_array_twice = &[ONCE_INIT, ONCE_INIT];
81     let _ref_array_once = &[ONCE_INIT, ONCE_INIT][0];
82
83     // referencing projection is still bad.
84     let _ = &ATOMIC_TUPLE; //~ ERROR interior mutability
85     let _ = &ATOMIC_TUPLE.0; //~ ERROR interior mutability
86     let _ = &(&&&&ATOMIC_TUPLE).0; //~ ERROR interior mutability
87     let _ = &ATOMIC_TUPLE.0[0]; //~ ERROR interior mutability
88     let _ = ATOMIC_TUPLE.0[0].load(Ordering::SeqCst); //~ ERROR interior mutability
89     let _ = &*ATOMIC_TUPLE.1; //~ ERROR interior mutability
90     let _ = &ATOMIC_TUPLE.2;
91     let _ = (&&&&ATOMIC_TUPLE).0;
92     let _ = (&&&&ATOMIC_TUPLE).2;
93     let _ = ATOMIC_TUPLE.0;
94     let _ = ATOMIC_TUPLE.0[0]; //~ ERROR interior mutability
95     let _ = ATOMIC_TUPLE.1.into_iter();
96     let _ = ATOMIC_TUPLE.2;
97     let _ = &{ ATOMIC_TUPLE };
98
99     CELL.set(2); //~ ERROR interior mutability
100     assert_eq!(CELL.get(), 6); //~ ERROR interior mutability
101
102     assert_eq!(INTEGER, 8);
103     assert!(STRING.is_empty());
104
105     let a = ATOMIC;
106     a.store(4, Ordering::SeqCst);
107     assert_eq!(a.load(Ordering::SeqCst), 4);
108
109     STATIC_TUPLE.0.store(3, Ordering::SeqCst);
110     assert_eq!(STATIC_TUPLE.0.load(Ordering::SeqCst), 3);
111     assert!(STATIC_TUPLE.1.is_empty());
112
113     u64::ATOMIC.store(5, Ordering::SeqCst); //~ ERROR interior mutability
114     assert_eq!(u64::ATOMIC.load(Ordering::SeqCst), 9); //~ ERROR interior mutability
115
116     assert_eq!(NO_ANN.to_string(), "70"); // should never lint this.
117
118     let _ = &CELL_REF.0;
119 }