diff --git a/library/std/src/panic.rs b/library/std/src/panic.rs index 85ba35dee148c..2dbac23662273 100644 --- a/library/std/src/panic.rs +++ b/library/std/src/panic.rs @@ -498,26 +498,24 @@ pub fn get_backtrace_style() -> Option { // to optimize away callers. return None; } - 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) - }) + if let Some(style) = BacktraceStyle::from_u8(SHOULD_CAPTURE.load(Ordering::Acquire)) { + return Some(style); + } + + let format = match crate::env::var_os("RUST_BACKTRACE") { + Some(x) if &x == "0" => BacktraceStyle::Off, + Some(x) if &x == "full" => BacktraceStyle::Full, + Some(_) => BacktraceStyle::Short, + None => { + if crate::sys::FULL_BACKTRACE_DEFAULT { + BacktraceStyle::Full + } else { + BacktraceStyle::Short + } + } + }; + set_backtrace_style(format); + Some(format) } #[cfg(test)]