]> git.lizzy.rs Git - rust.git/blob - tests/ui/declare_interior_mutable_const.rs
Change the criteria of `interior_mutable_const`
[rust.git] / tests / ui / declare_interior_mutable_const.rs
1 #![warn(clippy::declare_interior_mutable_const)]
2
3 use std::borrow::Cow;
4 use std::cell::Cell;
5 use std::fmt::Display;
6 use std::sync::atomic::AtomicUsize;
7 use std::sync::Once;
8
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
13
14 macro_rules! declare_const {
15     ($name:ident: $ty:ty = $e:expr) => {
16         const $name: $ty = $e;
17     };
18 }
19 declare_const!(_ONCE: Once = Once::new()); //~ ERROR interior mutable
20
21 // const ATOMIC_REF: &AtomicUsize = &AtomicUsize::new(7); // This will simply trigger E0492.
22
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.
28
29 const NO_ANN: &dyn Display = &70;
30
31 static STATIC_TUPLE: (AtomicUsize, String) = (ATOMIC, STRING);
32 //^ there should be no lints on this line
33
34 #[allow(clippy::declare_interior_mutable_const)]
35 const ONCE_INIT: Once = Once::new();
36
37 struct Wrapper<T>(T);
38
39 trait Trait<T: Trait2<AssocType5 = AtomicUsize>> {
40     type AssocType;
41     type AssocType2;
42     type AssocType3;
43
44     const ATOMIC: AtomicUsize; //~ ERROR interior mutable
45     const INTEGER: u64;
46     const STRING: String;
47     const SELF: Self;
48     const INPUT: T;
49     const INPUT_ASSOC: T::AssocType4;
50     const INPUT_ASSOC_2: T::AssocType5; //~ ERROR interior mutable
51     const ASSOC: Self::AssocType;
52     const ASSOC_2: Self::AssocType2;
53     const WRAPPED_ASSOC_2: Wrapper<Self::AssocType2>;
54     const WRAPPED_ASSOC_3: Wrapper<Self::AssocType3>;
55
56     const AN_INPUT: T = Self::INPUT;
57     declare_const!(ANOTHER_INPUT: T = Self::INPUT);
58     declare_const!(ANOTHER_ATOMIC: AtomicUsize = Self::ATOMIC); //~ ERROR interior mutable
59 }
60
61 trait Trait2 {
62     type AssocType4;
63     type AssocType5;
64
65     const SELF_2: Self;
66     const ASSOC_4: Self::AssocType4;
67 }
68
69 impl<T: Trait2<AssocType5 = AtomicUsize>> Trait<T> for u64 {
70     type AssocType = u16;
71     type AssocType2 = AtomicUsize;
72     type AssocType3 = T;
73
74     const ATOMIC: AtomicUsize = AtomicUsize::new(9);
75     const INTEGER: u64 = 10;
76     const STRING: String = String::new();
77     const SELF: Self = 11;
78     const INPUT: T = T::SELF_2;
79     const INPUT_ASSOC: T::AssocType4 = T::ASSOC_4;
80     const INPUT_ASSOC_2: T::AssocType5 = AtomicUsize::new(16);
81     const ASSOC: Self::AssocType = 13;
82     const ASSOC_2: Self::AssocType2 = AtomicUsize::new(15); //~ ERROR interior mutable
83     const WRAPPED_ASSOC_2: Wrapper<Self::AssocType2> = Wrapper(AtomicUsize::new(16)); //~ ERROR interior mutable
84     const WRAPPED_ASSOC_3: Wrapper<Self::AssocType3> = Wrapper(T::SELF_2);
85 }
86
87 struct Local<T, U>(T, U);
88
89 impl<T: Trait<U>, U: Trait2<AssocType5 = AtomicUsize>> Local<T, U> {
90     const ASSOC_5: AtomicUsize = AtomicUsize::new(14); //~ ERROR interior mutable
91     const COW: Cow<'static, str> = Cow::Borrowed("tuvwxy");
92     const U_SELF: U = U::SELF_2;
93     const T_ASSOC: T::AssocType = T::ASSOC;
94     const U_ASSOC: U::AssocType5 = AtomicUsize::new(17); //~ ERROR interior mutable
95 }
96
97 fn main() {}