Improve wording of static_mut_ref
Rename `static_mut_ref` lint to `static_mut_refs`.
This commit is contained in:
parent
eeeb021954
commit
408eeae59d
73 changed files with 783 additions and 460 deletions
|
@ -1,22 +1,26 @@
|
|||
Reference of mutable static.
|
||||
You have created a reference to a mutable static.
|
||||
|
||||
Erroneous code example:
|
||||
|
||||
```compile_fail,edition2024,E0796
|
||||
static mut X: i32 = 23;
|
||||
static mut Y: i32 = 24;
|
||||
|
||||
unsafe {
|
||||
let y = &X;
|
||||
let ref x = X;
|
||||
let (x, y) = (&X, &Y);
|
||||
foo(&X);
|
||||
fn work() {
|
||||
let _val = unsafe { X };
|
||||
}
|
||||
|
||||
fn foo<'a>(_x: &'a i32) {}
|
||||
let x_ref = unsafe { &mut X };
|
||||
work();
|
||||
// The next line has Undefined Behavior!
|
||||
// `x_ref` is a mutable reference and allows no aliases,
|
||||
// but `work` has been reading the reference between
|
||||
// the moment `x_ref` was created and when it was used.
|
||||
// This violates the uniqueness of `x_ref`.
|
||||
*x_ref = 42;
|
||||
```
|
||||
|
||||
Mutable statics can be written to by multiple threads: aliasing violations or
|
||||
data races will cause undefined behavior.
|
||||
A reference to a mutable static has lifetime `'static`. This is very dangerous
|
||||
as it is easy to accidentally overlap the lifetime of that reference with
|
||||
other, conflicting accesses to the same static.
|
||||
|
||||
Reference of mutable static is a hard error from 2024 edition.
|
||||
References to mutable statics are a hard error in the 2024 edition.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue