From c77a68a5639f3d0ed58191298488b531b72e9b9c Mon Sep 17 00:00:00 2001 From: Konippi Date: Fri, 2 Aug 2024 23:58:05 +0900 Subject: [PATCH] chore: refactor backtrace style in panic fix: rustfmt fix: excess return statement fix: fmt --- library/std/src/panic.rs | 48 +++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/library/std/src/panic.rs b/library/std/src/panic.rs index 2ca9256715cd8..85ba35dee148c 100644 --- a/library/std/src/panic.rs +++ b/library/std/src/panic.rs @@ -463,11 +463,10 @@ static SHOULD_CAPTURE: AtomicU8 = AtomicU8::new(0); /// environment variable; see the details in [`get_backtrace_style`]. #[unstable(feature = "panic_backtrace_config", issue = "93346")] pub fn set_backtrace_style(style: BacktraceStyle) { - if !cfg!(feature = "backtrace") { - // If the `backtrace` feature of this crate isn't enabled, skip setting. - return; + if cfg!(feature = "backtrace") { + // If the `backtrace` feature of this crate is enabled, set the backtrace style. + SHOULD_CAPTURE.store(style.as_u8(), Ordering::Release); } - SHOULD_CAPTURE.store(style.as_u8(), Ordering::Release); } /// Checks whether the standard library's panic hook will capture and print a @@ -499,27 +498,26 @@ pub fn get_backtrace_style() -> Option { // to optimize away callers. return None; } - if let Some(style) = BacktraceStyle::from_u8(SHOULD_CAPTURE.load(Ordering::Acquire)) { - return Some(style); - } - - let format = crate::env::var_os("RUST_BACKTRACE") - .map(|x| { - if &x == "0" { - BacktraceStyle::Off - } else if &x == "full" { - BacktraceStyle::Full - } else { - BacktraceStyle::Short - } - }) - .unwrap_or(if crate::sys::FULL_BACKTRACE_DEFAULT { - BacktraceStyle::Full - } else { - BacktraceStyle::Off - }); - set_backtrace_style(format); - Some(format) + BacktraceStyle::from_u8(SHOULD_CAPTURE.load(Ordering::Acquire)).map(Some).unwrap_or_else(|| { + let env_var = crate::env::var_os("RUST_BACKTRACE"); + let style = env_var + .and_then(|var| { + var.to_str().map(|s| match s { + "0" => BacktraceStyle::Off, + "full" => BacktraceStyle::Full, + _ => BacktraceStyle::Short, + }) + }) + .unwrap_or_else(|| { + if crate::sys::FULL_BACKTRACE_DEFAULT { + BacktraceStyle::Full + } else { + BacktraceStyle::Short + } + }); + set_backtrace_style(style); + Some(style) + }) } #[cfg(test)]