]> git.lizzy.rs Git - rust.git/blob - tests/ui/identity_op.fixed
Auto merge of #9684 - kraktus:ref_option_ref, r=xFrednet
[rust.git] / tests / ui / identity_op.fixed
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;
44     x;
45     x + 1;
46     x;
47     1 + x;
48     x - ZERO; //no error, as we skip lookups (for now)
49     x;
50     ((ZERO)) | x; //no error, as we skip lookups (for now)
51
52     x;
53     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     x;
60
61     let u: u8 = 0;
62     u;
63
64     1 << 0; // no error, this case is allowed, see issue 3430
65     42;
66     1;
67     42;
68     &x;
69     x;
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;
77     -2;
78     2 + x;
79     -2 + x;
80     x + 1;
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     (if b { 1 } else { 2 });
89     (if b { 1 } else { 2 }) + if b { 3 } else { 4 };
90     (match a { 0 => 10, _ => 20 });
91     (match a { 0 => 10, _ => 20 }) + match a { 0 => 30, _ => 40 };
92     (if b { 1 } else { 2 }) + match a { 0 => 30, _ => 40 };
93     (match a { 0 => 10, _ => 20 }) + if b { 3 } else { 4 };
94     (if b { 1 } else { 2 });
95
96     ({ a }) + 3;
97     ({ a } * 2);
98     (loop { let mut c = 0; if c == 10 { break c; } c += 1; }) + { a * 2 };
99
100     fn f(_: i32) {
101         todo!();
102     }
103     f(a + { 8 * 5 });
104     f(if b { 1 } else { 2 } + 3);
105     const _: i32 = { 2 * 4 } + 3;
106     const _: i32 = { 1 + 2 * 3 } + 3;
107
108     a as usize;
109     let _ = a as usize;
110     ({ a } as usize);
111
112     2 * { a };
113     (({ a } + 4));
114     1;
115 }
116
117 pub fn decide(a: bool, b: bool) -> u32 {
118     (if a { 1 } else { 2 }) + if b { 3 } else { 5 }
119 }