Skip to content

Commit c77a68a

Browse files
committed
chore: refactor backtrace style in panic
fix: rustfmt fix: excess return statement fix: fmt
1 parent 2cec7a8 commit c77a68a

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed

library/std/src/panic.rs

+23-25
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,26 @@ 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
520-
});
521-
set_backtrace_style(format);
522-
Some(format)
501+
BacktraceStyle::from_u8(SHOULD_CAPTURE.load(Ordering::Acquire)).map(Some).unwrap_or_else(|| {
502+
let env_var = crate::env::var_os("RUST_BACKTRACE");
503+
let style = env_var
504+
.and_then(|var| {
505+
var.to_str().map(|s| match s {
506+
"0" => BacktraceStyle::Off,
507+
"full" => BacktraceStyle::Full,
508+
_ => BacktraceStyle::Short,
509+
})
510+
})
511+
.unwrap_or_else(|| {
512+
if crate::sys::FULL_BACKTRACE_DEFAULT {
513+
BacktraceStyle::Full
514+
} else {
515+
BacktraceStyle::Short
516+
}
517+
});
518+
set_backtrace_style(style);
519+
Some(style)
520+
})
523521
}
524522

525523
#[cfg(test)]

0 commit comments

Comments
 (0)