]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/eq_op.rs
Rollup merge of #102454 - chenyukang:fix-102396-missing-parentheses, r=lcnr
[rust.git] / src / tools / clippy / tests / ui / eq_op.rs
1 // compile-flags: --test
2
3 #![warn(clippy::eq_op)]
4 #![allow(clippy::double_parens, clippy::identity_op, clippy::nonminimal_bool)]
5
6 fn main() {
7     // simple values and comparisons
8     let _ = 1 == 1;
9     let _ = "no" == "no";
10     // even though I agree that no means no ;-)
11     let _ = false != false;
12     let _ = 1.5 < 1.5;
13     let _ = 1u64 >= 1u64;
14
15     // casts, methods, parentheses
16     let _ = (1u32 as u64) & (1u32 as u64);
17     #[rustfmt::skip]
18     {
19         let _ = 1 ^ ((((((1))))));
20     };
21
22     // unary and binary operators
23     let _ = (-(2) < -(2));
24     let _ = ((1 + 1) & (1 + 1) == (1 + 1) & (1 + 1));
25     let _ = (1 * 2) + (3 * 4) == 1 * 2 + 3 * 4;
26
27     // various other things
28     let _ = ([1] != [1]);
29     let _ = ((1, 2) != (1, 2));
30     let _ = vec![1, 2, 3] == vec![1, 2, 3]; //no error yet, as we don't match macros
31
32     // const folding
33     let _ = 1 + 1 == 2;
34     let _ = 1 - 1 == 0;
35
36     let _ = 1 - 1;
37     let _ = 1 / 1;
38     let _ = true && true;
39
40     let _ = true || true;
41
42     let a: u32 = 0;
43     let b: u32 = 0;
44
45     let _ = a == b && b == a;
46     let _ = a != b && b != a;
47     let _ = a < b && b > a;
48     let _ = a <= b && b >= a;
49
50     let mut a = vec![1];
51     let _ = a == a;
52     let _ = 2 * a.len() == 2 * a.len(); // ok, functions
53     let _ = a.pop() == a.pop(); // ok, functions
54
55     check_ignore_macro();
56
57     // named constants
58     const A: u32 = 10;
59     const B: u32 = 10;
60     const C: u32 = A / B; // ok, different named constants
61     const D: u32 = A / A;
62 }
63
64 macro_rules! check_if_named_foo {
65     ($expression:expr) => {
66         if stringify!($expression) == "foo" {
67             println!("foo!");
68         } else {
69             println!("not foo.");
70         }
71     };
72 }
73
74 macro_rules! bool_macro {
75     ($expression:expr) => {
76         true
77     };
78 }
79
80 fn check_ignore_macro() {
81     check_if_named_foo!(foo);
82     // checks if the lint ignores macros with `!` operator
83     let _ = !bool_macro!(1) && !bool_macro!("");
84 }
85
86 struct Nested {
87     inner: ((i32,), (i32,), (i32,)),
88 }
89
90 fn check_nested(n1: &Nested, n2: &Nested) -> bool {
91     // `n2.inner.0.0` mistyped as `n1.inner.0.0`
92     (n1.inner.0).0 == (n1.inner.0).0 && (n1.inner.1).0 == (n2.inner.1).0 && (n1.inner.2).0 == (n2.inner.2).0
93 }
94
95 #[test]
96 fn eq_op_shouldnt_trigger_in_tests() {
97     let a = 1;
98     let result = a + 1 == 1 + a;
99     assert!(result);
100 }
101
102 #[test]
103 fn eq_op_macros_shouldnt_trigger_in_tests() {
104     let a = 1;
105     let b = 2;
106     assert_eq!(a, a);
107     assert_eq!(a + b, b + a);
108 }