]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/redundant_pattern_matching.fixed
Rollup merge of #73418 - doctorn:variants-intrinsic, r=kennytm
[rust.git] / src / tools / clippy / tests / ui / redundant_pattern_matching.fixed
1 // run-rustfix
2
3 #![feature(const_if_match)]
4 #![feature(const_loop)]
5 #![warn(clippy::all)]
6 #![warn(clippy::redundant_pattern_matching)]
7 #![allow(clippy::unit_arg, unused_must_use, clippy::needless_bool, deprecated)]
8
9 fn main() {
10     if Ok::<i32, i32>(42).is_ok() {}
11
12     if Err::<i32, i32>(42).is_err() {}
13
14     if None::<()>.is_none() {}
15
16     if Some(42).is_some() {}
17
18     if Some(42).is_some() {
19         foo();
20     } else {
21         bar();
22     }
23
24     while Some(42).is_some() {}
25
26     while Some(42).is_none() {}
27
28     while None::<()>.is_none() {}
29
30     while Ok::<i32, i32>(10).is_ok() {}
31
32     while Ok::<i32, i32>(10).is_err() {}
33
34     let mut v = vec![1, 2, 3];
35     while v.pop().is_some() {
36         foo();
37     }
38
39     if Ok::<i32, i32>(42).is_ok() {}
40
41     if Err::<i32, i32>(42).is_err() {}
42
43     if None::<i32>.is_none() {}
44
45     if Some(42).is_some() {}
46
47     if let Ok(x) = Ok::<i32, i32>(42) {
48         println!("{}", x);
49     }
50
51     Ok::<i32, i32>(42).is_ok();
52
53     Ok::<i32, i32>(42).is_err();
54
55     Err::<i32, i32>(42).is_err();
56
57     Err::<i32, i32>(42).is_ok();
58
59     Some(42).is_some();
60
61     None::<()>.is_none();
62
63     let _ = None::<()>.is_none();
64
65     let _ = if Ok::<usize, ()>(4).is_ok() { true } else { false };
66
67     let opt = Some(false);
68     let x = if opt.is_some() { true } else { false };
69     takes_bool(x);
70
71     issue5504();
72     issue5697();
73
74     let _ = if gen_opt().is_some() {
75         1
76     } else if gen_opt().is_none() {
77         2
78     } else if gen_res().is_ok() {
79         3
80     } else if gen_res().is_err() {
81         4
82     } else {
83         5
84     };
85 }
86
87 fn gen_opt() -> Option<()> {
88     None
89 }
90
91 fn gen_res() -> Result<(), ()> {
92     Ok(())
93 }
94
95 fn takes_bool(_: bool) {}
96
97 fn foo() {}
98
99 fn bar() {}
100
101 macro_rules! m {
102     () => {
103         Some(42u32)
104     };
105 }
106
107 fn issue5504() {
108     fn result_opt() -> Result<Option<i32>, i32> {
109         Err(42)
110     }
111
112     fn try_result_opt() -> Result<i32, i32> {
113         while r#try!(result_opt()).is_some() {}
114         if r#try!(result_opt()).is_some() {}
115         Ok(42)
116     }
117
118     try_result_opt();
119
120     if m!().is_some() {}
121     while m!().is_some() {}
122 }
123
124 // None of these should be linted because none of the suggested methods
125 // are `const fn` without toggling a feature.
126 const fn issue5697() {
127     if let Ok(_) = Ok::<i32, i32>(42) {}
128
129     if let Err(_) = Err::<i32, i32>(42) {}
130
131     if let Some(_) = Some(42) {}
132
133     if let None = None::<()> {}
134
135     while let Ok(_) = Ok::<i32, i32>(10) {}
136
137     while let Err(_) = Ok::<i32, i32>(10) {}
138
139     while let Some(_) = Some(42) {}
140
141     while let None = None::<()> {}
142
143     match Ok::<i32, i32>(42) {
144         Ok(_) => true,
145         Err(_) => false,
146     };
147
148     match Err::<i32, i32>(42) {
149         Ok(_) => false,
150         Err(_) => true,
151     };
152     match Some(42) {
153         Some(_) => true,
154         None => false,
155     };
156
157     match None::<()> {
158         Some(_) => false,
159         None => true,
160     };
161 }