2024-03-21 21:20:40 +00:00
|
|
|
#![allow(unused, clippy::assertions_on_constants, clippy::const_is_empty)]
|
2021-04-22 09:31:13 +00:00
|
|
|
#![warn(clippy::bool_assert_comparison)]
|
|
|
|
|
2021-09-08 14:31:47 +00:00
|
|
|
use std::ops::Not;
|
|
|
|
|
2021-04-22 09:31:13 +00:00
|
|
|
macro_rules! a {
|
|
|
|
() => {
|
|
|
|
true
|
|
|
|
};
|
|
|
|
}
|
|
|
|
macro_rules! b {
|
|
|
|
() => {
|
|
|
|
true
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-09-08 14:31:47 +00:00
|
|
|
// Implements the Not trait but with an output type
|
|
|
|
// that's not bool. Should not suggest a rewrite
|
2023-01-27 20:09:08 +00:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
2021-09-08 14:31:47 +00:00
|
|
|
enum ImplNotTraitWithoutBool {
|
|
|
|
VariantX(bool),
|
|
|
|
VariantY(u32),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialEq<bool> for ImplNotTraitWithoutBool {
|
|
|
|
fn eq(&self, other: &bool) -> bool {
|
|
|
|
match *self {
|
|
|
|
ImplNotTraitWithoutBool::VariantX(b) => b == *other,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Not for ImplNotTraitWithoutBool {
|
|
|
|
type Output = Self;
|
|
|
|
|
|
|
|
fn not(self) -> Self::Output {
|
|
|
|
match self {
|
|
|
|
ImplNotTraitWithoutBool::VariantX(b) => ImplNotTraitWithoutBool::VariantX(!b),
|
|
|
|
ImplNotTraitWithoutBool::VariantY(0) => ImplNotTraitWithoutBool::VariantY(1),
|
|
|
|
ImplNotTraitWithoutBool::VariantY(_) => ImplNotTraitWithoutBool::VariantY(0),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This type implements the Not trait with an Output of
|
|
|
|
// type bool. Using assert!(..) must be suggested
|
2023-01-27 20:09:08 +00:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
2021-09-08 14:31:47 +00:00
|
|
|
struct ImplNotTraitWithBool;
|
|
|
|
|
|
|
|
impl PartialEq<bool> for ImplNotTraitWithBool {
|
|
|
|
fn eq(&self, other: &bool) -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Not for ImplNotTraitWithBool {
|
|
|
|
type Output = bool;
|
|
|
|
|
|
|
|
fn not(self) -> Self::Output {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-27 20:09:08 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
struct NonCopy;
|
|
|
|
|
|
|
|
impl PartialEq<bool> for NonCopy {
|
|
|
|
fn eq(&self, other: &bool) -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Not for NonCopy {
|
|
|
|
type Output = bool;
|
|
|
|
|
|
|
|
fn not(self) -> Self::Output {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-22 09:31:13 +00:00
|
|
|
fn main() {
|
2021-09-08 14:31:47 +00:00
|
|
|
let a = ImplNotTraitWithoutBool::VariantX(true);
|
|
|
|
let b = ImplNotTraitWithBool;
|
|
|
|
|
2021-04-22 09:31:13 +00:00
|
|
|
assert_eq!("a".len(), 1);
|
|
|
|
assert_eq!("a".is_empty(), false);
|
|
|
|
assert_eq!("".is_empty(), true);
|
|
|
|
assert_eq!(true, "".is_empty());
|
|
|
|
assert_eq!(a!(), b!());
|
|
|
|
assert_eq!(a!(), "".is_empty());
|
|
|
|
assert_eq!("".is_empty(), b!());
|
2021-09-08 14:31:47 +00:00
|
|
|
assert_eq!(a, true);
|
|
|
|
assert_eq!(b, true);
|
2021-04-22 09:31:13 +00:00
|
|
|
|
|
|
|
assert_ne!("a".len(), 1);
|
|
|
|
assert_ne!("a".is_empty(), false);
|
|
|
|
assert_ne!("".is_empty(), true);
|
|
|
|
assert_ne!(true, "".is_empty());
|
|
|
|
assert_ne!(a!(), b!());
|
|
|
|
assert_ne!(a!(), "".is_empty());
|
|
|
|
assert_ne!("".is_empty(), b!());
|
2021-09-08 14:31:47 +00:00
|
|
|
assert_ne!(a, true);
|
|
|
|
assert_ne!(b, true);
|
2021-04-22 09:31:13 +00:00
|
|
|
|
|
|
|
debug_assert_eq!("a".len(), 1);
|
|
|
|
debug_assert_eq!("a".is_empty(), false);
|
|
|
|
debug_assert_eq!("".is_empty(), true);
|
|
|
|
debug_assert_eq!(true, "".is_empty());
|
|
|
|
debug_assert_eq!(a!(), b!());
|
|
|
|
debug_assert_eq!(a!(), "".is_empty());
|
|
|
|
debug_assert_eq!("".is_empty(), b!());
|
2021-09-08 14:31:47 +00:00
|
|
|
debug_assert_eq!(a, true);
|
|
|
|
debug_assert_eq!(b, true);
|
2021-04-22 09:31:13 +00:00
|
|
|
|
|
|
|
debug_assert_ne!("a".len(), 1);
|
|
|
|
debug_assert_ne!("a".is_empty(), false);
|
|
|
|
debug_assert_ne!("".is_empty(), true);
|
|
|
|
debug_assert_ne!(true, "".is_empty());
|
|
|
|
debug_assert_ne!(a!(), b!());
|
|
|
|
debug_assert_ne!(a!(), "".is_empty());
|
|
|
|
debug_assert_ne!("".is_empty(), b!());
|
2021-09-08 14:31:47 +00:00
|
|
|
debug_assert_ne!(a, true);
|
|
|
|
debug_assert_ne!(b, true);
|
2021-04-22 09:31:13 +00:00
|
|
|
|
|
|
|
// assert with error messages
|
|
|
|
assert_eq!("a".len(), 1, "tadam {}", 1);
|
|
|
|
assert_eq!("a".len(), 1, "tadam {}", true);
|
|
|
|
assert_eq!("a".is_empty(), false, "tadam {}", 1);
|
|
|
|
assert_eq!("a".is_empty(), false, "tadam {}", true);
|
|
|
|
assert_eq!(false, "a".is_empty(), "tadam {}", true);
|
2021-09-08 14:31:47 +00:00
|
|
|
assert_eq!(a, true, "tadam {}", false);
|
2021-04-22 09:31:13 +00:00
|
|
|
|
|
|
|
debug_assert_eq!("a".len(), 1, "tadam {}", 1);
|
|
|
|
debug_assert_eq!("a".len(), 1, "tadam {}", true);
|
|
|
|
debug_assert_eq!("a".is_empty(), false, "tadam {}", 1);
|
|
|
|
debug_assert_eq!("a".is_empty(), false, "tadam {}", true);
|
|
|
|
debug_assert_eq!(false, "a".is_empty(), "tadam {}", true);
|
2021-09-08 14:31:47 +00:00
|
|
|
debug_assert_eq!(a, true, "tadam {}", false);
|
2023-01-27 20:09:08 +00:00
|
|
|
|
|
|
|
assert_eq!(a!(), true);
|
|
|
|
assert_eq!(true, b!());
|
|
|
|
|
|
|
|
use debug_assert_eq as renamed;
|
|
|
|
renamed!(a, true);
|
|
|
|
renamed!(b, true);
|
|
|
|
|
|
|
|
let non_copy = NonCopy;
|
|
|
|
assert_eq!(non_copy, true);
|
|
|
|
// changing the above to `assert!(non_copy)` would cause a `borrow of moved value`
|
|
|
|
println!("{non_copy:?}");
|
|
|
|
|
|
|
|
macro_rules! in_macro {
|
|
|
|
($v:expr) => {{
|
|
|
|
assert_eq!($v, true);
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
in_macro!(a);
|
2023-02-10 13:01:19 +00:00
|
|
|
|
|
|
|
assert_eq!("".is_empty(), true);
|
|
|
|
assert_ne!("".is_empty(), false);
|
|
|
|
assert_ne!("requires negation".is_empty(), true);
|
|
|
|
assert_eq!("requires negation".is_empty(), false);
|
|
|
|
|
|
|
|
debug_assert_eq!("".is_empty(), true);
|
|
|
|
debug_assert_ne!("".is_empty(), false);
|
|
|
|
debug_assert_ne!("requires negation".is_empty(), true);
|
|
|
|
debug_assert_eq!("requires negation".is_empty(), false);
|
2021-04-22 09:31:13 +00:00
|
|
|
}
|