]> git.lizzy.rs Git - rust.git/blob - tests/ui/borrow_interior_mutable_const.rs
iterate List by value
[rust.git] / 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
4 use std::borrow::Cow;
5 use std::cell::Cell;
6 use std::fmt::Display;
7 use std::sync::atomic::{AtomicUsize, Ordering};
8 use std::sync::Once;
9
10 const ATOMIC: AtomicUsize = AtomicUsize::new(5);
11 const CELL: Cell<usize> = Cell::new(6);
12 const ATOMIC_TUPLE: ([AtomicUsize; 1], Vec<AtomicUsize>, u8) = ([ATOMIC], Vec::new(), 7);
13 const INTEGER: u8 = 8;
14 const STRING: String = String::new();
15 const STR: &str = "012345";
16 const COW: Cow<str> = Cow::Borrowed("abcdef");
17 const NO_ANN: &dyn Display = &70;
18 static STATIC_TUPLE: (AtomicUsize, String) = (ATOMIC, STRING);
19 const ONCE_INIT: Once = Once::new();
20
21 trait Trait<T>: Copy {
22     type NonCopyType;
23
24     const ATOMIC: AtomicUsize;
25 }
26
27 impl Trait<u32> for u64 {
28     type NonCopyType = u16;
29
30     const ATOMIC: AtomicUsize = AtomicUsize::new(9);
31 }
32
33 fn main() {
34     ATOMIC.store(1, Ordering::SeqCst); //~ ERROR interior mutability
35     assert_eq!(ATOMIC.load(Ordering::SeqCst), 5); //~ ERROR interior mutability
36
37     let _once = ONCE_INIT;
38     let _once_ref = &ONCE_INIT; //~ ERROR interior mutability
39     let _once_ref_2 = &&ONCE_INIT; //~ ERROR interior mutability
40     let _once_ref_4 = &&&&ONCE_INIT; //~ ERROR interior mutability
41     let _once_mut = &mut ONCE_INIT; //~ ERROR interior mutability
42     let _atomic_into_inner = ATOMIC.into_inner();
43     // these should be all fine.
44     let _twice = (ONCE_INIT, ONCE_INIT);
45     let _ref_twice = &(ONCE_INIT, ONCE_INIT);
46     let _ref_once = &(ONCE_INIT, ONCE_INIT).0;
47     let _array_twice = [ONCE_INIT, ONCE_INIT];
48     let _ref_array_twice = &[ONCE_INIT, ONCE_INIT];
49     let _ref_array_once = &[ONCE_INIT, ONCE_INIT][0];
50
51     // referencing projection is still bad.
52     let _ = &ATOMIC_TUPLE; //~ ERROR interior mutability
53     let _ = &ATOMIC_TUPLE.0; //~ ERROR interior mutability
54     let _ = &(&&&&ATOMIC_TUPLE).0; //~ ERROR interior mutability
55     let _ = &ATOMIC_TUPLE.0[0]; //~ ERROR interior mutability
56     let _ = ATOMIC_TUPLE.0[0].load(Ordering::SeqCst); //~ ERROR interior mutability
57     let _ = &*ATOMIC_TUPLE.1; //~ ERROR interior mutability
58     let _ = &ATOMIC_TUPLE.2;
59     let _ = (&&&&ATOMIC_TUPLE).0;
60     let _ = (&&&&ATOMIC_TUPLE).2;
61     let _ = ATOMIC_TUPLE.0;
62     let _ = ATOMIC_TUPLE.0[0]; //~ ERROR interior mutability
63     let _ = ATOMIC_TUPLE.1.into_iter();
64     let _ = ATOMIC_TUPLE.2;
65     let _ = &{ ATOMIC_TUPLE };
66
67     CELL.set(2); //~ ERROR interior mutability
68     assert_eq!(CELL.get(), 6); //~ ERROR interior mutability
69
70     assert_eq!(INTEGER, 8);
71     assert!(STRING.is_empty());
72
73     let a = ATOMIC;
74     a.store(4, Ordering::SeqCst);
75     assert_eq!(a.load(Ordering::SeqCst), 4);
76
77     STATIC_TUPLE.0.store(3, Ordering::SeqCst);
78     assert_eq!(STATIC_TUPLE.0.load(Ordering::SeqCst), 3);
79     assert!(STATIC_TUPLE.1.is_empty());
80
81     u64::ATOMIC.store(5, Ordering::SeqCst); //~ ERROR interior mutability
82     assert_eq!(u64::ATOMIC.load(Ordering::SeqCst), 9); //~ ERROR interior mutability
83
84     assert_eq!(NO_ANN.to_string(), "70"); // should never lint this.
85 }