Skip to content
Myr

Merge pull request #229 from dtolnay/must_use

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Add must_use to Error constructors

authored by David Tolnay · committed by GitHub · parents 3ca5036, cf5d619 · Browse files · 6 files+72 added−12 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

6 files+72 added−12 removed

  • tests:+23 added−0 removed
  • other:+49 added−12 removed

6 files+72 added−12 removed

modified:src/ensure.rs+3 added-3 removed
816 unmodified lines
817 }818 };819 ($cond:expr, $msg:literal $(,)?) => {820 if !$cond {821 return $crate::private::Err($crate::anyhow!($msg));821 return $crate::private::Err($crate::__anyhow!($msg));822 }823 };824 ($cond:expr, $err:expr $(,)?) => {825 if !$cond {826 return $crate::private::Err($crate::anyhow!($err));826 return $crate::private::Err($crate::__anyhow!($err));827 }828 };829 ($cond:expr, $fmt:expr, $($arg:tt)*) => {830 if !$cond {831 return $crate::private::Err($crate::anyhow!($fmt, $($arg)*));831 return $crate::private::Err($crate::__anyhow!($fmt, $($arg)*));832 }833 };834}
modified:src/error.rs+3 added
24 unmodified lines
25 /// created here to ensure that a backtrace exists.26 #[cfg(feature = "std")]27 #[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]28 #[cold]29 #[must_use]30 pub fn new<E>(error: E) -> Self31 where32 E: StdError + Send + Sync + 'static,33 {
38 unmodified lines
72 /// .await73 /// }74 /// ```75 #[cold]76 #[must_use]77 pub fn msg<M>(message: M) -> Self78 where79 M: Display + Debug + Send + Sync + 'static,80 {
213 unmodified lines
294 /// })295 /// }296 /// ```297 #[cold]298 #[must_use]299 pub fn context<C>(self, context: C) -> Self300 where301 C: Display + Send + Sync + 'static,302 {
668 unmodified lines
modified:src/lib.rs+8 added
666 unmodified lines
667 // anyhow!("interpolate {var}"), can downcast to String668 Error::msg(fmt::format(args))669 }670 }671672 #[doc(hidden)]673 #[inline]674 #[cold]675 #[must_use]676 pub fn must_use(error: Error) -> Error {677 error678 }679}
modified:src/macros.rs+35 added-9 removed
52 unmodified lines
53#[cfg(doc)]54#[macro_export]55macro_rules! bail {56 ($msg:literal $(,)?) => {57 return $crate::private::Err($crate::anyhow!($msg))57 return $crate::private::Err($crate::__anyhow!($msg))58 };59 ($err:expr $(,)?) => {60 return $crate::private::Err($crate::anyhow!($err))60 return $crate::private::Err($crate::__anyhow!($err))61 };62 ($fmt:expr, $($arg:tt)*) => {63 return $crate::private::Err($crate::anyhow!($fmt, $($arg)*))63 return $crate::private::Err($crate::__anyhow!($fmt, $($arg)*))64 };65}6667// Workaround for crates that intentionally contained `{}` in an error message
6 unmodified lines
74 ("pattern does not contain `{}`") => {75 return $crate::private::Err($crate::Error::msg("pattern does not contain `{}`"))76 };77 ($msg:literal $(,)?) => {78 return $crate::private::Err($crate::anyhow!($msg))78 return $crate::private::Err($crate::__anyhow!($msg))79 };80 ($err:expr $(,)?) => {81 return $crate::private::Err($crate::anyhow!($err))81 return $crate::private::Err($crate::__anyhow!($err))82 };83 ($fmt:expr, $($arg:tt)*) => {84 return $crate::private::Err($crate::anyhow!($fmt, $($arg)*))84 return $crate::private::Err($crate::__anyhow!($fmt, $($arg)*))85 };86}8788/// Return early with an error if a condition is not satisfied.
55 unmodified lines
144 }145 };146 ($cond:expr, $msg:literal $(,)?) => {147 if !$cond {148 return $crate::private::Err($crate::anyhow!($msg));148 return $crate::private::Err($crate::__anyhow!($msg));149 }150 };151 ($cond:expr, $err:expr $(,)?) => {152 if !$cond {153 return $crate::private::Err($crate::anyhow!($err));153 return $crate::private::Err($crate::__anyhow!($err));154 }155 };156 ($cond:expr, $fmt:expr, $($arg:tt)*) => {157 if !$cond {158 return $crate::private::Err($crate::anyhow!($fmt, $($arg)*));158 return $crate::private::Err($crate::__anyhow!($fmt, $($arg)*));159 }160 };161}162
42 unmodified lines
205/// }206/// ```207#[macro_export]208macro_rules! anyhow {209 ($msg:literal $(,)?) => {210 $crate::private::must_use({211 let error = $crate::private::format_err($crate::private::format_args!($msg));212 error213 })214 };215 ($err:expr $(,)?) => {216 $crate::private::must_use({217 use $crate::private::kind::*;218 let error = match $err {219 error => (&error).anyhow_kind().new(error),220 };221 error222 })223 };224 ($fmt:expr, $($arg:tt)*) => {225 $crate::Error::msg($crate::private::format!($fmt, $($arg)*))226 };227}228229// Not public API. This is used in the implementation of some of the other230// macros, in which the must_use call is not needed because the value is known231// to be used.232#[doc(hidden)]233#[macro_export]234macro_rules! __anyhow {235 ($msg:literal $(,)?) => ({236 let error = $crate::private::format_err($crate::private::format_args!($msg));237 error238 });
11 unmodified lines
added:tests/ui/must-use.rs+11 added
1#![deny(unused_must_use)]23use anyhow::anyhow;45fn main() -> anyhow::Result<()> {6 if true {7 // meant to write bail!8 anyhow!("it failed");9 }10 Ok(())11}
added:tests/ui/must-use.stderr+12 added
1error: unused return value of `must_use` that must be used2 --> tests/ui/must-use.rs:8:93 |48 | anyhow!("it failed");5 | ^^^^^^^^^^^^^^^^^^^^6 |7note: the lint level is defined here8 --> tests/ui/must-use.rs:1:99 |101 | #![deny(unused_must_use)]11 | ^^^^^^^^^^^^^^^12 = note: this error originates in the macro `anyhow` (in Nightly builds, run with -Z macro-backtrace for more info)