]> git.lizzy.rs Git - rust.git/blob - tests/ui/eq_op.rs
Add iterator test case for `eq_op` lint
[rust.git] / tests / ui / eq_op.rs
1 // does not test any rustfixable lints
2
3 #[rustfmt::skip]
4 #[warn(clippy::eq_op)]
5 #[allow(clippy::identity_op, clippy::double_parens, clippy::many_single_char_names)]
6 #[allow(clippy::no_effect, unused_variables, clippy::unnecessary_operation, clippy::short_circuit_statement)]
7 #[allow(clippy::nonminimal_bool)]
8 #[allow(unused)]
9 fn main() {
10     // simple values and comparisons
11     1 == 1;
12     "no" == "no";
13     // even though I agree that no means no ;-)
14     false != false;
15     1.5 < 1.5;
16     1u64 >= 1u64;
17
18     // casts, methods, parentheses
19     (1 as u64) & (1 as u64);
20     1 ^ ((((((1))))));
21
22     // unary and binary operators
23     (-(2) < -(2));
24     ((1 + 1) & (1 + 1) == (1 + 1) & (1 + 1));
25     (1 * 2) + (3 * 4) == 1 * 2 + 3 * 4;
26
27     // various other things
28     ([1] != [1]);
29     ((1, 2) != (1, 2));
30     vec![1, 2, 3] == vec![1, 2, 3]; //no error yet, as we don't match macros
31
32     // const folding
33     1 + 1 == 2;
34     1 - 1 == 0;
35
36     1 - 1;
37     1 / 1;
38     true && true;
39
40     true || true;
41
42
43     let a: u32 = 0;
44     let b: u32 = 0;
45
46     a == b && b == a;
47     a != b && b != a;
48     a < b && b > a;
49     a <= b && b >= a;
50
51     let mut a = vec![1];
52     a == a;
53     2*a.len() == 2*a.len(); // ok, functions
54     a.pop() == a.pop(); // ok, functions
55
56     check_ignore_macro();
57
58     // named constants
59     const A: u32 = 10;
60     const B: u32 = 10;
61     const C: u32 = A / B; // ok, different named constants
62     const D: u32 = A / A;
63
64     check_assert_identical_args();
65 }
66
67 #[rustfmt::skip]
68 macro_rules! check_if_named_foo {
69     ($expression:expr) => (
70         if stringify!($expression) == "foo" {
71             println!("foo!");
72         } else {
73             println!("not foo.");
74         }
75     )
76 }
77
78 macro_rules! bool_macro {
79     ($expression:expr) => {
80         true
81     };
82 }
83
84 #[allow(clippy::short_circuit_statement)]
85 fn check_ignore_macro() {
86     check_if_named_foo!(foo);
87     // checks if the lint ignores macros with `!` operator
88     !bool_macro!(1) && !bool_macro!("");
89 }
90
91 macro_rules! assert_in_macro_def {
92     () => {
93         let a = 42;
94         assert_eq!(a, a);
95         assert_ne!(a, a);
96         debug_assert_eq!(a, a);
97         debug_assert_ne!(a, a);
98     };
99 }
100
101 // lint identical args in assert-like macro invocations (see #3574)
102 fn check_assert_identical_args() {
103     // lint also in macro definition
104     assert_in_macro_def!();
105
106     let a = 1;
107     let b = 2;
108
109     // lint identical args in `assert_eq!`
110     assert_eq!(a, a);
111     assert_eq!(a + 1, a + 1);
112     // ok
113     assert_eq!(a, b);
114     assert_eq!(a, a + 1);
115     assert_eq!(a + 1, b + 1);
116
117     // lint identical args in `assert_ne!`
118     assert_ne!(a, a);
119     assert_ne!(a + 1, a + 1);
120     // ok
121     assert_ne!(a, b);
122     assert_ne!(a, a + 1);
123     assert_ne!(a + 1, b + 1);
124
125     // lint identical args in `debug_assert_eq!`
126     debug_assert_eq!(a, a);
127     debug_assert_eq!(a + 1, a + 1);
128     // ok
129     debug_assert_eq!(a, b);
130     debug_assert_eq!(a, a + 1);
131     debug_assert_eq!(a + 1, b + 1);
132
133     // lint identical args in `debug_assert_ne!`
134     debug_assert_ne!(a, a);
135     debug_assert_ne!(a + 1, a + 1);
136     // ok
137     debug_assert_ne!(a, b);
138     debug_assert_ne!(a, a + 1);
139     debug_assert_ne!(a + 1, b + 1);
140
141     let my_vec = vec![1; 5];
142     let mut my_iter = my_vec.iter();
143     assert_ne!(my_iter.next(), my_iter.next());
144 }