]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/borrow_interior_mutable_const/traits.rs
Auto merge of #107843 - bjorn3:sync_cg_clif-2023-02-09, r=bjorn3
[rust.git] / src / tools / clippy / tests / ui / borrow_interior_mutable_const / traits.rs
1 #![warn(clippy::borrow_interior_mutable_const)]
2 #![allow(clippy::declare_interior_mutable_const)]
3
4 // this file replicates its `declare` counterpart. Please see it for more discussions.
5
6 use std::borrow::Cow;
7 use std::cell::Cell;
8 use std::sync::atomic::{AtomicUsize, Ordering};
9
10 trait ConcreteTypes {
11     const ATOMIC: AtomicUsize;
12     const STRING: String;
13
14     fn function() {
15         let _ = &Self::ATOMIC; //~ ERROR interior mutable
16         let _ = &Self::STRING;
17     }
18 }
19
20 impl ConcreteTypes for u64 {
21     const ATOMIC: AtomicUsize = AtomicUsize::new(9);
22     const STRING: String = String::new();
23
24     fn function() {
25         // Lint this again since implementers can choose not to borrow it.
26         let _ = &Self::ATOMIC; //~ ERROR interior mutable
27         let _ = &Self::STRING;
28     }
29 }
30
31 // a helper trait used below
32 trait ConstDefault {
33     const DEFAULT: Self;
34 }
35
36 trait GenericTypes<T, U> {
37     const TO_REMAIN_GENERIC: T;
38     const TO_BE_CONCRETE: U;
39
40     fn function() {
41         let _ = &Self::TO_REMAIN_GENERIC;
42     }
43 }
44
45 impl<T: ConstDefault> GenericTypes<T, AtomicUsize> for Vec<T> {
46     const TO_REMAIN_GENERIC: T = T::DEFAULT;
47     const TO_BE_CONCRETE: AtomicUsize = AtomicUsize::new(11);
48
49     fn function() {
50         let _ = &Self::TO_REMAIN_GENERIC;
51         let _ = &Self::TO_BE_CONCRETE; //~ ERROR interior mutable
52     }
53 }
54
55 // a helper type used below
56 pub struct Wrapper<T>(T);
57
58 trait AssocTypes {
59     type ToBeFrozen;
60     type ToBeUnfrozen;
61     type ToBeGenericParam;
62
63     const TO_BE_FROZEN: Self::ToBeFrozen;
64     const TO_BE_UNFROZEN: Self::ToBeUnfrozen;
65     const WRAPPED_TO_BE_UNFROZEN: Wrapper<Self::ToBeUnfrozen>;
66     const WRAPPED_TO_BE_GENERIC_PARAM: Wrapper<Self::ToBeGenericParam>;
67
68     fn function() {
69         let _ = &Self::TO_BE_FROZEN;
70         let _ = &Self::WRAPPED_TO_BE_UNFROZEN;
71     }
72 }
73
74 impl<T: ConstDefault> AssocTypes for Vec<T> {
75     type ToBeFrozen = u16;
76     type ToBeUnfrozen = AtomicUsize;
77     type ToBeGenericParam = T;
78
79     const TO_BE_FROZEN: Self::ToBeFrozen = 12;
80     const TO_BE_UNFROZEN: Self::ToBeUnfrozen = AtomicUsize::new(13);
81     const WRAPPED_TO_BE_UNFROZEN: Wrapper<Self::ToBeUnfrozen> = Wrapper(AtomicUsize::new(14));
82     const WRAPPED_TO_BE_GENERIC_PARAM: Wrapper<Self::ToBeGenericParam> = Wrapper(T::DEFAULT);
83
84     fn function() {
85         let _ = &Self::TO_BE_FROZEN;
86         let _ = &Self::TO_BE_UNFROZEN; //~ ERROR interior mutable
87         let _ = &Self::WRAPPED_TO_BE_UNFROZEN; //~ ERROR interior mutable
88         let _ = &Self::WRAPPED_TO_BE_GENERIC_PARAM;
89     }
90 }
91
92 // a helper trait used below
93 trait AssocTypesHelper {
94     type NotToBeBounded;
95     type ToBeBounded;
96
97     const NOT_TO_BE_BOUNDED: Self::NotToBeBounded;
98 }
99
100 trait AssocTypesFromGenericParam<T>
101 where
102     T: AssocTypesHelper<ToBeBounded = AtomicUsize>,
103 {
104     const NOT_BOUNDED: T::NotToBeBounded;
105     const BOUNDED: T::ToBeBounded;
106
107     fn function() {
108         let _ = &Self::NOT_BOUNDED;
109         let _ = &Self::BOUNDED; //~ ERROR interior mutable
110     }
111 }
112
113 impl<T> AssocTypesFromGenericParam<T> for Vec<T>
114 where
115     T: AssocTypesHelper<ToBeBounded = AtomicUsize>,
116 {
117     const NOT_BOUNDED: T::NotToBeBounded = T::NOT_TO_BE_BOUNDED;
118     const BOUNDED: T::ToBeBounded = AtomicUsize::new(15);
119
120     fn function() {
121         let _ = &Self::NOT_BOUNDED;
122         let _ = &Self::BOUNDED; //~ ERROR interior mutable
123     }
124 }
125
126 trait SelfType: Sized {
127     const SELF: Self;
128     const WRAPPED_SELF: Option<Self>;
129
130     fn function() {
131         let _ = &Self::SELF;
132         let _ = &Self::WRAPPED_SELF;
133     }
134 }
135
136 impl SelfType for u64 {
137     const SELF: Self = 16;
138     const WRAPPED_SELF: Option<Self> = Some(20);
139
140     fn function() {
141         let _ = &Self::SELF;
142         let _ = &Self::WRAPPED_SELF;
143     }
144 }
145
146 impl SelfType for AtomicUsize {
147     const SELF: Self = AtomicUsize::new(17);
148     const WRAPPED_SELF: Option<Self> = Some(AtomicUsize::new(21));
149
150     fn function() {
151         let _ = &Self::SELF; //~ ERROR interior mutable
152         let _ = &Self::WRAPPED_SELF; //~ ERROR interior mutable
153     }
154 }
155
156 trait BothOfCellAndGeneric<T> {
157     const DIRECT: Cell<T>;
158     const INDIRECT: Cell<*const T>;
159
160     fn function() {
161         let _ = &Self::DIRECT;
162         let _ = &Self::INDIRECT; //~ ERROR interior mutable
163     }
164 }
165
166 impl<T: ConstDefault> BothOfCellAndGeneric<T> for Vec<T> {
167     const DIRECT: Cell<T> = Cell::new(T::DEFAULT);
168     const INDIRECT: Cell<*const T> = Cell::new(std::ptr::null());
169
170     fn function() {
171         let _ = &Self::DIRECT;
172         let _ = &Self::INDIRECT; //~ ERROR interior mutable
173     }
174 }
175
176 struct Local<T>(T);
177
178 impl<T> Local<T>
179 where
180     T: ConstDefault + AssocTypesHelper<ToBeBounded = AtomicUsize>,
181 {
182     const ATOMIC: AtomicUsize = AtomicUsize::new(18);
183     const COW: Cow<'static, str> = Cow::Borrowed("tuvwxy");
184
185     const GENERIC_TYPE: T = T::DEFAULT;
186
187     const ASSOC_TYPE: T::NotToBeBounded = T::NOT_TO_BE_BOUNDED;
188     const BOUNDED_ASSOC_TYPE: T::ToBeBounded = AtomicUsize::new(19);
189
190     fn function() {
191         let _ = &Self::ATOMIC; //~ ERROR interior mutable
192         let _ = &Self::COW;
193         let _ = &Self::GENERIC_TYPE;
194         let _ = &Self::ASSOC_TYPE;
195         let _ = &Self::BOUNDED_ASSOC_TYPE; //~ ERROR interior mutable
196     }
197 }
198
199 fn main() {
200     u64::ATOMIC.store(5, Ordering::SeqCst); //~ ERROR interior mutability
201     assert_eq!(u64::ATOMIC.load(Ordering::SeqCst), 9); //~ ERROR interior mutability
202 }