-
Notifications
You must be signed in to change notification settings - Fork 896
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a diagnostics for files that panic during linting rather than crashing Ruff.
- Loading branch information
1 parent
d5700d7
commit a34dcba
Showing
5 changed files
with
95 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#[derive(Default, Debug)] | ||
pub(crate) struct PanicError { | ||
pub info: String, | ||
pub backtrace: Option<std::backtrace::Backtrace>, | ||
} | ||
|
||
impl std::fmt::Display for PanicError { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
writeln!(f, "{}", self.info)?; | ||
if let Some(backtrace) = &self.backtrace { | ||
writeln!(f, "Backtrace: {backtrace}") | ||
} else { | ||
Ok(()) | ||
} | ||
} | ||
} | ||
|
||
thread_local! { | ||
static LAST_PANIC: std::cell::Cell<Option<PanicError>> = std::cell::Cell::new(None); | ||
} | ||
|
||
/// Take and set a specific panic hook before calling `f` inside a `catch_unwind`, then | ||
/// resetting the old panic hook. | ||
/// | ||
/// If `f` panicks an `Error` with the panic message plus backtrace (only if `RUST_BACKTRACE` is set) will be returned. | ||
pub(crate) fn catch_unwind<F, R>(f: F) -> Result<R, PanicError> | ||
where | ||
F: FnOnce() -> R + std::panic::UnwindSafe, | ||
{ | ||
let prev = std::panic::take_hook(); | ||
std::panic::set_hook(Box::new(|info| { | ||
let info = info.to_string(); | ||
let backtrace = std::backtrace::Backtrace::force_capture(); | ||
LAST_PANIC.with(|cell| { | ||
cell.set(Some(PanicError { | ||
info, | ||
backtrace: Some(backtrace), | ||
})) | ||
}) | ||
})); | ||
|
||
let result = std::panic::catch_unwind(f) | ||
.map_err(|_| LAST_PANIC.with(|cell| cell.take()).unwrap_or_default()); | ||
|
||
std::panic::set_hook(prev); | ||
|
||
result | ||
} |