]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/match_expr_like_matches_macro.fixed
Merge commit 'fdb84cbfd25908df5683f8f62388f663d9260e39' into clippyup
[rust.git] / src / tools / clippy / tests / ui / match_expr_like_matches_macro.fixed
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 = matches!(x, Some(0));
11
12     // Lint
13     let _w = matches!(x, Some(_));
14
15     // Turn into is_none
16     let _z = x.is_none();
17
18     // Lint
19     let _zz = !matches!(x, Some(r) if r == 0);
20
21     // Lint
22     let _zzz = matches!(x, Some(5));
23
24     // No lint
25     let _a = match x {
26         Some(_) => false,
27         _ => false,
28     };
29
30     // No lint
31     let _ab = match x {
32         Some(0) => false,
33         _ => true,
34         None => false,
35     };
36
37     enum E {
38         A(u32),
39         B(i32),
40         C,
41         D,
42     }
43     let x = E::A(2);
44     {
45         // lint
46         let _ans = matches!(x, E::A(_) | E::B(_));
47     }
48     {
49         // lint
50         // skip rustfmt to prevent removing block for first pattern
51         #[rustfmt::skip]
52         let _ans = matches!(x, E::A(_) | E::B(_));
53     }
54     {
55         // lint
56         let _ans = !matches!(x, E::B(_) | E::C);
57     }
58     {
59         // no lint
60         let _ans = match x {
61             E::A(_) => false,
62             E::B(_) => false,
63             E::C => true,
64             _ => true,
65         };
66     }
67     {
68         // no lint
69         let _ans = match x {
70             E::A(_) => true,
71             E::B(_) => false,
72             E::C => false,
73             _ => true,
74         };
75     }
76     {
77         // no lint
78         let _ans = match x {
79             E::A(a) if a < 10 => false,
80             E::B(a) if a < 10 => false,
81             _ => true,
82         };
83     }
84     {
85         // no lint
86         let _ans = match x {
87             E::A(_) => false,
88             E::B(a) if a < 10 => false,
89             _ => true,
90         };
91     }
92     {
93         // no lint
94         let _ans = match x {
95             E::A(a) => a == 10,
96             E::B(_) => false,
97             _ => true,
98         };
99     }
100     {
101         // no lint
102         let _ans = match x {
103             E::A(_) => false,
104             E::B(_) => true,
105             _ => false,
106         };
107     }
108
109     {
110         // should print "z" in suggestion (#6503)
111         let z = &Some(3);
112         let _z = matches!(z, Some(3));
113     }
114
115     {
116         // this could also print "z" in suggestion..?
117         let z = Some(3);
118         let _z = matches!(&z, Some(3));
119     }
120
121     {
122         enum AnEnum {
123             X,
124             Y,
125         }
126
127         fn foo(_x: AnEnum) {}
128
129         fn main() {
130             let z = AnEnum::X;
131             // we can't remove the reference here!
132             let _ = matches!(&z, AnEnum::X);
133             foo(z);
134         }
135     }
136
137     {
138         struct S(i32);
139
140         fn fun(_val: Option<S>) {}
141         let val = Some(S(42));
142         // we need the reference here because later val is consumed by fun()
143         let _res = matches!(&val, &Some(ref _a));
144         fun(val);
145     }
146
147     {
148         struct S(i32);
149
150         fn fun(_val: Option<S>) {}
151         let val = Some(S(42));
152         let _res = matches!(&val, &Some(ref _a));
153         fun(val);
154     }
155
156     {
157         enum E {
158             A,
159             B,
160             C,
161         }
162
163         let _ = match E::A {
164             E::B => true,
165             #[cfg(feature = "foo")]
166             E::A => true,
167             _ => false,
168         };
169     }
170 }