]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/needless_match.fixed
Rollup merge of #97236 - cjgillot:recover-lifetime-res, r=jackh726
[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     let _: Result<i32, i32> = if let Err(e) = Ok(1) { Err(e) } else { x };
103 }
104
105 fn if_let_custom_enum(x: Simple) {
106     let _: Simple = x;
107
108     // Don't trigger
109     let _: Simple = if let Simple::A = x {
110         Simple::A
111     } else if true {
112         Simple::B
113     } else {
114         x
115     };
116 }
117
118 mod issue8542 {
119     #[derive(Clone, Copy)]
120     enum E {
121         VariantA(u8, u8),
122         VariantB(u8, bool),
123     }
124
125     enum Complex {
126         A(u8),
127         B(u8, bool),
128         C(u8, i32, f64),
129         D(E, bool),
130     }
131
132     fn match_test() {
133         let ce = Complex::B(8, false);
134         let aa = 0_u8;
135         let bb = false;
136
137         let _: Complex = ce;
138
139         // Don't trigger
140         let _: Complex = match ce {
141             Complex::A(_) => Complex::A(aa),
142             Complex::B(_, b) => Complex::B(aa, b),
143             Complex::C(_, b, _) => Complex::C(aa, b, 64_f64),
144             Complex::D(e, b) => Complex::D(e, b),
145         };
146
147         // Don't trigger
148         let _: Complex = match ce {
149             Complex::A(a) => Complex::A(a),
150             Complex::B(a, _) => Complex::B(a, bb),
151             Complex::C(a, _, _) => Complex::C(a, 32_i32, 64_f64),
152             _ => ce,
153         };
154     }
155 }
156
157 /// Lint triggered when type coercions happen.
158 /// Do NOT trigger on any of these.
159 mod issue8551 {
160     trait Trait {}
161     struct Struct;
162     impl Trait for Struct {}
163
164     fn optmap(s: Option<&Struct>) -> Option<&dyn Trait> {
165         match s {
166             Some(s) => Some(s),
167             None => None,
168         }
169     }
170
171     fn lint_tests() {
172         let option: Option<&Struct> = None;
173         let _: Option<&dyn Trait> = match option {
174             Some(s) => Some(s),
175             None => None,
176         };
177
178         let _: Option<&dyn Trait> = if true {
179             match option {
180                 Some(s) => Some(s),
181                 None => None,
182             }
183         } else {
184             None
185         };
186
187         let result: Result<&Struct, i32> = Err(0);
188         let _: Result<&dyn Trait, i32> = match result {
189             Ok(s) => Ok(s),
190             Err(e) => Err(e),
191         };
192
193         let _: Option<&dyn Trait> = if let Some(s) = option { Some(s) } else { None };
194     }
195 }
196
197 trait Tr {
198     fn as_mut(&mut self) -> Result<&mut i32, &mut i32>;
199 }
200 impl Tr for Result<i32, i32> {
201     fn as_mut(&mut self) -> Result<&mut i32, &mut i32> {
202         match self {
203             Ok(x) => Ok(x),
204             Err(e) => Err(e),
205         }
206     }
207 }
208
209 fn main() {}