diff --git a/src/macros.rs b/src/macros.rs index b09afa4..a0f4cae 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -173,8 +173,9 @@ macro_rules! anyhow { }; ($err:expr $(,)?) => ({ use $crate::private::kind::*; - let error = $err; - (&error).anyhow_kind().new(error) + match $err { + error => (&error).anyhow_kind().new(error), + } }); ($fmt:expr, $($arg:tt)*) => { $crate::private::new_adhoc(format!($fmt, $($arg)*)) diff --git a/tests/ui/temporary-value.rs b/tests/ui/temporary-value.rs new file mode 100644 index 0000000..803809b --- /dev/null +++ b/tests/ui/temporary-value.rs @@ -0,0 +1,5 @@ +use anyhow::anyhow; + +fn main() { + let _ = anyhow!(&String::new()); +} diff --git a/tests/ui/temporary-value.stderr b/tests/ui/temporary-value.stderr new file mode 100644 index 0000000..fa753d2 --- /dev/null +++ b/tests/ui/temporary-value.stderr @@ -0,0 +1,8 @@ +error[E0716]: temporary value dropped while borrowed + --> $DIR/temporary-value.rs:4:22 + | +4 | let _ = anyhow!(&String::new()); + | ---------^^^^^^^^^^^^^-- temporary value is freed at the end of this statement + | | | + | | creates a temporary which is freed while still in use + | argument requires that borrow lasts for `'static`