]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/infallible_destructuring_match.rs
Auto merge of #104673 - matthiaskrgr:rollup-85f65ov, r=matthiaskrgr
[rust.git] / src / tools / clippy / tests / ui / infallible_destructuring_match.rs
1 // run-rustfix
2 #![feature(exhaustive_patterns, never_type)]
3 #![allow(dead_code, unreachable_code, unused_variables)]
4 #![allow(clippy::let_and_return)]
5
6 enum SingleVariantEnum {
7     Variant(i32),
8 }
9
10 struct TupleStruct(i32);
11
12 enum EmptyEnum {}
13
14 macro_rules! match_enum {
15     ($param:expr) => {
16         let data = match $param {
17             SingleVariantEnum::Variant(i) => i,
18         };
19     };
20 }
21
22 fn infallible_destructuring_match_enum() {
23     let wrapper = SingleVariantEnum::Variant(0);
24
25     // This should lint!
26     let data = match wrapper {
27         SingleVariantEnum::Variant(i) => i,
28     };
29
30     // This shouldn't (inside macro)
31     match_enum!(wrapper);
32
33     // This shouldn't!
34     let data = match wrapper {
35         SingleVariantEnum::Variant(_) => -1,
36     };
37
38     // Neither should this!
39     let data = match wrapper {
40         SingleVariantEnum::Variant(i) => -1,
41     };
42
43     let SingleVariantEnum::Variant(data) = wrapper;
44 }
45
46 macro_rules! match_struct {
47     ($param:expr) => {
48         let data = match $param {
49             TupleStruct(i) => i,
50         };
51     };
52 }
53
54 fn infallible_destructuring_match_struct() {
55     let wrapper = TupleStruct(0);
56
57     // This should lint!
58     let data = match wrapper {
59         TupleStruct(i) => i,
60     };
61
62     // This shouldn't (inside macro)
63     match_struct!(wrapper);
64
65     // This shouldn't!
66     let data = match wrapper {
67         TupleStruct(_) => -1,
68     };
69
70     // Neither should this!
71     let data = match wrapper {
72         TupleStruct(i) => -1,
73     };
74
75     let TupleStruct(data) = wrapper;
76 }
77
78 macro_rules! match_never_enum {
79     ($param:expr) => {
80         let data = match $param {
81             Ok(i) => i,
82         };
83     };
84 }
85
86 fn never_enum() {
87     let wrapper: Result<i32, !> = Ok(23);
88
89     // This should lint!
90     let data = match wrapper {
91         Ok(i) => i,
92     };
93
94     // This shouldn't (inside macro)
95     match_never_enum!(wrapper);
96
97     // This shouldn't!
98     let data = match wrapper {
99         Ok(_) => -1,
100     };
101
102     // Neither should this!
103     let data = match wrapper {
104         Ok(i) => -1,
105     };
106
107     let Ok(data) = wrapper;
108 }
109
110 impl EmptyEnum {
111     fn match_on(&self) -> ! {
112         // The lint shouldn't pick this up, as `let` won't work here!
113         let data = match *self {};
114         data
115     }
116 }
117
118 fn main() {}