]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/declare_interior_mutable_const.rs
Rollup merge of #71633 - a1phyr:infallible_error, r=dtolnay
[rust.git] / src / tools / clippy / 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 trait Trait<T>: Copy {
38     type NonCopyType;
39
40     const ATOMIC: AtomicUsize; //~ ERROR interior mutable
41     const INTEGER: u64;
42     const STRING: String;
43     const SELF: Self; // (no error)
44     const INPUT: T;
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`
50
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
55 }
56
57 trait Trait2 {
58     type CopyType: Copy;
59
60     const SELF_2: Self;
61     //~^ ERROR interior mutable
62     //~| HELP consider requiring `Self` to be `Copy`
63     const ASSOC_2: Self::CopyType; // (no error)
64 }
65
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;
69
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;
76 }
77
78 struct Local<T, U>(T, U);
79
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;
91 }
92
93 fn main() {}