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