]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/borrow_interior_mutable_const/enums.rs
Auto merge of #107843 - bjorn3:sync_cg_clif-2023-02-09, r=bjorn3
[rust.git] / src / tools / clippy / tests / ui / borrow_interior_mutable_const / enums.rs
1 // aux-build:helper.rs
2
3 #![warn(clippy::borrow_interior_mutable_const)]
4 #![allow(clippy::declare_interior_mutable_const)]
5
6 // this file (mostly) replicates its `declare` counterpart. Please see it for more discussions.
7
8 extern crate helper;
9
10 use std::cell::Cell;
11 use std::sync::atomic::AtomicUsize;
12
13 enum OptionalCell {
14     Unfrozen(Cell<bool>),
15     Frozen,
16 }
17
18 const UNFROZEN_VARIANT: OptionalCell = OptionalCell::Unfrozen(Cell::new(true));
19 const FROZEN_VARIANT: OptionalCell = OptionalCell::Frozen;
20
21 fn borrow_optional_cell() {
22     let _ = &UNFROZEN_VARIANT; //~ ERROR interior mutability
23     let _ = &FROZEN_VARIANT;
24 }
25
26 trait AssocConsts {
27     const TO_BE_UNFROZEN_VARIANT: OptionalCell;
28     const TO_BE_FROZEN_VARIANT: OptionalCell;
29
30     const DEFAULTED_ON_UNFROZEN_VARIANT: OptionalCell = OptionalCell::Unfrozen(Cell::new(false));
31     const DEFAULTED_ON_FROZEN_VARIANT: OptionalCell = OptionalCell::Frozen;
32
33     fn function() {
34         // This is the "suboptimal behavior" mentioned in `is_value_unfrozen`
35         // caused by a similar reason to unfrozen types without any default values
36         // get linted even if it has frozen variants'.
37         let _ = &Self::TO_BE_FROZEN_VARIANT; //~ ERROR interior mutable
38
39         // The lint ignores default values because an impl of this trait can set
40         // an unfrozen variant to `DEFAULTED_ON_FROZEN_VARIANT` and use the default impl for `function`.
41         let _ = &Self::DEFAULTED_ON_FROZEN_VARIANT; //~ ERROR interior mutable
42     }
43 }
44
45 impl AssocConsts for u64 {
46     const TO_BE_UNFROZEN_VARIANT: OptionalCell = OptionalCell::Unfrozen(Cell::new(false));
47     const TO_BE_FROZEN_VARIANT: OptionalCell = OptionalCell::Frozen;
48
49     fn function() {
50         let _ = &<Self as AssocConsts>::TO_BE_UNFROZEN_VARIANT; //~ ERROR interior mutable
51         let _ = &<Self as AssocConsts>::TO_BE_FROZEN_VARIANT;
52         let _ = &Self::DEFAULTED_ON_UNFROZEN_VARIANT; //~ ERROR interior mutable
53         let _ = &Self::DEFAULTED_ON_FROZEN_VARIANT;
54     }
55 }
56
57 trait AssocTypes {
58     type ToBeUnfrozen;
59
60     const TO_BE_UNFROZEN_VARIANT: Option<Self::ToBeUnfrozen>;
61     const TO_BE_FROZEN_VARIANT: Option<Self::ToBeUnfrozen>;
62
63     // there's no need to test here because it's the exactly same as `trait::AssocTypes`
64     fn function();
65 }
66
67 impl AssocTypes for u64 {
68     type ToBeUnfrozen = AtomicUsize;
69
70     const TO_BE_UNFROZEN_VARIANT: Option<Self::ToBeUnfrozen> = Some(Self::ToBeUnfrozen::new(4)); //~ ERROR interior mutable
71     const TO_BE_FROZEN_VARIANT: Option<Self::ToBeUnfrozen> = None;
72
73     fn function() {
74         let _ = &<Self as AssocTypes>::TO_BE_UNFROZEN_VARIANT; //~ ERROR interior mutable
75         let _ = &<Self as AssocTypes>::TO_BE_FROZEN_VARIANT;
76     }
77 }
78
79 enum BothOfCellAndGeneric<T> {
80     Unfrozen(Cell<*const T>),
81     Generic(*const T),
82     Frozen(usize),
83 }
84
85 impl<T> BothOfCellAndGeneric<T> {
86     const UNFROZEN_VARIANT: BothOfCellAndGeneric<T> = BothOfCellAndGeneric::Unfrozen(Cell::new(std::ptr::null())); //~ ERROR interior mutable
87     const GENERIC_VARIANT: BothOfCellAndGeneric<T> = BothOfCellAndGeneric::Generic(std::ptr::null()); //~ ERROR interior mutable
88     const FROZEN_VARIANT: BothOfCellAndGeneric<T> = BothOfCellAndGeneric::Frozen(5);
89
90     fn function() {
91         let _ = &Self::UNFROZEN_VARIANT; //~ ERROR interior mutability
92         let _ = &Self::GENERIC_VARIANT; //~ ERROR interior mutability
93         let _ = &Self::FROZEN_VARIANT;
94     }
95 }
96
97 fn main() {
98     // constants defined in foreign crates
99     let _ = &helper::WRAPPED_PRIVATE_UNFROZEN_VARIANT; //~ ERROR interior mutability
100     let _ = &helper::WRAPPED_PRIVATE_FROZEN_VARIANT;
101 }