]> git.lizzy.rs Git - rust.git/blob - tests/ui/identity_op.rs
Auto merge of #8665 - InfRandomness:option_take_on_temporary, r=llogiq
[rust.git] / 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 }