1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
10 #![feature(const_string_new, const_vec_new)]
11 #![allow(clippy::ref_in_deref, dead_code)]
15 use std::fmt::Display;
16 use std::sync::atomic::{AtomicUsize, Ordering, ATOMIC_USIZE_INIT};
19 const ATOMIC: AtomicUsize = AtomicUsize::new(5); //~ ERROR interior mutable
20 const CELL: Cell<usize> = Cell::new(6); //~ ERROR interior mutable
21 const ATOMIC_TUPLE: ([AtomicUsize; 1], Vec<AtomicUsize>, u8) = ([ATOMIC], Vec::new(), 7);
22 //~^ ERROR interior mutable
24 macro_rules! declare_const {
25 ($name:ident: $ty:ty = $e:expr) => {
26 const $name: $ty = $e;
29 declare_const!(_ONCE: Once = Once::new()); //~ ERROR interior mutable
31 // const ATOMIC_REF: &AtomicUsize = &AtomicUsize::new(7); // This will simply trigger E0492.
33 const INTEGER: u8 = 8;
34 const STRING: String = String::new();
35 const STR: &str = "012345";
36 const COW: Cow<str> = Cow::Borrowed("abcdef");
37 //^ note: a const item of Cow is used in the `postgres` package.
39 const NO_ANN: &Display = &70;
41 static STATIC_TUPLE: (AtomicUsize, String) = (ATOMIC, STRING);
42 //^ there should be no lints on this line
44 #[allow(clippy::declare_interior_mutable_const)]
45 const ONCE_INIT: Once = Once::new();
47 trait Trait<T>: Copy {
50 const ATOMIC: AtomicUsize; //~ ERROR interior mutable
53 const SELF: Self; // (no error)
55 //~^ ERROR interior mutable
56 //~| HELP consider requiring `T` to be `Copy`
57 const ASSOC: Self::NonCopyType;
58 //~^ ERROR interior mutable
59 //~| HELP consider requiring `<Self as Trait<T>>::NonCopyType` to be `Copy`
61 const AN_INPUT: T = Self::INPUT;
62 //~^ ERROR interior mutable
63 //~| ERROR consider requiring `T` to be `Copy`
64 declare_const!(ANOTHER_INPUT: T = Self::INPUT); //~ ERROR interior mutable
71 //~^ ERROR interior mutable
72 //~| HELP consider requiring `Self` to be `Copy`
73 const ASSOC_2: Self::CopyType; // (no error)
76 // we don't lint impl of traits, because an impl has no power to change the interface.
77 impl Trait<u32> for u64 {
78 type NonCopyType = u16;
80 const ATOMIC: AtomicUsize = AtomicUsize::new(9);
81 const INTEGER: u64 = 10;
82 const STRING: String = String::new();
83 const SELF: Self = 11;
84 const INPUT: u32 = 12;
85 const ASSOC: Self::NonCopyType = 13;
88 struct Local<T, U>(T, U);
90 impl<T: Trait2 + Trait<u32>, U: Trait2> Local<T, U> {
91 const ASSOC_3: AtomicUsize = AtomicUsize::new(14); //~ ERROR interior mutable
92 const COW: Cow<'static, str> = Cow::Borrowed("tuvwxy");
93 const T_SELF: T = T::SELF_2;
94 const U_SELF: U = U::SELF_2;
95 //~^ ERROR interior mutable
96 //~| HELP consider requiring `U` to be `Copy`
97 const T_ASSOC: T::NonCopyType = T::ASSOC;
98 //~^ ERROR interior mutable
99 //~| HELP consider requiring `<T as Trait<u32>>::NonCopyType` to be `Copy`
100 const U_ASSOC: U::CopyType = U::ASSOC_2;
104 ATOMIC.store(1, Ordering::SeqCst); //~ ERROR interior mutability
105 assert_eq!(ATOMIC.load(Ordering::SeqCst), 5); //~ ERROR interior mutability
107 ATOMIC_USIZE_INIT.store(2, Ordering::SeqCst); //~ ERROR interior mutability
108 assert_eq!(ATOMIC_USIZE_INIT.load(Ordering::SeqCst), 0); //~ ERROR interior mutability
110 let _once = ONCE_INIT;
111 let _once_ref = &ONCE_INIT; //~ ERROR interior mutability
112 let _once_ref_2 = &&ONCE_INIT; //~ ERROR interior mutability
113 let _once_ref_4 = &&&&ONCE_INIT; //~ ERROR interior mutability
114 let _once_mut = &mut ONCE_INIT; //~ ERROR interior mutability
115 let _atomic_into_inner = ATOMIC.into_inner();
116 // these should be all fine.
117 let _twice = (ONCE_INIT, ONCE_INIT);
118 let _ref_twice = &(ONCE_INIT, ONCE_INIT);
119 let _ref_once = &(ONCE_INIT, ONCE_INIT).0;
120 let _array_twice = [ONCE_INIT, ONCE_INIT];
121 let _ref_array_twice = &[ONCE_INIT, ONCE_INIT];
122 let _ref_array_once = &[ONCE_INIT, ONCE_INIT][0];
124 // referencing projection is still bad.
125 let _ = &ATOMIC_TUPLE; //~ ERROR interior mutability
126 let _ = &ATOMIC_TUPLE.0; //~ ERROR interior mutability
127 let _ = &(&&&&ATOMIC_TUPLE).0; //~ ERROR interior mutability
128 let _ = &ATOMIC_TUPLE.0[0]; //~ ERROR interior mutability
129 let _ = ATOMIC_TUPLE.0[0].load(Ordering::SeqCst); //~ ERROR interior mutability
130 let _ = &*ATOMIC_TUPLE.1; //~ ERROR interior mutability
131 let _ = &ATOMIC_TUPLE.2;
132 let _ = (&&&&ATOMIC_TUPLE).0;
133 let _ = (&&&&ATOMIC_TUPLE).2;
134 let _ = ATOMIC_TUPLE.0;
135 let _ = ATOMIC_TUPLE.0[0]; //~ ERROR interior mutability
136 let _ = ATOMIC_TUPLE.1.into_iter();
137 let _ = ATOMIC_TUPLE.2;
138 let _ = &{ ATOMIC_TUPLE };
140 CELL.set(2); //~ ERROR interior mutability
141 assert_eq!(CELL.get(), 6); //~ ERROR interior mutability
143 assert_eq!(INTEGER, 8);
144 assert!(STRING.is_empty());
147 a.store(4, Ordering::SeqCst);
148 assert_eq!(a.load(Ordering::SeqCst), 4);
150 STATIC_TUPLE.0.store(3, Ordering::SeqCst);
151 assert_eq!(STATIC_TUPLE.0.load(Ordering::SeqCst), 3);
152 assert!(STATIC_TUPLE.1.is_empty());
154 u64::ATOMIC.store(5, Ordering::SeqCst); //~ ERROR interior mutability
155 assert_eq!(u64::ATOMIC.load(Ordering::SeqCst), 9); //~ ERROR interior mutability
157 assert_eq!(NO_ANN.to_string(), "70"); // should never lint this.