From 5152c1d0ad65c5fe48f8711db7df8ae31e376ad1 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Fri, 17 May 2024 10:25:52 -0700 Subject: [PATCH 1/3] Add test of ensure with a &bool argument --- tests/test_macros.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/tests/test_macros.rs b/tests/test_macros.rs index a322b67..9072200 100644 --- a/tests/test_macros.rs +++ b/tests/test_macros.rs @@ -11,7 +11,7 @@ mod common; use self::common::*; -use anyhow::{anyhow, ensure}; +use anyhow::{anyhow, ensure, Result}; use std::cell::Cell; use std::future; @@ -53,6 +53,22 @@ fn test_ensure() { ); } +#[test] +fn test_ensure_nonbool() { + struct Struct { + condition: bool, + } + + fn f(s: &Struct) -> Result<()> { + match s { + Struct { condition } => ensure!(condition), // &bool + } + Ok(()) + } + + f(&Struct { condition: true }).unwrap(); +} + #[test] fn test_temporaries() { fn require_send_sync(_: impl Send + Sync) {} From f1cd62cc93a52ff02ff62184f02eaeff93e83592 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Fri, 17 May 2024 10:27:11 -0700 Subject: [PATCH 2/3] Add ui test for incorrect type of ensure argument --- tests/ui/ensure-nonbool.rs | 13 +++++++++++++ tests/ui/ensure-nonbool.stderr | 15 +++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 tests/ui/ensure-nonbool.rs create mode 100644 tests/ui/ensure-nonbool.stderr diff --git a/tests/ui/ensure-nonbool.rs b/tests/ui/ensure-nonbool.rs new file mode 100644 index 0000000..e78743b --- /dev/null +++ b/tests/ui/ensure-nonbool.rs @@ -0,0 +1,13 @@ +use anyhow::{ensure, Result}; + +fn main() -> Result<()> { + ensure!("..."); + + struct Struct(bool); + let mut s = Struct(true); + match &mut s { + Struct(cond) => ensure!(cond), + } + + Ok(()) +} diff --git a/tests/ui/ensure-nonbool.stderr b/tests/ui/ensure-nonbool.stderr new file mode 100644 index 0000000..7aeda2c --- /dev/null +++ b/tests/ui/ensure-nonbool.stderr @@ -0,0 +1,15 @@ +error[E0600]: cannot apply unary operator `!` to type `&'static str` + --> tests/ui/ensure-nonbool.rs:4:5 + | +4 | ensure!("..."); + | ^^^^^^^^^^^^^^ cannot apply unary operator `!` + | + = note: this error originates in the macro `$crate::__fallback_ensure` which comes from the expansion of the macro `ensure` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0600]: cannot apply unary operator `!` to type `&mut bool` + --> tests/ui/ensure-nonbool.rs:9:25 + | +9 | Struct(cond) => ensure!(cond), + | ^^^^^^^^^^^^^ cannot apply unary operator `!` + | + = note: this error originates in the macro `$crate::__fallback_ensure` which comes from the expansion of the macro `ensure` (in Nightly builds, run with -Z macro-backtrace for more info) From eb8becf66af4bd81ac500f82f66ea9169ee12252 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Fri, 17 May 2024 10:29:37 -0700 Subject: [PATCH 3/3] Ignore match_single_binding clippy lint in test warning: this match could be written as a `let` statement --> tests/test_macros.rs:63:9 | 63 | / match s { 64 | | Struct { condition } => ensure!(condition), // &bool 65 | | } | |_________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#match_single_binding = note: `#[warn(clippy::match_single_binding)]` on by default help: consider using a `let` statement | 63 ~ let Struct { condition } = s; 64 + ensure!(condition); | --- tests/test_macros.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_macros.rs b/tests/test_macros.rs index 9072200..d5486e6 100644 --- a/tests/test_macros.rs +++ b/tests/test_macros.rs @@ -3,6 +3,7 @@ clippy::eq_op, clippy::incompatible_msrv, // https://github.com/rust-lang/rust-clippy/issues/12257 clippy::items_after_statements, + clippy::match_single_binding, clippy::needless_pass_by_value, clippy::shadow_unrelated, clippy::wildcard_imports