Skip to content

Commit

Permalink
Add test for static_mut_ref lint
Browse files Browse the repository at this point in the history
  • Loading branch information
obeis committed Nov 3, 2023
1 parent 7c7b986 commit f3bfb1d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
9 changes: 7 additions & 2 deletions tests/ui/borrowck/borrowck-unsafe-static-mutable-borrows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@

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

struct Foo { x: [usize; 2] }
struct Foo {
x: [usize; 2],
}

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

impl Foo {
fn x(&mut self) -> &mut usize { &mut self.x[0] }
fn x(&mut self) -> &mut usize {
&mut self.x[0]
}
}

fn main() {
unsafe {
let sfoo: *mut Foo = &mut SFOO;
//~^ WARN use of mutable static [static_mut_ref]
let x = (*sfoo).x();
(*sfoo).x[1] += 1;
*x += 1;
Expand Down
11 changes: 11 additions & 0 deletions tests/ui/borrowck/borrowck-unsafe-static-mutable-borrows.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
warning: use of mutable static
--> $DIR/borrowck-unsafe-static-mutable-borrows.rs:19:30
|
LL | let sfoo: *mut Foo = &mut SFOO;
| ^^^^^^^^^ use of mutable static
|
= note: use of mutable static is a hard error in 2024 edition
= note: `#[warn(static_mut_ref)]` on by default

warning: 1 warning emitted

15 changes: 15 additions & 0 deletions tests/ui/lint/lint_static_mut_ref.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#![deny(static_mut_ref)]

fn main() {
static mut X: i32 = 1;
unsafe {
let _y = &X;
//~^ ERROR use of mutable static
}
}

unsafe fn _foo() {
static mut X: i32 = 1;
let _y = &X;
//~^ ERROR use of mutable static
}
23 changes: 23 additions & 0 deletions tests/ui/lint/lint_static_mut_ref.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
error: use of mutable static
--> $DIR/lint_static_mut_ref.rs:6:18
|
LL | let _y = &X;
| ^^ use of mutable static
|
= note: use of mutable static is a hard error in 2024 edition
note: the lint level is defined here
--> $DIR/lint_static_mut_ref.rs:1:9
|
LL | #![deny(static_mut_ref)]
| ^^^^^^^^^^^^^^

error: use of mutable static
--> $DIR/lint_static_mut_ref.rs:13:14
|
LL | let _y = &X;
| ^^ use of mutable static
|
= note: use of mutable static is a hard error in 2024 edition

error: aborting due to 2 previous errors

0 comments on commit f3bfb1d

Please sign in to comment.