From cd5faad37fe1bde729688f3d46852257c49d7a6c Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 9 Jan 2024 18:39:37 -0800 Subject: [PATCH] Fix static_mut_ref warning warning: shared reference of mutable static is discouraged --> src/lib.rs:170:29 | 170 | let argv = unsafe { &ARGV }; | ^^^^^ shared reference of mutable static | = note: for more information, see issue #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 | 170 | let argv = unsafe { addr_of!(ARGV) }; | ~~~~~~~~~~~~~~ --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 55d90de..e01256e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -155,7 +155,7 @@ mod r#impl { mod r#impl { use std::ffi::OsStr; use std::sync::Once; - use std::{env, iter, slice}; + use std::{env, iter, ptr, slice}; static ONCE: Once = Once::new(); static mut ARGV: Vec<&'static OsStr> = Vec::new(); @@ -167,7 +167,7 @@ mod r#impl { .collect(); unsafe { ARGV = argv } }); - let argv = unsafe { &ARGV }; + let argv = unsafe { &*ptr::addr_of!(ARGV) }; argv.iter().copied() }