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