2024-03-21 16:20:40 -05:00
|
|
|
#![allow(unused, clippy::assertions_on_constants, clippy::const_is_empty)]
|
2023-01-27 14:09:08 -06:00
|
|
|
#![warn(clippy::bool_assert_comparison)]
|
|
|
|
|
|
|
|
use std::ops::Not;
|
|
|
|
|
|
|
|
macro_rules! a {
|
|
|
|
() => {
|
|
|
|
true
|
|
|
|
};
|
|
|
|
}
|
|
|
|
macro_rules! b {
|
|
|
|
() => {
|
|
|
|
true
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements the Not trait but with an output type
|
|
|
|
// that's not bool. Should not suggest a rewrite
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
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
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let a = ImplNotTraitWithoutBool::VariantX(true);
|
|
|
|
let b = ImplNotTraitWithBool;
|
|
|
|
|
|
|
|
assert_eq!("a".len(), 1);
|
2023-02-10 07:01:19 -06:00
|
|
|
assert!(!"a".is_empty());
|
2023-01-27 14:09:08 -06:00
|
|
|
assert!("".is_empty());
|
|
|
|
assert!("".is_empty());
|
|
|
|
assert_eq!(a!(), b!());
|
|
|
|
assert_eq!(a!(), "".is_empty());
|
|
|
|
assert_eq!("".is_empty(), b!());
|
|
|
|
assert_eq!(a, true);
|
|
|
|
assert!(b);
|
|
|
|
|
|
|
|
assert_ne!("a".len(), 1);
|
|
|
|
assert!("a".is_empty());
|
2023-02-10 07:01:19 -06:00
|
|
|
assert!(!"".is_empty());
|
|
|
|
assert!(!"".is_empty());
|
2023-01-27 14:09:08 -06:00
|
|
|
assert_ne!(a!(), b!());
|
|
|
|
assert_ne!(a!(), "".is_empty());
|
|
|
|
assert_ne!("".is_empty(), b!());
|
|
|
|
assert_ne!(a, true);
|
2023-02-10 07:01:19 -06:00
|
|
|
assert!(!b);
|
2023-01-27 14:09:08 -06:00
|
|
|
|
|
|
|
debug_assert_eq!("a".len(), 1);
|
2023-02-10 07:01:19 -06:00
|
|
|
debug_assert!(!"a".is_empty());
|
2023-01-27 14:09:08 -06:00
|
|
|
debug_assert!("".is_empty());
|
|
|
|
debug_assert!("".is_empty());
|
|
|
|
debug_assert_eq!(a!(), b!());
|
|
|
|
debug_assert_eq!(a!(), "".is_empty());
|
|
|
|
debug_assert_eq!("".is_empty(), b!());
|
|
|
|
debug_assert_eq!(a, true);
|
|
|
|
debug_assert!(b);
|
|
|
|
|
|
|
|
debug_assert_ne!("a".len(), 1);
|
|
|
|
debug_assert!("a".is_empty());
|
2023-02-10 07:01:19 -06:00
|
|
|
debug_assert!(!"".is_empty());
|
|
|
|
debug_assert!(!"".is_empty());
|
2023-01-27 14:09:08 -06:00
|
|
|
debug_assert_ne!(a!(), b!());
|
|
|
|
debug_assert_ne!(a!(), "".is_empty());
|
|
|
|
debug_assert_ne!("".is_empty(), b!());
|
|
|
|
debug_assert_ne!(a, true);
|
2023-02-10 07:01:19 -06:00
|
|
|
debug_assert!(!b);
|
2023-01-27 14:09:08 -06:00
|
|
|
|
|
|
|
// assert with error messages
|
|
|
|
assert_eq!("a".len(), 1, "tadam {}", 1);
|
|
|
|
assert_eq!("a".len(), 1, "tadam {}", true);
|
2023-02-10 07:01:19 -06:00
|
|
|
assert!(!"a".is_empty(), "tadam {}", 1);
|
|
|
|
assert!(!"a".is_empty(), "tadam {}", true);
|
|
|
|
assert!(!"a".is_empty(), "tadam {}", true);
|
2023-01-27 14:09:08 -06:00
|
|
|
assert_eq!(a, true, "tadam {}", false);
|
|
|
|
|
|
|
|
debug_assert_eq!("a".len(), 1, "tadam {}", 1);
|
|
|
|
debug_assert_eq!("a".len(), 1, "tadam {}", true);
|
2023-02-10 07:01:19 -06:00
|
|
|
debug_assert!(!"a".is_empty(), "tadam {}", 1);
|
|
|
|
debug_assert!(!"a".is_empty(), "tadam {}", true);
|
|
|
|
debug_assert!(!"a".is_empty(), "tadam {}", true);
|
2023-01-27 14:09:08 -06:00
|
|
|
debug_assert_eq!(a, true, "tadam {}", false);
|
|
|
|
|
|
|
|
assert!(a!());
|
|
|
|
assert!(b!());
|
|
|
|
|
|
|
|
use debug_assert_eq as renamed;
|
|
|
|
renamed!(a, true);
|
|
|
|
debug_assert!(b);
|
|
|
|
|
|
|
|
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 07:01:19 -06:00
|
|
|
|
|
|
|
assert!("".is_empty());
|
|
|
|
assert!("".is_empty());
|
|
|
|
assert!(!"requires negation".is_empty());
|
|
|
|
assert!(!"requires negation".is_empty());
|
|
|
|
|
|
|
|
debug_assert!("".is_empty());
|
|
|
|
debug_assert!("".is_empty());
|
|
|
|
debug_assert!(!"requires negation".is_empty());
|
|
|
|
debug_assert!(!"requires negation".is_empty());
|
2023-01-27 14:09:08 -06:00
|
|
|
}
|