Skip to content

Commit 573689b

Browse files
committed
Remove must_use call from ensure! and bail! expansions
1 parent ffa919c commit 573689b

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

src/ensure.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -818,17 +818,17 @@ macro_rules! __fallback_ensure {
818818
};
819819
($cond:expr, $msg:literal $(,)?) => {
820820
if !$cond {
821-
return $crate::private::Err($crate::anyhow!($msg));
821+
return $crate::private::Err($crate::__anyhow!($msg));
822822
}
823823
};
824824
($cond:expr, $err:expr $(,)?) => {
825825
if !$cond {
826-
return $crate::private::Err($crate::anyhow!($err));
826+
return $crate::private::Err($crate::__anyhow!($err));
827827
}
828828
};
829829
($cond:expr, $fmt:expr, $($arg:tt)*) => {
830830
if !$cond {
831-
return $crate::private::Err($crate::anyhow!($fmt, $($arg)*));
831+
return $crate::private::Err($crate::__anyhow!($fmt, $($arg)*));
832832
}
833833
};
834834
}

src/macros.rs

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,13 @@
5454
#[macro_export]
5555
macro_rules! bail {
5656
($msg:literal $(,)?) => {
57-
return $crate::private::Err($crate::anyhow!($msg))
57+
return $crate::private::Err($crate::__anyhow!($msg))
5858
};
5959
($err:expr $(,)?) => {
60-
return $crate::private::Err($crate::anyhow!($err))
60+
return $crate::private::Err($crate::__anyhow!($err))
6161
};
6262
($fmt:expr, $($arg:tt)*) => {
63-
return $crate::private::Err($crate::anyhow!($fmt, $($arg)*))
63+
return $crate::private::Err($crate::__anyhow!($fmt, $($arg)*))
6464
};
6565
}
6666

@@ -75,13 +75,13 @@ macro_rules! bail {
7575
return $crate::private::Err($crate::Error::msg("pattern does not contain `{}`"))
7676
};
7777
($msg:literal $(,)?) => {
78-
return $crate::private::Err($crate::anyhow!($msg))
78+
return $crate::private::Err($crate::__anyhow!($msg))
7979
};
8080
($err:expr $(,)?) => {
81-
return $crate::private::Err($crate::anyhow!($err))
81+
return $crate::private::Err($crate::__anyhow!($err))
8282
};
8383
($fmt:expr, $($arg:tt)*) => {
84-
return $crate::private::Err($crate::anyhow!($fmt, $($arg)*))
84+
return $crate::private::Err($crate::__anyhow!($fmt, $($arg)*))
8585
};
8686
}
8787

@@ -145,17 +145,17 @@ macro_rules! ensure {
145145
};
146146
($cond:expr, $msg:literal $(,)?) => {
147147
if !$cond {
148-
return $crate::private::Err($crate::anyhow!($msg));
148+
return $crate::private::Err($crate::__anyhow!($msg));
149149
}
150150
};
151151
($cond:expr, $err:expr $(,)?) => {
152152
if !$cond {
153-
return $crate::private::Err($crate::anyhow!($err));
153+
return $crate::private::Err($crate::__anyhow!($err));
154154
}
155155
};
156156
($cond:expr, $fmt:expr, $($arg:tt)*) => {
157157
if !$cond {
158-
return $crate::private::Err($crate::anyhow!($fmt, $($arg)*));
158+
return $crate::private::Err($crate::__anyhow!($fmt, $($arg)*));
159159
}
160160
};
161161
}
@@ -225,3 +225,25 @@ macro_rules! anyhow {
225225
$crate::Error::msg($crate::private::format!($fmt, $($arg)*))
226226
};
227227
}
228+
229+
// Not public API. This is used in the implementation of some of the other
230+
// macros, in which the must_use call is not needed because the value is known
231+
// 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+
error
238+
});
239+
($err:expr $(,)?) => ({
240+
use $crate::private::kind::*;
241+
let error = match $err {
242+
error => (&error).anyhow_kind().new(error),
243+
};
244+
error
245+
});
246+
($fmt:expr, $($arg:tt)*) => {
247+
$crate::Error::msg($crate::private::format!($fmt, $($arg)*))
248+
};
249+
}

0 commit comments

Comments
 (0)