]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/tests/ui/match_expr_like_matches_macro.rs
New upstream version 1.61.0+dfsg1
[rustc.git] / src / tools / clippy / tests / ui / match_expr_like_matches_macro.rs
1 // run-rustfix
2
3 #![warn(clippy::match_like_matches_macro)]
4 #![allow(unreachable_patterns, dead_code, clippy::equatable_if_let)]
5
6 fn main() {
7 let x = Some(5);
8
9 // Lint
10 let _y = match x {
11 Some(0) => true,
12 _ => false,
13 };
14
15 // Lint
16 let _w = match x {
17 Some(_) => true,
18 _ => false,
19 };
20
21 // Turn into is_none
22 let _z = match x {
23 Some(_) => false,
24 None => true,
25 };
26
27 // Lint
28 let _zz = match x {
29 Some(r) if r == 0 => false,
30 _ => true,
31 };
32
33 // Lint
34 let _zzz = if let Some(5) = x { true } else { false };
35
36 // No lint
37 let _a = match x {
38 Some(_) => false,
39 _ => false,
40 };
41
42 // No lint
43 let _ab = match x {
44 Some(0) => false,
45 _ => true,
46 None => false,
47 };
48
49 enum E {
50 A(u32),
51 B(i32),
52 C,
53 D,
54 }
55 let x = E::A(2);
56 {
57 // lint
58 let _ans = match x {
59 E::A(_) => true,
60 E::B(_) => true,
61 _ => false,
62 };
63 }
64 {
65 // lint
66 let _ans = match x {
67 E::B(_) => false,
68 E::C => false,
69 _ => true,
70 };
71 }
72 {
73 // no lint
74 let _ans = match x {
75 E::A(_) => false,
76 E::B(_) => false,
77 E::C => true,
78 _ => true,
79 };
80 }
81 {
82 // no lint
83 let _ans = match x {
84 E::A(_) => true,
85 E::B(_) => false,
86 E::C => false,
87 _ => true,
88 };
89 }
90 {
91 // no lint
92 let _ans = match x {
93 E::A(a) if a < 10 => false,
94 E::B(a) if a < 10 => false,
95 _ => true,
96 };
97 }
98 {
99 // no lint
100 let _ans = match x {
101 E::A(_) => false,
102 E::B(a) if a < 10 => false,
103 _ => true,
104 };
105 }
106 {
107 // no lint
108 let _ans = match x {
109 E::A(a) => a == 10,
110 E::B(_) => false,
111 _ => true,
112 };
113 }
114 {
115 // no lint
116 let _ans = match x {
117 E::A(_) => false,
118 E::B(_) => true,
119 _ => false,
120 };
121 }
122
123 {
124 // should print "z" in suggestion (#6503)
125 let z = &Some(3);
126 let _z = match &z {
127 Some(3) => true,
128 _ => false,
129 };
130 }
131
132 {
133 // this could also print "z" in suggestion..?
134 let z = Some(3);
135 let _z = match &z {
136 Some(3) => true,
137 _ => false,
138 };
139 }
140
141 {
142 enum AnEnum {
143 X,
144 Y,
145 }
146
147 fn foo(_x: AnEnum) {}
148
149 fn main() {
150 let z = AnEnum::X;
151 // we can't remove the reference here!
152 let _ = match &z {
153 AnEnum::X => true,
154 _ => false,
155 };
156 foo(z);
157 }
158 }
159
160 {
161 struct S(i32);
162
163 fn fun(_val: Option<S>) {}
164 let val = Some(S(42));
165 // we need the reference here because later val is consumed by fun()
166 let _res = match &val {
167 &Some(ref _a) => true,
168 _ => false,
169 };
170 fun(val);
171 }
172
173 {
174 struct S(i32);
175
176 fn fun(_val: Option<S>) {}
177 let val = Some(S(42));
178 let _res = match &val {
179 &Some(ref _a) => true,
180 _ => false,
181 };
182 fun(val);
183 }
184
185 {
186 enum E {
187 A,
188 B,
189 C,
190 }
191
192 let _ = match E::A {
193 E::B => true,
194 #[cfg(feature = "foo")]
195 E::A => true,
196 _ => false,
197 };
198 }
199 }