]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/tests/ui/borrow_interior_mutable_const/others.rs
Update upstream source from tag 'upstream/1.52.1+dfsg1'
[rustc.git] / src / tools / clippy / tests / ui / borrow_interior_mutable_const / others.rs
CommitLineData
f20569fa
XL
1#![warn(clippy::borrow_interior_mutable_const)]
2#![allow(clippy::declare_interior_mutable_const, clippy::ref_in_deref)]
3#![allow(const_item_mutation)]
4
5use std::borrow::Cow;
6use std::cell::{Cell, UnsafeCell};
7use std::fmt::Display;
8use std::sync::atomic::{AtomicUsize, Ordering};
9use std::sync::Once;
10
11const ATOMIC: AtomicUsize = AtomicUsize::new(5);
12const CELL: Cell<usize> = Cell::new(6);
13const ATOMIC_TUPLE: ([AtomicUsize; 1], Vec<AtomicUsize>, u8) = ([ATOMIC], Vec::new(), 7);
14const INTEGER: u8 = 8;
15const STRING: String = String::new();
16const STR: &str = "012345";
17const COW: Cow<str> = Cow::Borrowed("abcdef");
18const NO_ANN: &dyn Display = &70;
19static STATIC_TUPLE: (AtomicUsize, String) = (ATOMIC, STRING);
20const 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.
26pub struct StaticRef<T> {
27 ptr: *const T,
28}
29
30impl<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
42impl<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.
51const CELL_REF: StaticRef<(UnsafeCell<u32>,)> = unsafe { StaticRef::new(std::ptr::null()) };
52
53fn 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}