]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/tests/ui/infallible_destructuring_match.rs
New upstream version 1.74.1+dfsg1
[rustc.git] / src / tools / clippy / tests / ui / infallible_destructuring_match.rs
1 #![feature(exhaustive_patterns, never_type)]
2 #![allow(dead_code, unreachable_code, unused_variables)]
3 #![allow(clippy::let_and_return)]
4
5 enum SingleVariantEnum {
6 Variant(i32),
7 }
8
9 struct TupleStruct(i32);
10
11 struct NonCopy;
12 struct TupleStructWithNonCopy(NonCopy);
13
14 enum EmptyEnum {}
15
16 macro_rules! match_enum {
17 ($param:expr) => {
18 let data = match $param {
19 SingleVariantEnum::Variant(i) => i,
20 };
21 };
22 }
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 (inside macro)
33 match_enum!(wrapper);
34
35 // This shouldn't!
36 let data = match wrapper {
37 SingleVariantEnum::Variant(_) => -1,
38 };
39
40 // Neither should this!
41 let data = match wrapper {
42 SingleVariantEnum::Variant(i) => -1,
43 };
44
45 let SingleVariantEnum::Variant(data) = wrapper;
46 }
47
48 macro_rules! match_struct {
49 ($param:expr) => {
50 let data = match $param {
51 TupleStruct(i) => i,
52 };
53 };
54 }
55
56 fn infallible_destructuring_match_struct() {
57 let wrapper = TupleStruct(0);
58
59 // This should lint!
60 let data = match wrapper {
61 TupleStruct(i) => i,
62 };
63
64 // This shouldn't (inside macro)
65 match_struct!(wrapper);
66
67 // This shouldn't!
68 let data = match wrapper {
69 TupleStruct(_) => -1,
70 };
71
72 // Neither should this!
73 let data = match wrapper {
74 TupleStruct(i) => -1,
75 };
76
77 let TupleStruct(data) = wrapper;
78 }
79
80 fn infallible_destructuring_match_struct_with_noncopy() {
81 let wrapper = TupleStructWithNonCopy(NonCopy);
82
83 // This should lint! (keeping `ref` in the suggestion)
84 let data = match wrapper {
85 TupleStructWithNonCopy(ref n) => n,
86 };
87
88 let TupleStructWithNonCopy(ref data) = wrapper;
89 }
90
91 macro_rules! match_never_enum {
92 ($param:expr) => {
93 let data = match $param {
94 Ok(i) => i,
95 };
96 };
97 }
98
99 fn never_enum() {
100 let wrapper: Result<i32, !> = Ok(23);
101
102 // This should lint!
103 let data = match wrapper {
104 Ok(i) => i,
105 };
106
107 // This shouldn't (inside macro)
108 match_never_enum!(wrapper);
109
110 // This shouldn't!
111 let data = match wrapper {
112 Ok(_) => -1,
113 };
114
115 // Neither should this!
116 let data = match wrapper {
117 Ok(i) => -1,
118 };
119
120 let Ok(data) = wrapper;
121 }
122
123 impl EmptyEnum {
124 fn match_on(&self) -> ! {
125 // The lint shouldn't pick this up, as `let` won't work here!
126 let data = match *self {};
127 data
128 }
129 }
130
131 fn main() {}