Skip to content

Commit 743f1ee

Browse files
committed
Add test for static_mut_ref lint
1 parent 7c7b986 commit 743f1ee

4 files changed

+56
-2
lines changed

tests/ui/borrowck/borrowck-unsafe-static-mutable-borrows.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,22 @@
22

33
// Test file taken from issue 45129 (https://github.com/rust-lang/rust/issues/45129)
44

5-
struct Foo { x: [usize; 2] }
5+
struct Foo {
6+
x: [usize; 2],
7+
}
68

79
static mut SFOO: Foo = Foo { x: [23, 32] };
810

911
impl Foo {
10-
fn x(&mut self) -> &mut usize { &mut self.x[0] }
12+
fn x(&mut self) -> &mut usize {
13+
&mut self.x[0]
14+
}
1115
}
1216

1317
fn main() {
1418
unsafe {
1519
let sfoo: *mut Foo = &mut SFOO;
20+
//~^ WARN use of mutable static [static_mut_ref]
1621
let x = (*sfoo).x();
1722
(*sfoo).x[1] += 1;
1823
*x += 1;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
warning: use of mutable static
2+
--> $DIR/borrowck-unsafe-static-mutable-borrows.rs:19:30
3+
|
4+
LL | let sfoo: *mut Foo = &mut SFOO;
5+
| ^^^^^^^^^ use of mutable static
6+
|
7+
= note: use of mutable static is a hard error in 2024 edition
8+
= note: `#[warn(static_mut_ref)]` on by default
9+
10+
warning: 1 warning emitted
11+

tests/ui/lint/lint_static_mut_ref.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![deny(static_mut_ref)]
2+
3+
fn main() {
4+
static mut X: i32 = 1;
5+
unsafe {
6+
let _y = &X;
7+
//~^ ERROR use of mutable static
8+
}
9+
}
10+
11+
unsafe fn _foo() {
12+
static mut X: i32 = 1;
13+
let _y = &X;
14+
//~^ ERROR use of mutable static
15+
}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error: use of mutable static
2+
--> $DIR/lint_static_mut_ref.rs:6:18
3+
|
4+
LL | let _y = &X;
5+
| ^^ use of mutable static
6+
|
7+
= note: use of mutable static is a hard error in 2024 edition
8+
note: the lint level is defined here
9+
--> $DIR/lint_static_mut_ref.rs:1:9
10+
|
11+
LL | #![deny(static_mut_ref)]
12+
| ^^^^^^^^^^^^^^
13+
14+
error: use of mutable static
15+
--> $DIR/lint_static_mut_ref.rs:13:14
16+
|
17+
LL | let _y = &X;
18+
| ^^ use of mutable static
19+
|
20+
= note: use of mutable static is a hard error in 2024 edition
21+
22+
error: aborting due to 2 previous errors
23+

0 commit comments

Comments
 (0)