]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/needless_match.rs
Rollup merge of #95000 - fee1-dead:fee1-dead-patch-1, 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 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 = match i {
18         0 => 0,
19         1 => 1,
20         2 => 2,
21         _ => i,
22     };
23     let _: i32 = match i {
24         0 => 0,
25         1 => 1,
26         ref i => *i,
27     };
28     let mut _i_mut = match i {
29         0 => 0,
30         1 => 1,
31         ref mut i => *i,
32     };
33
34     let s = "test";
35     let _: &str = match s {
36         "a" => "a",
37         "b" => "b",
38         s => s,
39     };
40 }
41
42 fn custom_type_match(se: Choice) {
43     let _: Choice = match se {
44         Choice::A => Choice::A,
45         Choice::B => Choice::B,
46         Choice::C => Choice::C,
47         Choice::D => Choice::D,
48     };
49     // Don't trigger
50     let _: Choice = match se {
51         Choice::A => Choice::A,
52         Choice::B => Choice::B,
53         _ => Choice::C,
54     };
55     // Mingled, don't trigger
56     let _: Choice = match se {
57         Choice::A => Choice::B,
58         Choice::B => Choice::C,
59         Choice::C => Choice::D,
60         Choice::D => Choice::A,
61     };
62 }
63
64 fn option_match(x: Option<i32>) {
65     let _: Option<i32> = match x {
66         Some(a) => Some(a),
67         None => None,
68     };
69     // Don't trigger, this is the case for manual_map_option
70     let _: Option<i32> = match x {
71         Some(a) => Some(-a),
72         None => None,
73     };
74 }
75
76 fn func_ret_err<T>(err: T) -> Result<i32, T> {
77     Err(err)
78 }
79
80 fn result_match() {
81     let _: Result<i32, i32> = match Ok(1) {
82         Ok(a) => Ok(a),
83         Err(err) => Err(err),
84     };
85     let _: Result<i32, i32> = match func_ret_err(0_i32) {
86         Err(err) => Err(err),
87         Ok(a) => Ok(a),
88     };
89 }
90
91 fn if_let_option() -> Option<i32> {
92     if let Some(a) = Some(1) { Some(a) } else { None }
93 }
94
95 fn if_let_result(x: Result<(), i32>) {
96     let _: Result<(), i32> = if let Err(e) = x { Err(e) } else { x };
97     let _: Result<(), i32> = if let Ok(val) = x { Ok(val) } else { x };
98     // Input type mismatch, don't trigger
99     let _: Result<(), i32> = if let Err(e) = Ok(1) { Err(e) } else { x };
100 }
101
102 fn if_let_custom_enum(x: Choice) {
103     let _: Choice = if let Choice::A = x {
104         Choice::A
105     } else if let Choice::B = x {
106         Choice::B
107     } else if let Choice::C = x {
108         Choice::C
109     } else {
110         x
111     };
112     // Don't trigger
113     let _: Choice = if let Choice::A = x {
114         Choice::A
115     } else if true {
116         Choice::B
117     } else {
118         x
119     };
120 }
121
122 fn main() {}