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