]> git.lizzy.rs Git - rust.git/blob - tests/ui/match_expr_like_matches_macro.fixed
match_like_matches_macro: strip refs in suggestion
[rust.git] / 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)]
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         let _ans = !matches!(x, E::B(_) | E::C);
51     }
52     {
53         // no lint
54         let _ans = match x {
55             E::A(_) => false,
56             E::B(_) => false,
57             E::C => true,
58             _ => true,
59         };
60     }
61     {
62         // no lint
63         let _ans = match x {
64             E::A(_) => true,
65             E::B(_) => false,
66             E::C => false,
67             _ => true,
68         };
69     }
70     {
71         // no lint
72         let _ans = match x {
73             E::A(a) if a < 10 => false,
74             E::B(a) if a < 10 => false,
75             _ => true,
76         };
77     }
78     {
79         // no lint
80         let _ans = match x {
81             E::A(_) => false,
82             E::B(a) if a < 10 => false,
83             _ => true,
84         };
85     }
86     {
87         // no lint
88         let _ans = match x {
89             E::A(a) => a == 10,
90             E::B(_) => false,
91             _ => true,
92         };
93     }
94     {
95         // no lint
96         let _ans = match x {
97             E::A(_) => false,
98             E::B(_) => true,
99             _ => false,
100         };
101     }
102
103     {
104         // should print "z" in suggestion (#6503)
105         let z = &Some(3);
106         let _z = matches!(z, Some(3));
107     }
108
109     {
110         // this could also print "z" in suggestion..?
111         let z = Some(3);
112         let _z = matches!(&z, Some(3));
113     }
114
115     {
116         enum AnEnum {
117             X,
118             Y,
119         }
120
121         fn foo(_x: AnEnum) {}
122
123         fn main() {
124             let z = AnEnum::X;
125             // we can't remove the reference here!
126             let _ = matches!(&z, AnEnum::X);
127             foo(z);
128         }
129     }
130
131     {
132         struct S(i32);
133
134         fn fun(_val: Option<S>) {}
135         let val = Some(S(42));
136         // we need the reference here because later val is consumed by fun()
137         let _res = matches!(&val, &Some(ref _a));
138         fun(val);
139     }
140
141     {
142         struct S(i32);
143
144         fn fun(_val: Option<S>) {}
145         let val = Some(S(42));
146         let _res = matches!(&val, &Some(ref _a));
147         fun(val);
148     }
149 }