]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/borrow_interior_mutable_const/others.rs
Preparing for merge from rustc
[rust.git] / src / tools / clippy / tests / ui / borrow_interior_mutable_const / others.rs
1 #![warn(clippy::borrow_interior_mutable_const)]
2 #![allow(clippy::declare_interior_mutable_const, clippy::needless_borrow)]
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 // 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> {
27     ptr: *const T,
28 }
29
30 impl<T> StaticRef<T> {
31     /// Create a new `StaticRef` from a raw pointer
32     ///
33     /// ## Safety
34     ///
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> {
38         StaticRef { ptr }
39     }
40 }
41
42 impl<T> std::ops::Deref for StaticRef<T> {
43     type Target = T;
44
45     fn deref(&self) -> &'static T {
46         unsafe { &*self.ptr }
47     }
48 }
49
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()) };
52
53 fn main() {
54     ATOMIC.store(1, Ordering::SeqCst); //~ ERROR interior mutability
55     assert_eq!(ATOMIC.load(Ordering::SeqCst), 5); //~ ERROR interior mutability
56
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];
70
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 };
86
87     CELL.set(2); //~ ERROR interior mutability
88     assert_eq!(CELL.get(), 6); //~ ERROR interior mutability
89
90     assert_eq!(INTEGER, 8);
91     assert!(STRING.is_empty());
92
93     let a = ATOMIC;
94     a.store(4, Ordering::SeqCst);
95     assert_eq!(a.load(Ordering::SeqCst), 4);
96
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());
100
101     assert_eq!(NO_ANN.to_string(), "70"); // should never lint this.
102
103     let _ = &CELL_REF.0;
104 }