]> git.lizzy.rs Git - rust.git/blob - tests/ui/infallible_destructuring_match.rs
rustfmt tests
[rust.git] / tests / ui / infallible_destructuring_match.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 #![feature(exhaustive_patterns, never_type)]
11 #![allow(clippy::let_and_return)]
12
13 enum SingleVariantEnum {
14     Variant(i32),
15 }
16
17 struct TupleStruct(i32);
18
19 enum EmptyEnum {}
20
21 fn infallible_destructuring_match_enum() {
22     let wrapper = SingleVariantEnum::Variant(0);
23
24     // This should lint!
25     let data = match wrapper {
26         SingleVariantEnum::Variant(i) => i,
27     };
28
29     // This shouldn't!
30     let data = match wrapper {
31         SingleVariantEnum::Variant(_) => -1,
32     };
33
34     // Neither should this!
35     let data = match wrapper {
36         SingleVariantEnum::Variant(i) => -1,
37     };
38
39     let SingleVariantEnum::Variant(data) = wrapper;
40 }
41
42 fn infallible_destructuring_match_struct() {
43     let wrapper = TupleStruct(0);
44
45     // This should lint!
46     let data = match wrapper {
47         TupleStruct(i) => i,
48     };
49
50     // This shouldn't!
51     let data = match wrapper {
52         TupleStruct(_) => -1,
53     };
54
55     // Neither should this!
56     let data = match wrapper {
57         TupleStruct(i) => -1,
58     };
59
60     let TupleStruct(data) = wrapper;
61 }
62
63 fn never_enum() {
64     let wrapper: Result<i32, !> = Ok(23);
65
66     // This should lint!
67     let data = match wrapper {
68         Ok(i) => i,
69     };
70
71     // This shouldn't!
72     let data = match wrapper {
73         Ok(_) => -1,
74     };
75
76     // Neither should this!
77     let data = match wrapper {
78         Ok(i) => -1,
79     };
80
81     let Ok(data) = wrapper;
82 }
83
84 impl EmptyEnum {
85     fn match_on(&self) -> ! {
86         // The lint shouldn't pick this up, as `let` won't work here!
87         let data = match *self {};
88         data
89     }
90 }
91
92 fn main() {}