Skip to content

Commit

Permalink
Add must_use to value created by anyhow! macro
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Mar 7, 2022
1 parent 1af20cc commit ffa919c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
8 changes: 8 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -668,4 +668,12 @@ pub mod private {
Error::msg(fmt::format(args))
}
}

#[doc(hidden)]
#[inline]
#[cold]
#[must_use]
pub fn must_use(error: Error) -> Error {
error
}
}
26 changes: 15 additions & 11 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,17 +206,21 @@ macro_rules! ensure {
/// ```
#[macro_export]
macro_rules! anyhow {
($msg:literal $(,)?) => ({
let error = $crate::private::format_err($crate::private::format_args!($msg));
error
});
($err:expr $(,)?) => ({
use $crate::private::kind::*;
let error = match $err {
error => (&error).anyhow_kind().new(error),
};
error
});
($msg:literal $(,)?) => {
$crate::private::must_use({
let error = $crate::private::format_err($crate::private::format_args!($msg));
error
})
};
($err:expr $(,)?) => {
$crate::private::must_use({
use $crate::private::kind::*;
let error = match $err {
error => (&error).anyhow_kind().new(error),
};
error
})
};
($fmt:expr, $($arg:tt)*) => {
$crate::Error::msg($crate::private::format!($fmt, $($arg)*))
};
Expand Down

0 comments on commit ffa919c

Please sign in to comment.