Skip to content

Commit

Permalink
Remove unsafe by using LazyLock (#15096)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
epage authored Jan 24, 2025
2 parents 91d8140 + 8da5ba4 commit 86e83e8
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions crates/cargo-test-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use proc_macro::*;
use std::path::Path;
use std::process::Command;
use std::sync::Once;
use std::sync::LazyLock;

/// Replacement for `#[test]`
///
Expand Down Expand Up @@ -254,23 +254,21 @@ fn to_token_stream(code: &str) -> TokenStream {
code.parse().unwrap()
}

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

fn version() -> (u32, bool) {
static INIT: Once = Once::new();
INIT.call_once(|| {
let output = Command::new("rustc")
.arg("-V")
.output()
.expect("rustc should run");
let stdout = std::str::from_utf8(&output.stdout).expect("utf8");
let vers = stdout.split_whitespace().skip(1).next().unwrap();
let is_nightly = option_env!("CARGO_TEST_DISABLE_NIGHTLY").is_none()
&& (vers.contains("-nightly") || vers.contains("-dev"));
let minor = vers.split('.').skip(1).next().unwrap().parse().unwrap();
unsafe { VERSION = (minor, is_nightly) }
});
unsafe { VERSION }
LazyLock::force(&VERSION).clone()
}

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

0 comments on commit 86e83e8

Please sign in to comment.