1 #![warn(clippy::declare_interior_mutable_const)]
6 use std::sync::atomic::AtomicUsize;
9 const ATOMIC: AtomicUsize = AtomicUsize::new(5); //~ ERROR interior mutable
10 const CELL: Cell<usize> = Cell::new(6); //~ ERROR interior mutable
11 const ATOMIC_TUPLE: ([AtomicUsize; 1], Vec<AtomicUsize>, u8) = ([ATOMIC], Vec::new(), 7);
12 //~^ ERROR interior mutable
14 macro_rules! declare_const {
15 ($name:ident: $ty:ty = $e:expr) => {
16 const $name: $ty = $e;
19 declare_const!(_ONCE: Once = Once::new()); //~ ERROR interior mutable
21 // const ATOMIC_REF: &AtomicUsize = &AtomicUsize::new(7); // This will simply trigger E0492.
23 const INTEGER: u8 = 8;
24 const STRING: String = String::new();
25 const STR: &str = "012345";
26 const COW: Cow<str> = Cow::Borrowed("abcdef");
27 //^ note: a const item of Cow is used in the `postgres` package.
29 const NO_ANN: &dyn Display = &70;
31 static STATIC_TUPLE: (AtomicUsize, String) = (ATOMIC, STRING);
32 //^ there should be no lints on this line
34 #[allow(clippy::declare_interior_mutable_const)]
35 const ONCE_INIT: Once = Once::new();
37 trait Trait<T>: Copy {
40 const ATOMIC: AtomicUsize; //~ ERROR interior mutable
43 const SELF: Self; // (no error)
45 //~^ ERROR interior mutable
46 //~| HELP consider requiring `T` to be `Copy`
47 const ASSOC: Self::NonCopyType;
48 //~^ ERROR interior mutable
49 //~| HELP consider requiring `<Self as Trait<T>>::NonCopyType` to be `Copy`
51 const AN_INPUT: T = Self::INPUT;
52 //~^ ERROR interior mutable
53 //~| ERROR consider requiring `T` to be `Copy`
54 declare_const!(ANOTHER_INPUT: T = Self::INPUT); //~ ERROR interior mutable
61 //~^ ERROR interior mutable
62 //~| HELP consider requiring `Self` to be `Copy`
63 const ASSOC_2: Self::CopyType; // (no error)
66 // we don't lint impl of traits, because an impl has no power to change the interface.
67 impl Trait<u32> for u64 {
68 type NonCopyType = u16;
70 const ATOMIC: AtomicUsize = AtomicUsize::new(9);
71 const INTEGER: u64 = 10;
72 const STRING: String = String::new();
73 const SELF: Self = 11;
74 const INPUT: u32 = 12;
75 const ASSOC: Self::NonCopyType = 13;
78 struct Local<T, U>(T, U);
80 impl<T: Trait2 + Trait<u32>, U: Trait2> Local<T, U> {
81 const ASSOC_3: AtomicUsize = AtomicUsize::new(14); //~ ERROR interior mutable
82 const COW: Cow<'static, str> = Cow::Borrowed("tuvwxy");
83 const T_SELF: T = T::SELF_2;
84 const U_SELF: U = U::SELF_2;
85 //~^ ERROR interior mutable
86 //~| HELP consider requiring `U` to be `Copy`
87 const T_ASSOC: T::NonCopyType = T::ASSOC;
88 //~^ ERROR interior mutable
89 //~| HELP consider requiring `<T as Trait<u32>>::NonCopyType` to be `Copy`
90 const U_ASSOC: U::CopyType = U::ASSOC_2;