1 #![feature(const_string_new, const_vec_new)]
2 #![allow(clippy::ref_in_deref, dead_code)]
7 use std::sync::atomic::{AtomicUsize, Ordering};
10 const ATOMIC: AtomicUsize = AtomicUsize::new(5); //~ ERROR interior mutable
11 const CELL: Cell<usize> = Cell::new(6); //~ ERROR interior mutable
12 const ATOMIC_TUPLE: ([AtomicUsize; 1], Vec<AtomicUsize>, u8) = ([ATOMIC], Vec::new(), 7);
13 //~^ ERROR interior mutable
15 macro_rules! declare_const {
16 ($name:ident: $ty:ty = $e:expr) => {
17 const $name: $ty = $e;
20 declare_const!(_ONCE: Once = Once::new()); //~ ERROR interior mutable
22 // const ATOMIC_REF: &AtomicUsize = &AtomicUsize::new(7); // This will simply trigger E0492.
24 const INTEGER: u8 = 8;
25 const STRING: String = String::new();
26 const STR: &str = "012345";
27 const COW: Cow<str> = Cow::Borrowed("abcdef");
28 //^ note: a const item of Cow is used in the `postgres` package.
30 const NO_ANN: &Display = &70;
32 static STATIC_TUPLE: (AtomicUsize, String) = (ATOMIC, STRING);
33 //^ there should be no lints on this line
35 #[allow(clippy::declare_interior_mutable_const)]
36 const ONCE_INIT: Once = Once::new();
38 trait Trait<T>: Copy {
41 const ATOMIC: AtomicUsize; //~ ERROR interior mutable
44 const SELF: Self; // (no error)
46 //~^ ERROR interior mutable
47 //~| HELP consider requiring `T` to be `Copy`
48 const ASSOC: Self::NonCopyType;
49 //~^ ERROR interior mutable
50 //~| HELP consider requiring `<Self as Trait<T>>::NonCopyType` to be `Copy`
52 const AN_INPUT: T = Self::INPUT;
53 //~^ ERROR interior mutable
54 //~| ERROR consider requiring `T` to be `Copy`
55 declare_const!(ANOTHER_INPUT: T = Self::INPUT); //~ ERROR interior mutable
62 //~^ ERROR interior mutable
63 //~| HELP consider requiring `Self` to be `Copy`
64 const ASSOC_2: Self::CopyType; // (no error)
67 // we don't lint impl of traits, because an impl has no power to change the interface.
68 impl Trait<u32> for u64 {
69 type NonCopyType = u16;
71 const ATOMIC: AtomicUsize = AtomicUsize::new(9);
72 const INTEGER: u64 = 10;
73 const STRING: String = String::new();
74 const SELF: Self = 11;
75 const INPUT: u32 = 12;
76 const ASSOC: Self::NonCopyType = 13;
79 struct Local<T, U>(T, U);
81 impl<T: Trait2 + Trait<u32>, U: Trait2> Local<T, U> {
82 const ASSOC_3: AtomicUsize = AtomicUsize::new(14); //~ ERROR interior mutable
83 const COW: Cow<'static, str> = Cow::Borrowed("tuvwxy");
84 const T_SELF: T = T::SELF_2;
85 const U_SELF: U = U::SELF_2;
86 //~^ ERROR interior mutable
87 //~| HELP consider requiring `U` to be `Copy`
88 const T_ASSOC: T::NonCopyType = T::ASSOC;
89 //~^ ERROR interior mutable
90 //~| HELP consider requiring `<T as Trait<u32>>::NonCopyType` to be `Copy`
91 const U_ASSOC: U::CopyType = U::ASSOC_2;
95 ATOMIC.store(1, Ordering::SeqCst); //~ ERROR interior mutability
96 assert_eq!(ATOMIC.load(Ordering::SeqCst), 5); //~ ERROR interior mutability
98 let _once = ONCE_INIT;
99 let _once_ref = &ONCE_INIT; //~ ERROR interior mutability
100 let _once_ref_2 = &&ONCE_INIT; //~ ERROR interior mutability
101 let _once_ref_4 = &&&&ONCE_INIT; //~ ERROR interior mutability
102 let _once_mut = &mut ONCE_INIT; //~ ERROR interior mutability
103 let _atomic_into_inner = ATOMIC.into_inner();
104 // these should be all fine.
105 let _twice = (ONCE_INIT, ONCE_INIT);
106 let _ref_twice = &(ONCE_INIT, ONCE_INIT);
107 let _ref_once = &(ONCE_INIT, ONCE_INIT).0;
108 let _array_twice = [ONCE_INIT, ONCE_INIT];
109 let _ref_array_twice = &[ONCE_INIT, ONCE_INIT];
110 let _ref_array_once = &[ONCE_INIT, ONCE_INIT][0];
112 // referencing projection is still bad.
113 let _ = &ATOMIC_TUPLE; //~ ERROR interior mutability
114 let _ = &ATOMIC_TUPLE.0; //~ ERROR interior mutability
115 let _ = &(&&&&ATOMIC_TUPLE).0; //~ ERROR interior mutability
116 let _ = &ATOMIC_TUPLE.0[0]; //~ ERROR interior mutability
117 let _ = ATOMIC_TUPLE.0[0].load(Ordering::SeqCst); //~ ERROR interior mutability
118 let _ = &*ATOMIC_TUPLE.1; //~ ERROR interior mutability
119 let _ = &ATOMIC_TUPLE.2;
120 let _ = (&&&&ATOMIC_TUPLE).0;
121 let _ = (&&&&ATOMIC_TUPLE).2;
122 let _ = ATOMIC_TUPLE.0;
123 let _ = ATOMIC_TUPLE.0[0]; //~ ERROR interior mutability
124 let _ = ATOMIC_TUPLE.1.into_iter();
125 let _ = ATOMIC_TUPLE.2;
126 let _ = &{ ATOMIC_TUPLE };
128 CELL.set(2); //~ ERROR interior mutability
129 assert_eq!(CELL.get(), 6); //~ ERROR interior mutability
131 assert_eq!(INTEGER, 8);
132 assert!(STRING.is_empty());
135 a.store(4, Ordering::SeqCst);
136 assert_eq!(a.load(Ordering::SeqCst), 4);
138 STATIC_TUPLE.0.store(3, Ordering::SeqCst);
139 assert_eq!(STATIC_TUPLE.0.load(Ordering::SeqCst), 3);
140 assert!(STATIC_TUPLE.1.is_empty());
142 u64::ATOMIC.store(5, Ordering::SeqCst); //~ ERROR interior mutability
143 assert_eq!(u64::ATOMIC.load(Ordering::SeqCst), 9); //~ ERROR interior mutability
145 assert_eq!(NO_ANN.to_string(), "70"); // should never lint this.