Skip to content

Commit 43b7ea2

Browse files
committed
wip test
1 parent 5c63f87 commit 43b7ea2

4 files changed

+657
-0
lines changed
+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#![deny(clippy::borrow_interior_mutable_const)]
2+
#![allow(clippy::declare_interior_mutable_const, const_item_mutation)]
3+
4+
use core::cell::{Cell, UnsafeCell};
5+
use core::ops::Deref;
6+
7+
trait ConstDefault {
8+
const DEFAULT: Self;
9+
}
10+
impl ConstDefault for u32 {
11+
const DEFAULT: Self = 0;
12+
}
13+
impl<T: ConstDefault> ConstDefault for Cell<T> {
14+
const DEFAULT: Self = Cell::new(T::DEFAULT);
15+
}
16+
17+
fn main() {
18+
{
19+
const C: String = String::new();
20+
let _ = C;
21+
let _ = &C;
22+
let _ = C.len();
23+
let _ = &*C;
24+
}
25+
{
26+
const C: UnsafeCell<u32> = UnsafeCell::new(0);
27+
let _ = C;
28+
let _ = &C; //~ borrow_interior_mutable_const
29+
let _ = C.into_inner();
30+
let _ = C.get(); //~ borrow_interior_mutable_const
31+
}
32+
{
33+
const C: Cell<u32> = Cell::new(0);
34+
let _ = C;
35+
let _ = &C; //~ borrow_interior_mutable_const
36+
let _ = &mut C; //~ borrow_interior_mutable_const
37+
let _ = C.into_inner();
38+
39+
let local = C;
40+
C.swap(&local) //~ borrow_interior_mutable_const
41+
}
42+
{
43+
const C: [(Cell<u32>,); 1] = [(Cell::new(0),)];
44+
let _ = C;
45+
let _ = &C; //~ borrow_interior_mutable_const
46+
let _ = &C[0]; //~ borrow_interior_mutable_const
47+
let _ = &C[0].0; //~ borrow_interior_mutable_const
48+
C[0].0.set(1); //~ borrow_interior_mutable_const
49+
}
50+
{
51+
struct S(Cell<u32>);
52+
impl S {
53+
const C: Self = Self(Cell::new(0));
54+
}
55+
impl Deref for S {
56+
type Target = Cell<u32>;
57+
fn deref(&self) -> &Self::Target {
58+
&self.0
59+
}
60+
}
61+
let _ = S::C;
62+
let _ = S::C.0;
63+
let _ = &S::C; //~ borrow_interior_mutable_const
64+
let _ = &S::C.0; //~ borrow_interior_mutable_const
65+
S::C.set(1); //~ borrow_interior_mutable_const
66+
let _ = &*S::C; //~ borrow_interior_mutable_const
67+
(*S::C).set(1); //~ borrow_interior_mutable_const
68+
}
69+
{
70+
enum E {
71+
Cell(Cell<u32>),
72+
Other,
73+
}
74+
const CELL: E = E::Cell(Cell::new(0));
75+
const OTHER: E = E::Other;
76+
77+
let _ = CELL;
78+
let _ = &CELL; //~ borrow_interior_mutable_const
79+
let E::Cell(_) = CELL else {
80+
return;
81+
};
82+
83+
let _ = OTHER;
84+
let _ = &OTHER;
85+
let E::Cell(ref _x) = OTHER else {
86+
return;
87+
};
88+
}
89+
{
90+
struct S<T> {
91+
cell: (Cell<T>, u32),
92+
other: Option<T>,
93+
}
94+
impl<T: ConstDefault + Copy> S<T> {
95+
const C: Self = Self {
96+
cell: (Cell::<T>::DEFAULT, 0),
97+
other: Some(T::DEFAULT),
98+
};
99+
100+
fn f() {
101+
let _ = Self::C;
102+
let _ = &Self::C; //~ borrow_interior_mutable_const
103+
let _ = Self::C.other;
104+
let _ = &Self::C.other;
105+
let _ = &Self::C.cell; //~ borrow_interior_mutable_const
106+
let _ = &Self::C.cell.0; //~ borrow_interior_mutable_const
107+
Self::C.cell.0.set(T::DEFAULT); //~ borrow_interior_mutable_const
108+
let _ = &Self::C.cell.1;
109+
}
110+
}
111+
}
112+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
error: borrow of a named constant with interior mutability
2+
--> tests/ui/borrow_interior_mutable_const.rs:28:17
3+
|
4+
LL | let _ = &C;
5+
| ^^
6+
|
7+
= help: this lint can be silenced by assigning the value to a local variable before borrowing
8+
note: the lint level is defined here
9+
--> tests/ui/borrow_interior_mutable_const.rs:1:9
10+
|
11+
LL | #![deny(clippy::borrow_interior_mutable_const)]
12+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
13+
14+
error: borrow of a named constant with interior mutability
15+
--> tests/ui/borrow_interior_mutable_const.rs:30:17
16+
|
17+
LL | let _ = C.get();
18+
| ^
19+
|
20+
= help: this lint can be silenced by assigning the value to a local variable before borrowing
21+
= note: there is a compiler inserted borrow here
22+
23+
error: borrow of a named constant with interior mutability
24+
--> tests/ui/borrow_interior_mutable_const.rs:35:17
25+
|
26+
LL | let _ = &C;
27+
| ^^
28+
|
29+
= help: this lint can be silenced by assigning the value to a local variable before borrowing
30+
31+
error: borrow of a named constant with interior mutability
32+
--> tests/ui/borrow_interior_mutable_const.rs:36:17
33+
|
34+
LL | let _ = &mut C;
35+
| ^^^^^^
36+
|
37+
= help: this lint can be silenced by assigning the value to a local variable before borrowing
38+
39+
error: borrow of a named constant with interior mutability
40+
--> tests/ui/borrow_interior_mutable_const.rs:40:9
41+
|
42+
LL | C.swap(&local)
43+
| ^
44+
|
45+
= help: this lint can be silenced by assigning the value to a local variable before borrowing
46+
= note: there is a compiler inserted borrow here
47+
48+
error: borrow of a named constant with interior mutability
49+
--> tests/ui/borrow_interior_mutable_const.rs:45:17
50+
|
51+
LL | let _ = &C;
52+
| ^^
53+
|
54+
= help: this lint can be silenced by assigning the value to a local variable before borrowing
55+
56+
error: borrow of a named constant with interior mutability
57+
--> tests/ui/borrow_interior_mutable_const.rs:46:17
58+
|
59+
LL | let _ = &C[0];
60+
| ^^^^^
61+
|
62+
= help: this lint can be silenced by assigning the value to a local variable before borrowing
63+
64+
error: borrow of a named constant with interior mutability
65+
--> tests/ui/borrow_interior_mutable_const.rs:47:17
66+
|
67+
LL | let _ = &C[0].0;
68+
| ^^^^^^^
69+
|
70+
= help: this lint can be silenced by assigning the value to a local variable before borrowing
71+
72+
error: borrow of a named constant with interior mutability
73+
--> tests/ui/borrow_interior_mutable_const.rs:48:9
74+
|
75+
LL | C[0].0.set(1);
76+
| ^^^^^^
77+
|
78+
= help: this lint can be silenced by assigning the value to a local variable before borrowing
79+
= note: there is a compiler inserted borrow here
80+
81+
error: borrow of a named constant with interior mutability
82+
--> tests/ui/borrow_interior_mutable_const.rs:63:17
83+
|
84+
LL | let _ = &S::C;
85+
| ^^^^^
86+
|
87+
= help: this lint can be silenced by assigning the value to a local variable before borrowing
88+
89+
error: borrow of a named constant with interior mutability
90+
--> tests/ui/borrow_interior_mutable_const.rs:64:17
91+
|
92+
LL | let _ = &S::C.0;
93+
| ^^^^^^^
94+
|
95+
= help: this lint can be silenced by assigning the value to a local variable before borrowing
96+
97+
error: borrow of a named constant with interior mutability
98+
--> tests/ui/borrow_interior_mutable_const.rs:65:9
99+
|
100+
LL | S::C.set(1);
101+
| ^^^^
102+
|
103+
= help: this lint can be silenced by assigning the value to a local variable before borrowing
104+
= note: there is a compiler inserted call to `Deref::deref` here
105+
106+
error: borrow of a named constant with interior mutability
107+
--> tests/ui/borrow_interior_mutable_const.rs:66:18
108+
|
109+
LL | let _ = &*S::C;
110+
| ^^^^^
111+
|
112+
= help: this lint can be silenced by assigning the value to a local variable before borrowing
113+
= note: this deref expression is a call to `Deref::deref`
114+
115+
error: borrow of a named constant with interior mutability
116+
--> tests/ui/borrow_interior_mutable_const.rs:67:9
117+
|
118+
LL | (*S::C).set(1);
119+
| ^^^^^^^
120+
|
121+
= help: this lint can be silenced by assigning the value to a local variable before borrowing
122+
= note: this deref expression is a call to `Deref::deref`
123+
124+
error: borrow of a named constant with interior mutability
125+
--> tests/ui/borrow_interior_mutable_const.rs:78:17
126+
|
127+
LL | let _ = &CELL;
128+
| ^^^^^
129+
|
130+
= help: this lint can be silenced by assigning the value to a local variable before borrowing
131+
132+
error: borrow of a named constant with interior mutability
133+
--> tests/ui/borrow_interior_mutable_const.rs:102:25
134+
|
135+
LL | let _ = &Self::C;
136+
| ^^^^^^^^
137+
|
138+
= help: this lint can be silenced by assigning the value to a local variable before borrowing
139+
140+
error: borrow of a named constant with interior mutability
141+
--> tests/ui/borrow_interior_mutable_const.rs:105:25
142+
|
143+
LL | let _ = &Self::C.cell;
144+
| ^^^^^^^^^^^^^
145+
|
146+
= help: this lint can be silenced by assigning the value to a local variable before borrowing
147+
148+
error: borrow of a named constant with interior mutability
149+
--> tests/ui/borrow_interior_mutable_const.rs:106:25
150+
|
151+
LL | let _ = &Self::C.cell.0;
152+
| ^^^^^^^^^^^^^^^
153+
|
154+
= help: this lint can be silenced by assigning the value to a local variable before borrowing
155+
156+
error: borrow of a named constant with interior mutability
157+
--> tests/ui/borrow_interior_mutable_const.rs:107:17
158+
|
159+
LL | Self::C.cell.0.set(T::DEFAULT);
160+
| ^^^^^^^^^^^^^^
161+
|
162+
= help: this lint can be silenced by assigning the value to a local variable before borrowing
163+
= note: there is a compiler inserted borrow here
164+
165+
error: aborting due to 19 previous errors
166+

0 commit comments

Comments
 (0)