Skip to content

Commit cf2dc18

Browse files
committed
chore: refactor backtrace style in panic
fix: rustfmt
1 parent 2cec7a8 commit cf2dc18

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

library/std/src/panic.rs

+24-24
Original file line numberDiff line numberDiff line change
@@ -463,11 +463,10 @@ static SHOULD_CAPTURE: AtomicU8 = AtomicU8::new(0);
463463
/// environment variable; see the details in [`get_backtrace_style`].
464464
#[unstable(feature = "panic_backtrace_config", issue = "93346")]
465465
pub fn set_backtrace_style(style: BacktraceStyle) {
466-
if !cfg!(feature = "backtrace") {
467-
// If the `backtrace` feature of this crate isn't enabled, skip setting.
468-
return;
466+
if cfg!(feature = "backtrace") {
467+
// If the `backtrace` feature of this crate is enabled, set the backtrace style.
468+
SHOULD_CAPTURE.store(style.as_u8(), Ordering::Release);
469469
}
470-
SHOULD_CAPTURE.store(style.as_u8(), Ordering::Release);
471470
}
472471

473472
/// Checks whether the standard library's panic hook will capture and print a
@@ -499,27 +498,28 @@ pub fn get_backtrace_style() -> Option<BacktraceStyle> {
499498
// to optimize away callers.
500499
return None;
501500
}
502-
if let Some(style) = BacktraceStyle::from_u8(SHOULD_CAPTURE.load(Ordering::Acquire)) {
503-
return Some(style);
504-
}
505-
506-
let format = crate::env::var_os("RUST_BACKTRACE")
507-
.map(|x| {
508-
if &x == "0" {
509-
BacktraceStyle::Off
510-
} else if &x == "full" {
511-
BacktraceStyle::Full
512-
} else {
513-
BacktraceStyle::Short
514-
}
515-
})
516-
.unwrap_or(if crate::sys::FULL_BACKTRACE_DEFAULT {
517-
BacktraceStyle::Full
518-
} else {
519-
BacktraceStyle::Off
501+
return BacktraceStyle::from_u8(SHOULD_CAPTURE.load(Ordering::Acquire))
502+
.map(Some)
503+
.unwrap_or_else(|| {
504+
let env_var = crate::env::var_os("RUST_BACKTRACE");
505+
let style = env_var
506+
.and_then(|var| {
507+
var.to_str().map(|s| match s {
508+
"0" => BacktraceStyle::Off,
509+
"full" => BacktraceStyle::Full,
510+
_ => BacktraceStyle::Short,
511+
})
512+
})
513+
.unwrap_or_else(|| {
514+
if crate::sys::FULL_BACKTRACE_DEFAULT {
515+
BacktraceStyle::Full
516+
} else {
517+
BacktraceStyle::Short
518+
}
519+
});
520+
set_backtrace_style(style);
521+
Some(style)
520522
});
521-
set_backtrace_style(format);
522-
Some(format)
523523
}
524524

525525
#[cfg(test)]

0 commit comments

Comments
 (0)