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
Provenance
- Commit
c8a3e42- Tree
cde098a- Author
David Tolnay <dtolnay@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
3ca5036- Parent 2
cf5d619
6 files changed+72 added−12 removed
modified:src/ensure.rs+3 added-3 removed
816 unmodified lines817 }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 lines25 /// 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 lines72 /// .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 lines294 /// })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 linesmodified:src/lib.rs+8 added
666 unmodified lines667 // anyhow!("interpolate {var}"), can downcast to String668 Error::msg(fmt::format(args))669 }670 }671+672+ #[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 lines53 #[cfg(doc)]54 #[macro_export]55 macro_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 }66 67 // Workaround for crates that intentionally contained `{}` in an error message6 unmodified lines74 ("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 }87 88 /// Return early with an error if a condition is not satisfied.55 unmodified lines144 }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 lines205 /// }206 /// ```207 #[macro_export]208 macro_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+}228+229+// 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]234+macro_rules! __anyhow {235 ($msg:literal $(,)?) => ({236 let error = $crate::private::format_err($crate::private::format_args!($msg));237 error238 });11 unmodified linesadded:tests/ui/must-use.rs+11 added
1+#![deny(unused_must_use)]2+3+use anyhow::anyhow;4+5+fn 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
1+error: unused return value of `must_use` that must be used2+ --> tests/ui/must-use.rs:8:93+ |4+8 | anyhow!("it failed");5+ | ^^^^^^^^^^^^^^^^^^^^6+ |7+note: the lint level is defined here8+ --> tests/ui/must-use.rs:1:99+ |10+1 | #![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)