Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

suggest mut removal sometimes for static mut ref lint/error #120574

Open
est31 opened this issue Feb 2, 2024 · 0 comments
Open

suggest mut removal sometimes for static mut ref lint/error #120574

est31 opened this issue Feb 2, 2024 · 0 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@est31
Copy link
Member

est31 commented Feb 2, 2024

Since #117556 we now lint for code like (playground):

use std::sync::atomic::AtomicI32;
use std::sync::Mutex;

static mut X: AtomicI32 = AtomicI32::new(1);

static mut Y: Mutex<usize> = Mutex::new(0);

fn foo() {
    unsafe {
        let _x = &X;
        let _y = &Y;
    }
}

gives:

warning: shared reference of mutable static is discouraged
  --> src/lib.rs:12:18
   |
12 |         let _x = &X;
   |                  ^^ shared reference of mutable static
   |
   = note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
   = note: reference of mutable static is a hard error from 2024 edition
   = note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
   = note: `#[warn(static_mut_ref)]` on by default
help: shared references are dangerous since if there's any kind of mutation of that static while the reference lives, that's UB; use `addr_of!` instead to create a raw pointer
   |
12 |         let _x = addr_of!(X);
   |                  ~~~~~~~~~~~

warning: shared reference of mutable static is discouraged
  --> src/lib.rs:13:18
   |
13 |         let _y = &Y;
   |                  ^^ shared reference of mutable static
   |
   = note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
   = note: reference of mutable static is a hard error from 2024 edition
   = note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
help: shared references are dangerous since if there's any kind of mutation of that static while the reference lives, that's UB; use `addr_of!` instead to create a raw pointer
   |
13 |         let _y = addr_of!(Y);
   |                  ~~~~~~~~~~~

It suggests using addr_of which is correct but it requires unsafe and we actually don't need it: we can just remove the mut here:

use std::sync::atomic::AtomicI32;
use std::sync::Mutex;

static X: AtomicI32 = AtomicI32::new(1);

static Y: Mutex<usize> = Mutex::new(0);

fn foo() {
    unsafe {
        let _x = &X;
        let _y = &Y;
    }
}

The lint message (error in 2024 edition) should suggest such removal of mut where possible.

The tough part is to find out where this is actually possible. With the right wording we might get away with a heuristic that doesn't recognize all cases correctly.

@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Feb 2, 2024
@est31 est31 added A-diagnostics Area: Messages for errors, warnings, and lints and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Feb 2, 2024
@obeis obeis removed their assignment Feb 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints
Projects
None yet
Development

No branches or pull requests

3 participants