Skip to content

Commit

Permalink
Merge pull request #365 from dtolnay/ensuretype
Browse files Browse the repository at this point in the history
Add tests for `ensure!` with non-bool argument
  • Loading branch information
dtolnay committed May 17, 2024
2 parents 96f0392 + eb8becf commit 52a46eb
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
19 changes: 18 additions & 1 deletion tests/test_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -11,7 +12,7 @@
mod common;

use self::common::*;
use anyhow::{anyhow, ensure};
use anyhow::{anyhow, ensure, Result};
use std::cell::Cell;
use std::future;

Expand Down Expand Up @@ -53,6 +54,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) {}
Expand Down
13 changes: 13 additions & 0 deletions tests/ui/ensure-nonbool.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
15 changes: 15 additions & 0 deletions tests/ui/ensure-nonbool.stderr
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 52a46eb

Please sign in to comment.