]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/borrow_interior_mutable_const.rs
Rollup merge of #78730 - kornelski:not-inverse, r=Dylan-DPC
[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> {
23     type AssocType;
24
25     const ATOMIC: AtomicUsize;
26     const INPUT: T;
27     const ASSOC: Self::AssocType;
28
29     fn function() {
30         let _ = &Self::INPUT;
31         let _ = &Self::ASSOC;
32     }
33 }
34
35 impl Trait<u32> for u64 {
36     type AssocType = AtomicUsize;
37
38     const ATOMIC: AtomicUsize = AtomicUsize::new(9);
39     const INPUT: u32 = 10;
40     const ASSOC: Self::AssocType = AtomicUsize::new(11);
41
42     fn function() {
43         let _ = &Self::INPUT;
44         let _ = &Self::ASSOC; //~ ERROR interior mutability
45     }
46 }
47
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> {
53     ptr: *const T,
54 }
55
56 impl<T> StaticRef<T> {
57     /// Create a new `StaticRef` from a raw pointer
58     ///
59     /// ## Safety
60     ///
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> {
64         StaticRef { ptr }
65     }
66 }
67
68 impl<T> std::ops::Deref for StaticRef<T> {
69     type Target = T;
70
71     fn deref(&self) -> &'static T {
72         unsafe { &*self.ptr }
73     }
74 }
75
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()) };
78
79 fn main() {
80     ATOMIC.store(1, Ordering::SeqCst); //~ ERROR interior mutability
81     assert_eq!(ATOMIC.load(Ordering::SeqCst), 5); //~ ERROR interior mutability
82
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];
96
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 };
112
113     CELL.set(2); //~ ERROR interior mutability
114     assert_eq!(CELL.get(), 6); //~ ERROR interior mutability
115
116     assert_eq!(INTEGER, 8);
117     assert!(STRING.is_empty());
118
119     let a = ATOMIC;
120     a.store(4, Ordering::SeqCst);
121     assert_eq!(a.load(Ordering::SeqCst), 4);
122
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());
126
127     u64::ATOMIC.store(5, Ordering::SeqCst); //~ ERROR interior mutability
128     assert_eq!(u64::ATOMIC.load(Ordering::SeqCst), 9); //~ ERROR interior mutability
129
130     assert_eq!(NO_ANN.to_string(), "70"); // should never lint this.
131
132     let _ = &CELL_REF.0;
133 }