]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/manual_let_else_match.rs
Auto merge of #104160 - Ayush1325:windows-args, r=m-ou-se
[rust.git] / src / tools / clippy / tests / ui / manual_let_else_match.rs
1 #![allow(unused_braces, unused_variables, dead_code)]
2 #![allow(clippy::collapsible_else_if, clippy::let_unit_value)]
3 #![warn(clippy::manual_let_else)]
4 // Ensure that we don't conflict with match -> if let lints
5 #![warn(clippy::single_match_else, clippy::single_match)]
6
7 fn f() -> Result<u32, u32> {
8     Ok(0)
9 }
10
11 fn g() -> Option<()> {
12     None
13 }
14
15 fn h() -> (Option<()>, Option<()>) {
16     (None, None)
17 }
18
19 enum Variant {
20     Foo,
21     Bar(u32),
22     Baz(u32),
23 }
24
25 fn build_enum() -> Variant {
26     Variant::Foo
27 }
28
29 fn main() {}
30
31 fn fire() {
32     let v = match g() {
33         Some(v_some) => v_some,
34         None => return,
35     };
36
37     let v = match g() {
38         Some(v_some) => v_some,
39         _ => return,
40     };
41
42     loop {
43         // More complex pattern for the identity arm and diverging arm
44         let v = match h() {
45             (Some(_), Some(_)) | (None, None) => continue,
46             (Some(v), None) | (None, Some(v)) => v,
47         };
48         // Custom enums are supported as long as the "else" arm is a simple _
49         let v = match build_enum() {
50             _ => continue,
51             Variant::Bar(v) | Variant::Baz(v) => v,
52         };
53     }
54
55     // There is a _ in the diverging arm
56     // TODO also support unused bindings aka _v
57     let v = match f() {
58         Ok(v) => v,
59         Err(_) => return,
60     };
61
62     // Err(()) is an allowed pattern
63     let v = match f().map_err(|_| ()) {
64         Ok(v) => v,
65         Err(()) => return,
66     };
67 }
68
69 fn not_fire() {
70     // Multiple diverging arms
71     let v = match h() {
72         _ => panic!(),
73         (None, Some(_v)) => return,
74         (Some(v), None) => v,
75     };
76
77     // Multiple identity arms
78     let v = match h() {
79         _ => panic!(),
80         (None, Some(v)) => v,
81         (Some(v), None) => v,
82     };
83
84     // No diverging arm at all, only identity arms.
85     // This is no case for let else, but destructuring assignment.
86     let v = match f() {
87         Ok(v) => v,
88         Err(e) => e,
89     };
90
91     // The identity arm has a guard
92     let v = match g() {
93         Some(v) if g().is_none() => v,
94         _ => return,
95     };
96
97     // The diverging arm has a guard
98     let v = match f() {
99         Err(v) if v > 0 => panic!(),
100         Ok(v) | Err(v) => v,
101     };
102
103     // The diverging arm creates a binding
104     let v = match f() {
105         Ok(v) => v,
106         Err(e) => panic!("error: {e}"),
107     };
108
109     // Custom enum where the diverging arm
110     // explicitly mentions the variant
111     let v = match build_enum() {
112         Variant::Foo => return,
113         Variant::Bar(v) | Variant::Baz(v) => v,
114     };
115
116     // The custom enum is surrounded by an Err()
117     let v = match Err(build_enum()) {
118         Ok(v) | Err(Variant::Bar(v) | Variant::Baz(v)) => v,
119         Err(Variant::Foo) => return,
120     };
121 }