]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/needless_match.rs
Auto merge of #96895 - SparrowLii:interval, r=Mark-Simulacrum
[rust.git] / src / tools / clippy / tests / ui / needless_match.rs
1 // run-rustfix
2 #![warn(clippy::needless_match)]
3 #![allow(clippy::manual_map)]
4 #![allow(dead_code)]
5
6 #[derive(Clone, Copy)]
7 enum Simple {
8     A,
9     B,
10     C,
11     D,
12 }
13
14 fn useless_match() {
15     let i = 10;
16     let _: i32 = match i {
17         0 => 0,
18         1 => 1,
19         2 => 2,
20         _ => i,
21     };
22     let s = "test";
23     let _: &str = match s {
24         "a" => "a",
25         "b" => "b",
26         s => s,
27     };
28 }
29
30 fn custom_type_match() {
31     let se = Simple::A;
32     let _: Simple = match se {
33         Simple::A => Simple::A,
34         Simple::B => Simple::B,
35         Simple::C => Simple::C,
36         Simple::D => Simple::D,
37     };
38     // Don't trigger
39     let _: Simple = match se {
40         Simple::A => Simple::A,
41         Simple::B => Simple::B,
42         _ => Simple::C,
43     };
44     // Mingled, don't trigger
45     let _: Simple = match se {
46         Simple::A => Simple::B,
47         Simple::B => Simple::C,
48         Simple::C => Simple::D,
49         Simple::D => Simple::A,
50     };
51 }
52
53 fn option_match(x: Option<i32>) {
54     let _: Option<i32> = match x {
55         Some(a) => Some(a),
56         None => None,
57     };
58     // Don't trigger, this is the case for manual_map_option
59     let _: Option<i32> = match x {
60         Some(a) => Some(-a),
61         None => None,
62     };
63 }
64
65 fn func_ret_err<T>(err: T) -> Result<i32, T> {
66     Err(err)
67 }
68
69 fn result_match() {
70     let _: Result<i32, i32> = match Ok(1) {
71         Ok(a) => Ok(a),
72         Err(err) => Err(err),
73     };
74     let _: Result<i32, i32> = match func_ret_err(0_i32) {
75         Err(err) => Err(err),
76         Ok(a) => Ok(a),
77     };
78     // as ref, don't trigger
79     let res = &func_ret_err(0_i32);
80     let _: Result<&i32, &i32> = match *res {
81         Ok(ref x) => Ok(x),
82         Err(ref x) => Err(x),
83     };
84 }
85
86 fn if_let_option() {
87     let _ = if let Some(a) = Some(1) { Some(a) } else { None };
88
89     fn do_something() {}
90
91     // Don't trigger
92     let _ = if let Some(a) = Some(1) {
93         Some(a)
94     } else {
95         do_something();
96         None
97     };
98
99     // Don't trigger
100     let _ = if let Some(a) = Some(1) {
101         do_something();
102         Some(a)
103     } else {
104         None
105     };
106
107     // Don't trigger
108     let _ = if let Some(a) = Some(1) { Some(a) } else { Some(2) };
109 }
110
111 fn if_let_option_result() -> Result<(), ()> {
112     fn f(x: i32) -> Result<Option<i32>, ()> {
113         Ok(Some(x))
114     }
115     // Don't trigger
116     let _ = if let Some(v) = f(1)? { Some(v) } else { f(2)? };
117     Ok(())
118 }
119
120 fn if_let_result() {
121     let x: Result<i32, i32> = Ok(1);
122     let _: Result<i32, i32> = if let Err(e) = x { Err(e) } else { x };
123     let _: Result<i32, i32> = if let Ok(val) = x { Ok(val) } else { x };
124     // Input type mismatch, don't trigger
125     let _: Result<i32, i32> = if let Err(e) = Ok(1) { Err(e) } else { x };
126 }
127
128 fn if_let_custom_enum(x: Simple) {
129     let _: Simple = if let Simple::A = x {
130         Simple::A
131     } else if let Simple::B = x {
132         Simple::B
133     } else if let Simple::C = x {
134         Simple::C
135     } else {
136         x
137     };
138
139     // Don't trigger
140     let _: Simple = if let Simple::A = x {
141         Simple::A
142     } else if true {
143         Simple::B
144     } else {
145         x
146     };
147 }
148
149 mod issue8542 {
150     #[derive(Clone, Copy)]
151     enum E {
152         VariantA(u8, u8),
153         VariantB(u8, bool),
154     }
155
156     enum Complex {
157         A(u8),
158         B(u8, bool),
159         C(u8, i32, f64),
160         D(E, bool),
161     }
162
163     fn match_test() {
164         let ce = Complex::B(8, false);
165         let aa = 0_u8;
166         let bb = false;
167
168         let _: Complex = match ce {
169             Complex::A(a) => Complex::A(a),
170             Complex::B(a, b) => Complex::B(a, b),
171             Complex::C(a, b, c) => Complex::C(a, b, c),
172             Complex::D(E::VariantA(ea, eb), b) => Complex::D(E::VariantA(ea, eb), b),
173             Complex::D(E::VariantB(ea, eb), b) => Complex::D(E::VariantB(ea, eb), b),
174         };
175
176         // Don't trigger
177         let _: Complex = match ce {
178             Complex::A(_) => Complex::A(aa),
179             Complex::B(_, b) => Complex::B(aa, b),
180             Complex::C(_, b, _) => Complex::C(aa, b, 64_f64),
181             Complex::D(e, b) => Complex::D(e, b),
182         };
183
184         // Don't trigger
185         let _: Complex = match ce {
186             Complex::A(a) => Complex::A(a),
187             Complex::B(a, _) => Complex::B(a, bb),
188             Complex::C(a, _, _) => Complex::C(a, 32_i32, 64_f64),
189             _ => ce,
190         };
191     }
192 }
193
194 /// Lint triggered when type coercions happen.
195 /// Do NOT trigger on any of these.
196 mod issue8551 {
197     trait Trait {}
198     struct Struct;
199     impl Trait for Struct {}
200
201     fn optmap(s: Option<&Struct>) -> Option<&dyn Trait> {
202         match s {
203             Some(s) => Some(s),
204             None => None,
205         }
206     }
207
208     fn lint_tests() {
209         let option: Option<&Struct> = None;
210         let _: Option<&dyn Trait> = match option {
211             Some(s) => Some(s),
212             None => None,
213         };
214
215         let _: Option<&dyn Trait> = if true {
216             match option {
217                 Some(s) => Some(s),
218                 None => None,
219             }
220         } else {
221             None
222         };
223
224         let result: Result<&Struct, i32> = Err(0);
225         let _: Result<&dyn Trait, i32> = match result {
226             Ok(s) => Ok(s),
227             Err(e) => Err(e),
228         };
229
230         let _: Option<&dyn Trait> = if let Some(s) = option { Some(s) } else { None };
231     }
232 }
233
234 trait Tr {
235     fn as_mut(&mut self) -> Result<&mut i32, &mut i32>;
236 }
237 impl Tr for Result<i32, i32> {
238     fn as_mut(&mut self) -> Result<&mut i32, &mut i32> {
239         match self {
240             Ok(x) => Ok(x),
241             Err(e) => Err(e),
242         }
243     }
244 }
245
246 fn main() {}