]> git.lizzy.rs Git - rust.git/blob - src/test/ui/closures/2229_closure_analysis/match-edge-cases.rs
Add additional match test case
[rust.git] / src / test / ui / closures / 2229_closure_analysis / match-edge-cases.rs
1 // run-pass
2 // edition:2021
3
4 const PATTERN_REF: &str = "Hello World";
5 const NUMBER: i32 = 30;
6 const NUMBER_POINTER: *const i32 = &NUMBER;
7
8 pub fn edge_case_ref(event: &str) {
9     let _ = || {
10         match event {
11             PATTERN_REF => (),
12             _ => (),
13         };
14     };
15 }
16
17 pub fn edge_case_str(event: String) {
18     let _ = || {
19         match event.as_str() {
20             "hello" => (),
21             _ => (),
22         };
23     };
24 }
25
26 pub fn edge_case_raw_ptr(event: *const i32) {
27     let _ = || {
28         match event {
29             NUMBER_POINTER => (),
30             _ => (),
31         };
32     };
33 }
34
35 pub fn edge_case_char(event: char) {
36     let _ = || {
37         match event {
38             'a' => (),
39             _ => (),
40         };
41     };
42 }
43
44 enum SingleVariant {
45     A
46 }
47
48 struct TestStruct {
49     x: i32,
50     y: i32,
51     z: i32,
52 }
53
54 fn edge_case_if() {
55     let sv = SingleVariant::A;
56     let condition = true;
57     // sv should not be captured as it is a SingleVariant
58     let _a = || {
59         match sv {
60             SingleVariant::A if condition => (),
61             _ => ()
62         }
63     };
64     let mut mut_sv = sv;
65     _a();
66
67     // ts should be captured
68     let ts = TestStruct { x: 1, y: 1, z: 1 };
69     let _b = || { match ts {
70         TestStruct{ x: 1, .. } => (),
71         _ => ()
72     }};
73     let mut mut_ts = ts;
74     //~^ ERROR: cannot move out of `ts` because it is borrowed
75     _b();
76 }
77
78 fn main() {}