Skip to content

Commit

Permalink
Remove must_use call from ensure! and bail! expansions
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Mar 7, 2022
1 parent ffa919c commit 573689b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/ensure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -818,17 +818,17 @@ macro_rules! __fallback_ensure {
};
($cond:expr, $msg:literal $(,)?) => {
if !$cond {
return $crate::private::Err($crate::anyhow!($msg));
return $crate::private::Err($crate::__anyhow!($msg));
}
};
($cond:expr, $err:expr $(,)?) => {
if !$cond {
return $crate::private::Err($crate::anyhow!($err));
return $crate::private::Err($crate::__anyhow!($err));
}
};
($cond:expr, $fmt:expr, $($arg:tt)*) => {
if !$cond {
return $crate::private::Err($crate::anyhow!($fmt, $($arg)*));
return $crate::private::Err($crate::__anyhow!($fmt, $($arg)*));
}
};
}
40 changes: 31 additions & 9 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@
#[macro_export]
macro_rules! bail {
($msg:literal $(,)?) => {
return $crate::private::Err($crate::anyhow!($msg))
return $crate::private::Err($crate::__anyhow!($msg))
};
($err:expr $(,)?) => {
return $crate::private::Err($crate::anyhow!($err))
return $crate::private::Err($crate::__anyhow!($err))
};
($fmt:expr, $($arg:tt)*) => {
return $crate::private::Err($crate::anyhow!($fmt, $($arg)*))
return $crate::private::Err($crate::__anyhow!($fmt, $($arg)*))
};
}

Expand All @@ -75,13 +75,13 @@ macro_rules! bail {
return $crate::private::Err($crate::Error::msg("pattern does not contain `{}`"))
};
($msg:literal $(,)?) => {
return $crate::private::Err($crate::anyhow!($msg))
return $crate::private::Err($crate::__anyhow!($msg))
};
($err:expr $(,)?) => {
return $crate::private::Err($crate::anyhow!($err))
return $crate::private::Err($crate::__anyhow!($err))
};
($fmt:expr, $($arg:tt)*) => {
return $crate::private::Err($crate::anyhow!($fmt, $($arg)*))
return $crate::private::Err($crate::__anyhow!($fmt, $($arg)*))
};
}

Expand Down Expand Up @@ -145,17 +145,17 @@ macro_rules! ensure {
};
($cond:expr, $msg:literal $(,)?) => {
if !$cond {
return $crate::private::Err($crate::anyhow!($msg));
return $crate::private::Err($crate::__anyhow!($msg));
}
};
($cond:expr, $err:expr $(,)?) => {
if !$cond {
return $crate::private::Err($crate::anyhow!($err));
return $crate::private::Err($crate::__anyhow!($err));
}
};
($cond:expr, $fmt:expr, $($arg:tt)*) => {
if !$cond {
return $crate::private::Err($crate::anyhow!($fmt, $($arg)*));
return $crate::private::Err($crate::__anyhow!($fmt, $($arg)*));
}
};
}
Expand Down Expand Up @@ -225,3 +225,25 @@ macro_rules! anyhow {
$crate::Error::msg($crate::private::format!($fmt, $($arg)*))
};
}

// Not public API. This is used in the implementation of some of the other
// macros, in which the must_use call is not needed because the value is known
// to be used.
#[doc(hidden)]
#[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
});
($fmt:expr, $($arg:tt)*) => {
$crate::Error::msg($crate::private::format!($fmt, $($arg)*))
};
}

0 comments on commit 573689b

Please sign in to comment.