]> git.lizzy.rs Git - rust.git/blob - tests/ui/infallible_destructuring_match.rs
Merge pull request #2696 from nathan/patch-1
[rust.git] / tests / ui / infallible_destructuring_match.rs
1 #![feature(exhaustive_patterns, never_type)]
2 #![allow(let_and_return)]
3
4 enum SingleVariantEnum {
5     Variant(i32),
6 }
7
8 struct TupleStruct(i32);
9
10 enum EmptyEnum {}
11
12 fn infallible_destructuring_match_enum() {
13     let wrapper = SingleVariantEnum::Variant(0);
14
15     // This should lint!
16     let data = match wrapper {
17         SingleVariantEnum::Variant(i) => i,
18     };
19
20     // This shouldn't!
21     let data = match wrapper {
22         SingleVariantEnum::Variant(_) => -1,
23     };
24
25     // Neither should this!
26     let data = match wrapper {
27         SingleVariantEnum::Variant(i) => -1,
28     };
29
30     let SingleVariantEnum::Variant(data) = wrapper;
31 }
32
33 fn infallible_destructuring_match_struct() {
34     let wrapper = TupleStruct(0);
35
36     // This should lint!
37     let data = match wrapper {
38         TupleStruct(i) => i,
39     };
40
41     // This shouldn't!
42     let data = match wrapper {
43         TupleStruct(_) => -1,
44     };
45
46     // Neither should this!
47     let data = match wrapper {
48         TupleStruct(i) => -1,
49     };
50
51     let TupleStruct(data) = wrapper;
52 }
53
54 fn never_enum() {
55     let wrapper: Result<i32, !> = Ok(23);
56
57     // This should lint!
58     let data = match wrapper {
59         Ok(i) => i,
60     };
61
62     // This shouldn't!
63     let data = match wrapper {
64         Ok(_) => -1,
65     };
66
67     // Neither should this!
68     let data = match wrapper {
69         Ok(i) => -1,
70     };
71
72     let Ok(data) = wrapper;
73 }
74
75 impl EmptyEnum {
76     fn match_on(&self) -> ! {
77         // The lint shouldn't pick this up, as `let` won't work here!
78         let data = match *self {};
79         data
80     }
81 }
82
83 fn main() {}