]> git.lizzy.rs Git - rust.git/blob - tests/ui/needless_match.fixed
ece18ad737fdad5264a22085a2dcfb94003a83b8
[rust.git] / 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 Choice {
8     A,
9     B,
10     C,
11     D,
12 }
13
14 #[allow(unused_mut)]
15 fn useless_match() {
16     let mut i = 10;
17     let _: i32 = i;
18     let _: i32 = i;
19     let mut _i_mut = i;
20
21     let s = "test";
22     let _: &str = s;
23 }
24
25 fn custom_type_match(se: Choice) {
26     let _: Choice = se;
27     // Don't trigger
28     let _: Choice = match se {
29         Choice::A => Choice::A,
30         Choice::B => Choice::B,
31         _ => Choice::C,
32     };
33     // Mingled, don't trigger
34     let _: Choice = match se {
35         Choice::A => Choice::B,
36         Choice::B => Choice::C,
37         Choice::C => Choice::D,
38         Choice::D => Choice::A,
39     };
40 }
41
42 fn option_match(x: Option<i32>) {
43     let _: Option<i32> = x;
44     // Don't trigger, this is the case for manual_map_option
45     let _: Option<i32> = match x {
46         Some(a) => Some(-a),
47         None => None,
48     };
49 }
50
51 fn func_ret_err<T>(err: T) -> Result<i32, T> {
52     Err(err)
53 }
54
55 fn result_match() {
56     let _: Result<i32, i32> = Ok(1);
57     let _: Result<i32, i32> = func_ret_err(0_i32);
58 }
59
60 fn if_let_option() -> Option<i32> {
61     Some(1)
62 }
63
64 fn if_let_result(x: Result<(), i32>) {
65     let _: Result<(), i32> = x;
66     let _: Result<(), i32> = x;
67     // Input type mismatch, don't trigger
68     let _: Result<(), i32> = if let Err(e) = Ok(1) { Err(e) } else { x };
69 }
70
71 fn if_let_custom_enum(x: Choice) {
72     let _: Choice = x;
73     // Don't trigger
74     let _: Choice = if let Choice::A = x {
75         Choice::A
76     } else if true {
77         Choice::B
78     } else {
79         x
80     };
81 }
82
83 fn main() {}