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