fork/rust

git clone https://git.julia.blue/fork/rust

borsAuto merge of #103046 - JanBeh:PR_clarify_cmp_terminology, r=workingjubilee3bcad65fbf6

master
4.2 KiB169 linesraw
1#![allow(unused, clippy::assertions_on_constants)]
2#![warn(clippy::bool_assert_comparison)]
3
4use std::ops::Not;
5
6macro_rules! a {
7    () => {
8        true
9    };
10}
11macro_rules! b {
12    () => {
13        true
14    };
15}
16
17// Implements the Not trait but with an output type
18// that's not bool. Should not suggest a rewrite
19#[derive(Debug, Clone, Copy)]
20enum ImplNotTraitWithoutBool {
21    VariantX(bool),
22    VariantY(u32),
23}
24
25impl PartialEq<bool> for ImplNotTraitWithoutBool {
26    fn eq(&self, other: &bool) -> bool {
27        match *self {
28            ImplNotTraitWithoutBool::VariantX(b) => b == *other,
29            _ => false,
30        }
31    }
32}
33
34impl Not for ImplNotTraitWithoutBool {
35    type Output = Self;
36
37    fn not(self) -> Self::Output {
38        match self {
39            ImplNotTraitWithoutBool::VariantX(b) => ImplNotTraitWithoutBool::VariantX(!b),
40            ImplNotTraitWithoutBool::VariantY(0) => ImplNotTraitWithoutBool::VariantY(1),
41            ImplNotTraitWithoutBool::VariantY(_) => ImplNotTraitWithoutBool::VariantY(0),
42        }
43    }
44}
45
46// This type implements the Not trait with an Output of
47// type bool. Using assert!(..) must be suggested
48#[derive(Debug, Clone, Copy)]
49struct ImplNotTraitWithBool;
50
51impl PartialEq<bool> for ImplNotTraitWithBool {
52    fn eq(&self, other: &bool) -> bool {
53        false
54    }
55}
56
57impl Not for ImplNotTraitWithBool {
58    type Output = bool;
59
60    fn not(self) -> Self::Output {
61        true
62    }
63}
64
65#[derive(Debug)]
66struct NonCopy;
67
68impl PartialEq<bool> for NonCopy {
69    fn eq(&self, other: &bool) -> bool {
70        false
71    }
72}
73
74impl Not for NonCopy {
75    type Output = bool;
76
77    fn not(self) -> Self::Output {
78        true
79    }
80}
81
82fn main() {
83    let a = ImplNotTraitWithoutBool::VariantX(true);
84    let b = ImplNotTraitWithBool;
85
86    assert_eq!("a".len(), 1);
87    assert!(!"a".is_empty());
88    assert!("".is_empty());
89    assert!("".is_empty());
90    assert_eq!(a!(), b!());
91    assert_eq!(a!(), "".is_empty());
92    assert_eq!("".is_empty(), b!());
93    assert_eq!(a, true);
94    assert!(b);
95
96    assert_ne!("a".len(), 1);
97    assert!("a".is_empty());
98    assert!(!"".is_empty());
99    assert!(!"".is_empty());
100    assert_ne!(a!(), b!());
101    assert_ne!(a!(), "".is_empty());
102    assert_ne!("".is_empty(), b!());
103    assert_ne!(a, true);
104    assert!(!b);
105
106    debug_assert_eq!("a".len(), 1);
107    debug_assert!(!"a".is_empty());
108    debug_assert!("".is_empty());
109    debug_assert!("".is_empty());
110    debug_assert_eq!(a!(), b!());
111    debug_assert_eq!(a!(), "".is_empty());
112    debug_assert_eq!("".is_empty(), b!());
113    debug_assert_eq!(a, true);
114    debug_assert!(b);
115
116    debug_assert_ne!("a".len(), 1);
117    debug_assert!("a".is_empty());
118    debug_assert!(!"".is_empty());
119    debug_assert!(!"".is_empty());
120    debug_assert_ne!(a!(), b!());
121    debug_assert_ne!(a!(), "".is_empty());
122    debug_assert_ne!("".is_empty(), b!());
123    debug_assert_ne!(a, true);
124    debug_assert!(!b);
125
126    // assert with error messages
127    assert_eq!("a".len(), 1, "tadam {}", 1);
128    assert_eq!("a".len(), 1, "tadam {}", true);
129    assert!(!"a".is_empty(), "tadam {}", 1);
130    assert!(!"a".is_empty(), "tadam {}", true);
131    assert!(!"a".is_empty(), "tadam {}", true);
132    assert_eq!(a, true, "tadam {}", false);
133
134    debug_assert_eq!("a".len(), 1, "tadam {}", 1);
135    debug_assert_eq!("a".len(), 1, "tadam {}", true);
136    debug_assert!(!"a".is_empty(), "tadam {}", 1);
137    debug_assert!(!"a".is_empty(), "tadam {}", true);
138    debug_assert!(!"a".is_empty(), "tadam {}", true);
139    debug_assert_eq!(a, true, "tadam {}", false);
140
141    assert!(a!());
142    assert!(b!());
143
144    use debug_assert_eq as renamed;
145    renamed!(a, true);
146    debug_assert!(b);
147
148    let non_copy = NonCopy;
149    assert_eq!(non_copy, true);
150    // changing the above to `assert!(non_copy)` would cause a `borrow of moved value`
151    println!("{non_copy:?}");
152
153    macro_rules! in_macro {
154        ($v:expr) => {{
155            assert_eq!($v, true);
156        }};
157    }
158    in_macro!(a);
159
160    assert!("".is_empty());
161    assert!("".is_empty());
162    assert!(!"requires negation".is_empty());
163    assert!(!"requires negation".is_empty());
164
165    debug_assert!("".is_empty());
166    debug_assert!("".is_empty());
167    debug_assert!(!"requires negation".is_empty());
168    debug_assert!(!"requires negation".is_empty());
169}