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