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