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