fork/rust

git clone https://git.julia.blue/fork/rust

borsAuto merge of #103046 - JanBeh:PR_clarify_cmp_terminology, r=workingjubilee3bcad65fbf6

master
2.4 KiB123 linesraw
1#![feature(exhaustive_patterns, never_type)]
2#![allow(dead_code, unreachable_code, unused_variables)]
3#![allow(clippy::let_and_return)]
4
5enum SingleVariantEnum {
6    Variant(i32),
7}
8
9struct TupleStruct(i32);
10
11struct NonCopy;
12struct TupleStructWithNonCopy(NonCopy);
13
14enum EmptyEnum {}
15
16macro_rules! match_enum {
17    ($param:expr) => {
18        let data = match $param {
19            SingleVariantEnum::Variant(i) => i,
20        };
21    };
22}
23
24fn infallible_destructuring_match_enum() {
25    let wrapper = SingleVariantEnum::Variant(0);
26
27    // This should lint!
28    let SingleVariantEnum::Variant(data) = wrapper;
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
46macro_rules! match_struct {
47    ($param:expr) => {
48        let data = match $param {
49            TupleStruct(i) => i,
50        };
51    };
52}
53
54fn infallible_destructuring_match_struct() {
55    let wrapper = TupleStruct(0);
56
57    // This should lint!
58    let TupleStruct(data) = wrapper;
59
60    // This shouldn't (inside macro)
61    match_struct!(wrapper);
62
63    // This shouldn't!
64    let data = match wrapper {
65        TupleStruct(_) => -1,
66    };
67
68    // Neither should this!
69    let data = match wrapper {
70        TupleStruct(i) => -1,
71    };
72
73    let TupleStruct(data) = wrapper;
74}
75
76fn infallible_destructuring_match_struct_with_noncopy() {
77    let wrapper = TupleStructWithNonCopy(NonCopy);
78
79    // This should lint! (keeping `ref` in the suggestion)
80    let TupleStructWithNonCopy(ref data) = wrapper;
81
82    let TupleStructWithNonCopy(ref data) = wrapper;
83}
84
85macro_rules! match_never_enum {
86    ($param:expr) => {
87        let data = match $param {
88            Ok(i) => i,
89        };
90    };
91}
92
93fn never_enum() {
94    let wrapper: Result<i32, !> = Ok(23);
95
96    // This should lint!
97    let Ok(data) = wrapper;
98
99    // This shouldn't (inside macro)
100    match_never_enum!(wrapper);
101
102    // This shouldn't!
103    let data = match wrapper {
104        Ok(_) => -1,
105    };
106
107    // Neither should this!
108    let data = match wrapper {
109        Ok(i) => -1,
110    };
111
112    let Ok(data) = wrapper;
113}
114
115impl EmptyEnum {
116    fn match_on(&self) -> ! {
117        // The lint shouldn't pick this up, as `let` won't work here!
118        let data = match *self {};
119        data
120    }
121}
122
123fn main() {}