From 53ec78d8c05c722fc429cef3d48902c5ca4a0a2a Mon Sep 17 00:00:00 2001 From: Freja Roberts Date: Sat, 30 Dec 2023 02:37:14 +0100 Subject: [PATCH] fix: `ok_or_eyre` not using `track_caller` (#140) `ok_or_eyre` did not use `track_caller`, which meant the error location was incorrect compared to the equivalent anyhow::context feature --- eyre/src/option.rs | 1 + eyre/tests/test_location.rs | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/eyre/src/option.rs b/eyre/src/option.rs index eaa1e84..881222a 100644 --- a/eyre/src/option.rs +++ b/eyre/src/option.rs @@ -2,6 +2,7 @@ use crate::OptionExt; use core::fmt::{Debug, Display}; impl OptionExt for Option { + #[track_caller] fn ok_or_eyre(self, message: M) -> crate::Result where M: Debug + Display + Send + Sync + 'static, diff --git a/eyre/tests/test_location.rs b/eyre/tests/test_location.rs index 58737fe..766514a 100644 --- a/eyre/tests/test_location.rs +++ b/eyre/tests/test_location.rs @@ -1,6 +1,6 @@ use std::panic::Location; -use eyre::WrapErr; +use eyre::{OptionExt as _, WrapErr}; struct LocationHandler { actual: Option<&'static str>, @@ -83,6 +83,19 @@ fn test_wrap_err_with() { println!("{:?}", err); } +#[test] +fn test_option_ok_or_eyre() { + let _ = eyre::set_hook(Box::new(|_e| { + let expected_location = file!(); + Box::new(LocationHandler::new(expected_location)) + })); + + let err = None::<()>.ok_or_eyre("oopsie").unwrap_err(); + + // should panic if the location isn't in our crate + println!("{:?}", err); +} + #[test] fn test_context() { let _ = eyre::set_hook(Box::new(|_e| {