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.
11 #![feature(tool_lints)]
13 #![feature(const_string_new, const_vec_new)]
14 #![allow(clippy::ref_in_deref, dead_code)]
16 use std::sync::atomic::{ATOMIC_USIZE_INIT, AtomicUsize, Ordering};
20 use std::fmt::Display;
22 const ATOMIC: AtomicUsize = AtomicUsize::new(5); //~ ERROR interior mutable
23 const CELL: Cell<usize> = Cell::new(6); //~ ERROR interior mutable
24 const ATOMIC_TUPLE: ([AtomicUsize; 1], Vec<AtomicUsize>, u8) = ([ATOMIC], Vec::new(), 7);
25 //~^ ERROR interior mutable
27 macro_rules! declare_const {
28 ($name:ident: $ty:ty = $e:expr) => { const $name: $ty = $e; };
30 declare_const!(_ONCE: Once = Once::new()); //~ ERROR interior mutable
32 // const ATOMIC_REF: &AtomicUsize = &AtomicUsize::new(7); // This will simply trigger E0492.
34 const INTEGER: u8 = 8;
35 const STRING: String = String::new();
36 const STR: &str = "012345";
37 const COW: Cow<str> = Cow::Borrowed("abcdef");
38 //^ note: a const item of Cow is used in the `postgres` package.
40 const NO_ANN: &Display = &70;
42 static STATIC_TUPLE: (AtomicUsize, String) = (ATOMIC, STRING);
43 //^ there should be no lints on this line
45 #[allow(clippy::declare_interior_mutable_const)]
46 const ONCE_INIT: Once = Once::new();
48 trait Trait<T>: Copy {
51 const ATOMIC: AtomicUsize; //~ ERROR interior mutable
54 const SELF: Self; // (no error)
56 //~^ ERROR interior mutable
57 //~| HELP consider requiring `T` to be `Copy`
58 const ASSOC: Self::NonCopyType;
59 //~^ ERROR interior mutable
60 //~| HELP consider requiring `<Self as Trait<T>>::NonCopyType` to be `Copy`
62 const AN_INPUT: T = Self::INPUT;
63 //~^ ERROR interior mutable
64 //~| ERROR consider requiring `T` to be `Copy`
65 declare_const!(ANOTHER_INPUT: T = Self::INPUT); //~ ERROR interior mutable
72 //~^ ERROR interior mutable
73 //~| HELP consider requiring `Self` to be `Copy`
74 const ASSOC_2: Self::CopyType; // (no error)
77 // we don't lint impl of traits, because an impl has no power to change the interface.
78 impl Trait<u32> for u64 {
79 type NonCopyType = u16;
81 const ATOMIC: AtomicUsize = AtomicUsize::new(9);
82 const INTEGER: u64 = 10;
83 const STRING: String = String::new();
84 const SELF: Self = 11;
85 const INPUT: u32 = 12;
86 const ASSOC: Self::NonCopyType = 13;
89 struct Local<T, U>(T, U);
91 impl<T: Trait2 + Trait<u32>, U: Trait2> Local<T, U> {
92 const ASSOC_3: AtomicUsize = AtomicUsize::new(14); //~ ERROR interior mutable
93 const COW: Cow<'static, str> = Cow::Borrowed("tuvwxy");
94 const T_SELF: T = T::SELF_2;
95 const U_SELF: U = U::SELF_2;
96 //~^ ERROR interior mutable
97 //~| HELP consider requiring `U` to be `Copy`
98 const T_ASSOC: T::NonCopyType = T::ASSOC;
99 //~^ ERROR interior mutable
100 //~| HELP consider requiring `<T as Trait<u32>>::NonCopyType` to be `Copy`
101 const U_ASSOC: U::CopyType = U::ASSOC_2;
105 ATOMIC.store(1, Ordering::SeqCst); //~ ERROR interior mutability
106 assert_eq!(ATOMIC.load(Ordering::SeqCst), 5); //~ ERROR interior mutability
108 ATOMIC_USIZE_INIT.store(2, Ordering::SeqCst); //~ ERROR interior mutability
109 assert_eq!(ATOMIC_USIZE_INIT.load(Ordering::SeqCst), 0); //~ ERROR interior mutability
111 let _once = ONCE_INIT;
112 let _once_ref = &ONCE_INIT; //~ ERROR interior mutability
113 let _once_ref_2 = &&ONCE_INIT; //~ ERROR interior mutability
114 let _once_ref_4 = &&&&ONCE_INIT; //~ ERROR interior mutability
115 let _once_mut = &mut ONCE_INIT; //~ ERROR interior mutability
116 let _atomic_into_inner = ATOMIC.into_inner();
117 // these should be all fine.
118 let _twice = (ONCE_INIT, ONCE_INIT);
119 let _ref_twice = &(ONCE_INIT, ONCE_INIT);
120 let _ref_once = &(ONCE_INIT, ONCE_INIT).0;
121 let _array_twice = [ONCE_INIT, ONCE_INIT];
122 let _ref_array_twice = &[ONCE_INIT, ONCE_INIT];
123 let _ref_array_once = &[ONCE_INIT, ONCE_INIT][0];
125 // referencing projection is still bad.
126 let _ = &ATOMIC_TUPLE; //~ ERROR interior mutability
127 let _ = &ATOMIC_TUPLE.0; //~ ERROR interior mutability
128 let _ = &(&&&&ATOMIC_TUPLE).0; //~ ERROR interior mutability
129 let _ = &ATOMIC_TUPLE.0[0]; //~ ERROR interior mutability
130 let _ = ATOMIC_TUPLE.0[0].load(Ordering::SeqCst); //~ ERROR interior mutability
131 let _ = &*ATOMIC_TUPLE.1; //~ ERROR interior mutability
132 let _ = &ATOMIC_TUPLE.2;
133 let _ = (&&&&ATOMIC_TUPLE).0;
134 let _ = (&&&&ATOMIC_TUPLE).2;
135 let _ = ATOMIC_TUPLE.0;
136 let _ = ATOMIC_TUPLE.0[0]; //~ ERROR interior mutability
137 let _ = ATOMIC_TUPLE.1.into_iter();
138 let _ = ATOMIC_TUPLE.2;
139 let _ = &{ATOMIC_TUPLE};
141 CELL.set(2); //~ ERROR interior mutability
142 assert_eq!(CELL.get(), 6); //~ ERROR interior mutability
144 assert_eq!(INTEGER, 8);
145 assert!(STRING.is_empty());
148 a.store(4, Ordering::SeqCst);
149 assert_eq!(a.load(Ordering::SeqCst), 4);
151 STATIC_TUPLE.0.store(3, Ordering::SeqCst);
152 assert_eq!(STATIC_TUPLE.0.load(Ordering::SeqCst), 3);
153 assert!(STATIC_TUPLE.1.is_empty());
155 u64::ATOMIC.store(5, Ordering::SeqCst); //~ ERROR interior mutability
156 assert_eq!(u64::ATOMIC.load(Ordering::SeqCst), 9); //~ ERROR interior mutability
158 assert_eq!(NO_ANN.to_string(), "70"); // should never lint this.