Skip to content

Commit 86e83e8

Browse files
authored
Remove unsafe by using LazyLock (#15096)
There isn't a good reason to use unsafe code here, and as far as I know `LazyLock` meets the MSRV of 1.82 as it was added in 1.80. Should have no change to behavior.
2 parents 91d8140 + 8da5ba4 commit 86e83e8

File tree

1 file changed

+14
-16
lines changed
  • crates/cargo-test-macro/src

1 file changed

+14
-16
lines changed

crates/cargo-test-macro/src/lib.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use proc_macro::*;
1111
use std::path::Path;
1212
use std::process::Command;
13-
use std::sync::Once;
13+
use std::sync::LazyLock;
1414

1515
/// Replacement for `#[test]`
1616
///
@@ -254,23 +254,21 @@ fn to_token_stream(code: &str) -> TokenStream {
254254
code.parse().unwrap()
255255
}
256256

257-
static mut VERSION: (u32, bool) = (0, false);
257+
static VERSION: std::sync::LazyLock<(u32, bool)> = LazyLock::new(|| {
258+
let output = Command::new("rustc")
259+
.arg("-V")
260+
.output()
261+
.expect("rustc should run");
262+
let stdout = std::str::from_utf8(&output.stdout).expect("utf8");
263+
let vers = stdout.split_whitespace().skip(1).next().unwrap();
264+
let is_nightly = option_env!("CARGO_TEST_DISABLE_NIGHTLY").is_none()
265+
&& (vers.contains("-nightly") || vers.contains("-dev"));
266+
let minor = vers.split('.').skip(1).next().unwrap().parse().unwrap();
267+
(minor, is_nightly)
268+
});
258269

259270
fn version() -> (u32, bool) {
260-
static INIT: Once = Once::new();
261-
INIT.call_once(|| {
262-
let output = Command::new("rustc")
263-
.arg("-V")
264-
.output()
265-
.expect("rustc should run");
266-
let stdout = std::str::from_utf8(&output.stdout).expect("utf8");
267-
let vers = stdout.split_whitespace().skip(1).next().unwrap();
268-
let is_nightly = option_env!("CARGO_TEST_DISABLE_NIGHTLY").is_none()
269-
&& (vers.contains("-nightly") || vers.contains("-dev"));
270-
let minor = vers.split('.').skip(1).next().unwrap().parse().unwrap();
271-
unsafe { VERSION = (minor, is_nightly) }
272-
});
273-
unsafe { VERSION }
271+
LazyLock::force(&VERSION).clone()
274272
}
275273

276274
fn check_command(command_path: &Path, args: &[&str]) -> bool {

0 commit comments

Comments
 (0)