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