Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Allow usage of `ensure!` without an error message (#12324)

Nick Fitzgerald21968116170e
diff --git a/crates/error/src/macros.rs b/crates/error/src/macros.rs
index dce1d00f08..848b6080d0 100644
--- a/crates/error/src/macros.rs
+++ b/crates/error/src/macros.rs
@@ -189,6 +189,10 @@ macro_rules! bail {
/// ```
#[macro_export]
macro_rules! ensure {
+ ( $condition:expr ) => {{
+ $crate::ensure!($condition, concat!("Condition failed: `", stringify!($condition), "`"))
+ }};
+
( $condition:expr , $( $args:tt )* ) => {{
if $crate::macros::ensure::not($condition) {
$crate::bail!( $( $args )* );
diff --git a/crates/error/tests/tests.rs b/crates/error/tests/tests.rs
index 9fa25285a5..587a146ec1 100644
--- a/crates/error/tests/tests.rs
+++ b/crates/error/tests/tests.rs
@@ -398,6 +398,16 @@ fn ensure_macro() {
}
assert!(ensure_bool_ref(&true).is_ok());
assert_eq!(ensure_bool_ref(&false).unwrap_err().to_string(), "whoops");
+
+ fn ensure_no_message(a: u32) -> Result<()> {
+ ensure!(a == 42);
+ Ok(())
+ }
+ assert!(ensure_no_message(42).is_ok());
+ assert_eq!(
+ ensure_no_message(0).unwrap_err().to_string(),
+ "Condition failed: `a == 42`"
+ );
}
#[test]