]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/identity_op.rs
Auto merge of #96906 - tbu-:pr_stabilize_to_ipv4_mapped, r=dtolnay
[rust.git] / src / tools / clippy / tests / ui / identity_op.rs
1 use std::fmt::Write as _;
2
3 const ONE: i64 = 1;
4 const NEG_ONE: i64 = -1;
5 const ZERO: i64 = 0;
6
7 struct A(String);
8
9 impl std::ops::Shl<i32> for A {
10     type Output = A;
11     fn shl(mut self, other: i32) -> Self {
12         let _ = write!(self.0, "{}", other);
13         self
14     }
15 }
16
17 struct Length(u8);
18 struct Meter;
19
20 impl core::ops::Mul<Meter> for u8 {
21     type Output = Length;
22     fn mul(self, _: Meter) -> Length {
23         Length(self)
24     }
25 }
26
27 #[allow(
28     clippy::eq_op,
29     clippy::no_effect,
30     clippy::unnecessary_operation,
31     clippy::op_ref,
32     clippy::double_parens
33 )]
34 #[warn(clippy::identity_op)]
35 #[rustfmt::skip]
36 fn main() {
37     let x = 0;
38
39     x + 0;
40     x + (1 - 1);
41     x + 1;
42     0 + x;
43     1 + x;
44     x - ZERO; //no error, as we skip lookups (for now)
45     x | (0);
46     ((ZERO)) | x; //no error, as we skip lookups (for now)
47
48     x * 1;
49     1 * x;
50     x / ONE; //no error, as we skip lookups (for now)
51
52     x / 2; //no false positive
53
54     x & NEG_ONE; //no error, as we skip lookups (for now)
55     -1 & x;
56
57     let u: u8 = 0;
58     u & 255;
59
60     1 << 0; // no error, this case is allowed, see issue 3430
61     42 << 0;
62     1 >> 0;
63     42 >> 0;
64     &x >> 0;
65     x >> &0;
66
67     let mut a = A("".into());
68     let b = a << 0; // no error: non-integer
69
70     1 * Meter; // no error: non-integer
71
72     2 % 3;
73     -2 % 3;
74     2 % -3 + x;
75     -2 % -3 + x;
76     x + 1 % 3;
77     (x + 1) % 3; // no error
78     4 % 3; // no error
79     4 % -3; // no error
80
81     // See #8724
82     let a = 0;
83     let b = true;
84     0 + if b { 1 } else { 2 };
85     0 + if b { 1 } else { 2 } + if b { 3 } else { 4 }; // no error
86     0 + match a { 0 => 10, _ => 20 };
87     0 + match a { 0 => 10, _ => 20 } + match a { 0 => 30, _ => 40 }; // no error
88     0 + if b { 1 } else { 2 } + match a { 0 => 30, _ => 40 }; // no error
89     0 + match a { 0 => 10, _ => 20 } + if b { 3 } else { 4 }; // no error
90     
91     0 + if b { 0 + 1 } else { 2 };
92     0 + match a { 0 =>  0 + 10, _ => 20 };
93     0 + if b { 0 + 1 } else { 2 } + match a { 0 => 0 + 30, _ => 40 };
94
95     let _ = 0 + if 0 + 1 > 0 { 1 } else { 2 } + if 0 + 1 > 0 { 3 } else { 4 };
96     let _ = 0 + match 0 + 1 { 0 => 10, _ => 20 } + match 0 + 1  { 0 => 30, _ => 40 };
97
98     0 + if b { 1 } else { 2 } + if b { 3 } else { 4 } + 0;
99     
100     0 + { a } + 3; // no error
101     0 + loop { let mut c = 0; if c == 10 { break c; } c += 1; } + { a * 2 }; // no error
102     
103     fn f(_: i32) {
104         todo!();
105     }
106     f(1 * a + { 8 * 5 });
107     f(0 + if b { 1 } else { 2 } + 3); // no error
108     const _: i32 = { 2 * 4 } + 0 + 3;
109     const _: i32 = 0 + { 1 + 2 * 3 } + 3; // no error
110 }