]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/redundant_pattern_matching_option.rs
Rollup merge of #76768 - workingjubilee:reject-oob-shuffles, r=ralfjung
[rust.git] / src / tools / clippy / tests / ui / redundant_pattern_matching_option.rs
1 // run-rustfix
2
3 #![warn(clippy::all)]
4 #![warn(clippy::redundant_pattern_matching)]
5 #![allow(
6     clippy::unit_arg,
7     unused_must_use,
8     clippy::needless_bool,
9     clippy::match_like_matches_macro,
10     deprecated
11 )]
12
13 fn main() {
14     if let None = None::<()> {}
15
16     if let Some(_) = Some(42) {}
17
18     if let Some(_) = Some(42) {
19         foo();
20     } else {
21         bar();
22     }
23
24     while let Some(_) = Some(42) {}
25
26     while let None = Some(42) {}
27
28     while let None = None::<()> {}
29
30     let mut v = vec![1, 2, 3];
31     while let Some(_) = v.pop() {
32         foo();
33     }
34
35     if None::<i32>.is_none() {}
36
37     if Some(42).is_some() {}
38
39     match Some(42) {
40         Some(_) => true,
41         None => false,
42     };
43
44     match None::<()> {
45         Some(_) => false,
46         None => true,
47     };
48
49     let _ = match None::<()> {
50         Some(_) => false,
51         None => true,
52     };
53
54     let opt = Some(false);
55     let x = if let Some(_) = opt { true } else { false };
56     takes_bool(x);
57
58     issue6067();
59
60     let _ = if let Some(_) = gen_opt() {
61         1
62     } else if let None = gen_opt() {
63         2
64     } else {
65         3
66     };
67 }
68
69 fn gen_opt() -> Option<()> {
70     None
71 }
72
73 fn takes_bool(_: bool) {}
74
75 fn foo() {}
76
77 fn bar() {}
78
79 // Methods that are unstable const should not be suggested within a const context, see issue #5697.
80 // However, in Rust 1.48.0 the methods `is_some` and `is_none` of `Option` were stabilized as const,
81 // so the following should be linted.
82 const fn issue6067() {
83     if let Some(_) = Some(42) {}
84
85     if let None = None::<()> {}
86
87     while let Some(_) = Some(42) {}
88
89     while let None = None::<()> {}
90
91     match Some(42) {
92         Some(_) => true,
93         None => false,
94     };
95
96     match None::<()> {
97         Some(_) => false,
98         None => true,
99     };
100 }