Skip to content

Commit e58d062

Browse files
committed
Update Clippy testcases
Update the test `redundant_pattern_matching`: check if `is_some` and `is_none` are suggested within const contexts.
1 parent 4f859fb commit e58d062

File tree

3 files changed

+91
-65
lines changed

3 files changed

+91
-65
lines changed

src/tools/clippy/tests/ui/redundant_pattern_matching.fixed

+14-25
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ fn main() {
7676
takes_bool(x);
7777

7878
issue5504();
79-
issue5697();
8079
issue6067();
8180

8281
let _ = if gen_opt().is_some() {
@@ -129,41 +128,31 @@ fn issue5504() {
129128
while m!().is_some() {}
130129
}
131130

132-
// None of these should be linted because none of the suggested methods
133-
// are `const fn` without toggling a feature.
134-
const fn issue5697() {
135-
if let Some(_) = Some(42) {}
136-
137-
if let None = None::<()> {}
138-
139-
while let Some(_) = Some(42) {}
140-
141-
while let None = None::<()> {}
142-
143-
match Some(42) {
144-
Some(_) => true,
145-
None => false,
146-
};
147-
148-
match None::<()> {
149-
Some(_) => false,
150-
None => true,
151-
};
152-
}
153-
154131
// Methods that are unstable const should not be suggested within a const context, see issue #5697.
155-
// However, in Rust 1.48.0 the methods `is_ok` and `is_err` of `Result` were stabilized as const,
156-
// so the following should be linted.
132+
// However, in Rust 1.48.0 the methods `is_ok` and `is_err` of `Result`, and `is_some` and `is_none`
133+
// of `Option` were stabilized as const, so the following should be linted.
157134
const fn issue6067() {
158135
if Ok::<i32, i32>(42).is_ok() {}
159136

160137
if Err::<i32, i32>(42).is_err() {}
161138

139+
if Some(42).is_some() {}
140+
141+
if None::<()>.is_none() {}
142+
162143
while Ok::<i32, i32>(10).is_ok() {}
163144

164145
while Ok::<i32, i32>(10).is_err() {}
165146

147+
while Some(42).is_some() {}
148+
149+
while None::<()>.is_none() {}
150+
166151
Ok::<i32, i32>(42).is_ok();
167152

168153
Err::<i32, i32>(42).is_err();
154+
155+
Some(42).is_some();
156+
157+
None::<()>.is_none();
169158
}

src/tools/clippy/tests/ui/redundant_pattern_matching.rs

+20-25
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ fn main() {
9797
takes_bool(x);
9898

9999
issue5504();
100-
issue5697();
101100
issue6067();
102101

103102
let _ = if let Some(_) = gen_opt() {
@@ -150,40 +149,26 @@ fn issue5504() {
150149
while let Some(_) = m!() {}
151150
}
152151

153-
// None of these should be linted because none of the suggested methods
154-
// are `const fn` without toggling a feature.
155-
const fn issue5697() {
156-
if let Some(_) = Some(42) {}
157-
158-
if let None = None::<()> {}
159-
160-
while let Some(_) = Some(42) {}
161-
162-
while let None = None::<()> {}
163-
164-
match Some(42) {
165-
Some(_) => true,
166-
None => false,
167-
};
168-
169-
match None::<()> {
170-
Some(_) => false,
171-
None => true,
172-
};
173-
}
174-
175152
// Methods that are unstable const should not be suggested within a const context, see issue #5697.
176-
// However, in Rust 1.48.0 the methods `is_ok` and `is_err` of `Result` were stabilized as const,
177-
// so the following should be linted.
153+
// However, in Rust 1.48.0 the methods `is_ok` and `is_err` of `Result`, and `is_some` and `is_none`
154+
// of `Option` were stabilized as const, so the following should be linted.
178155
const fn issue6067() {
179156
if let Ok(_) = Ok::<i32, i32>(42) {}
180157

181158
if let Err(_) = Err::<i32, i32>(42) {}
182159

160+
if let Some(_) = Some(42) {}
161+
162+
if let None = None::<()> {}
163+
183164
while let Ok(_) = Ok::<i32, i32>(10) {}
184165

185166
while let Err(_) = Ok::<i32, i32>(10) {}
186167

168+
while let Some(_) = Some(42) {}
169+
170+
while let None = None::<()> {}
171+
187172
match Ok::<i32, i32>(42) {
188173
Ok(_) => true,
189174
Err(_) => false,
@@ -193,4 +178,14 @@ const fn issue6067() {
193178
Ok(_) => false,
194179
Err(_) => true,
195180
};
181+
182+
match Some(42) {
183+
Some(_) => true,
184+
None => false,
185+
};
186+
187+
match None::<()> {
188+
Some(_) => false,
189+
None => true,
190+
};
196191
}

src/tools/clippy/tests/ui/redundant_pattern_matching.stderr

+57-15
Original file line numberDiff line numberDiff line change
@@ -149,79 +149,103 @@ LL | let x = if let Some(_) = opt { true } else { false };
149149
| -------^^^^^^^------ help: try this: `if opt.is_some()`
150150

151151
error: redundant pattern matching, consider using `is_some()`
152-
--> $DIR/redundant_pattern_matching.rs:103:20
152+
--> $DIR/redundant_pattern_matching.rs:102:20
153153
|
154154
LL | let _ = if let Some(_) = gen_opt() {
155155
| -------^^^^^^^------------ help: try this: `if gen_opt().is_some()`
156156

157157
error: redundant pattern matching, consider using `is_none()`
158-
--> $DIR/redundant_pattern_matching.rs:105:19
158+
--> $DIR/redundant_pattern_matching.rs:104:19
159159
|
160160
LL | } else if let None = gen_opt() {
161161
| -------^^^^------------ help: try this: `if gen_opt().is_none()`
162162

163163
error: redundant pattern matching, consider using `is_ok()`
164-
--> $DIR/redundant_pattern_matching.rs:107:19
164+
--> $DIR/redundant_pattern_matching.rs:106:19
165165
|
166166
LL | } else if let Ok(_) = gen_res() {
167167
| -------^^^^^------------ help: try this: `if gen_res().is_ok()`
168168

169169
error: redundant pattern matching, consider using `is_err()`
170-
--> $DIR/redundant_pattern_matching.rs:109:19
170+
--> $DIR/redundant_pattern_matching.rs:108:19
171171
|
172172
LL | } else if let Err(_) = gen_res() {
173173
| -------^^^^^^------------ help: try this: `if gen_res().is_err()`
174174

175175
error: redundant pattern matching, consider using `is_some()`
176-
--> $DIR/redundant_pattern_matching.rs:142:19
176+
--> $DIR/redundant_pattern_matching.rs:141:19
177177
|
178178
LL | while let Some(_) = r#try!(result_opt()) {}
179179
| ----------^^^^^^^----------------------- help: try this: `while r#try!(result_opt()).is_some()`
180180

181181
error: redundant pattern matching, consider using `is_some()`
182-
--> $DIR/redundant_pattern_matching.rs:143:16
182+
--> $DIR/redundant_pattern_matching.rs:142:16
183183
|
184184
LL | if let Some(_) = r#try!(result_opt()) {}
185185
| -------^^^^^^^----------------------- help: try this: `if r#try!(result_opt()).is_some()`
186186

187187
error: redundant pattern matching, consider using `is_some()`
188-
--> $DIR/redundant_pattern_matching.rs:149:12
188+
--> $DIR/redundant_pattern_matching.rs:148:12
189189
|
190190
LL | if let Some(_) = m!() {}
191191
| -------^^^^^^^------- help: try this: `if m!().is_some()`
192192

193193
error: redundant pattern matching, consider using `is_some()`
194-
--> $DIR/redundant_pattern_matching.rs:150:15
194+
--> $DIR/redundant_pattern_matching.rs:149:15
195195
|
196196
LL | while let Some(_) = m!() {}
197197
| ----------^^^^^^^------- help: try this: `while m!().is_some()`
198198

199199
error: redundant pattern matching, consider using `is_ok()`
200-
--> $DIR/redundant_pattern_matching.rs:179:12
200+
--> $DIR/redundant_pattern_matching.rs:156:12
201201
|
202202
LL | if let Ok(_) = Ok::<i32, i32>(42) {}
203203
| -------^^^^^--------------------- help: try this: `if Ok::<i32, i32>(42).is_ok()`
204204

205205
error: redundant pattern matching, consider using `is_err()`
206-
--> $DIR/redundant_pattern_matching.rs:181:12
206+
--> $DIR/redundant_pattern_matching.rs:158:12
207207
|
208208
LL | if let Err(_) = Err::<i32, i32>(42) {}
209209
| -------^^^^^^---------------------- help: try this: `if Err::<i32, i32>(42).is_err()`
210210

211+
error: redundant pattern matching, consider using `is_some()`
212+
--> $DIR/redundant_pattern_matching.rs:160:12
213+
|
214+
LL | if let Some(_) = Some(42) {}
215+
| -------^^^^^^^----------- help: try this: `if Some(42).is_some()`
216+
217+
error: redundant pattern matching, consider using `is_none()`
218+
--> $DIR/redundant_pattern_matching.rs:162:12
219+
|
220+
LL | if let None = None::<()> {}
221+
| -------^^^^------------- help: try this: `if None::<()>.is_none()`
222+
211223
error: redundant pattern matching, consider using `is_ok()`
212-
--> $DIR/redundant_pattern_matching.rs:183:15
224+
--> $DIR/redundant_pattern_matching.rs:164:15
213225
|
214226
LL | while let Ok(_) = Ok::<i32, i32>(10) {}
215227
| ----------^^^^^--------------------- help: try this: `while Ok::<i32, i32>(10).is_ok()`
216228

217229
error: redundant pattern matching, consider using `is_err()`
218-
--> $DIR/redundant_pattern_matching.rs:185:15
230+
--> $DIR/redundant_pattern_matching.rs:166:15
219231
|
220232
LL | while let Err(_) = Ok::<i32, i32>(10) {}
221233
| ----------^^^^^^--------------------- help: try this: `while Ok::<i32, i32>(10).is_err()`
222234

235+
error: redundant pattern matching, consider using `is_some()`
236+
--> $DIR/redundant_pattern_matching.rs:168:15
237+
|
238+
LL | while let Some(_) = Some(42) {}
239+
| ----------^^^^^^^----------- help: try this: `while Some(42).is_some()`
240+
241+
error: redundant pattern matching, consider using `is_none()`
242+
--> $DIR/redundant_pattern_matching.rs:170:15
243+
|
244+
LL | while let None = None::<()> {}
245+
| ----------^^^^------------- help: try this: `while None::<()>.is_none()`
246+
223247
error: redundant pattern matching, consider using `is_ok()`
224-
--> $DIR/redundant_pattern_matching.rs:187:5
248+
--> $DIR/redundant_pattern_matching.rs:172:5
225249
|
226250
LL | / match Ok::<i32, i32>(42) {
227251
LL | | Ok(_) => true,
@@ -230,13 +254,31 @@ LL | | };
230254
| |_____^ help: try this: `Ok::<i32, i32>(42).is_ok()`
231255

232256
error: redundant pattern matching, consider using `is_err()`
233-
--> $DIR/redundant_pattern_matching.rs:192:5
257+
--> $DIR/redundant_pattern_matching.rs:177:5
234258
|
235259
LL | / match Err::<i32, i32>(42) {
236260
LL | | Ok(_) => false,
237261
LL | | Err(_) => true,
238262
LL | | };
239263
| |_____^ help: try this: `Err::<i32, i32>(42).is_err()`
240264

241-
error: aborting due to 35 previous errors
265+
error: redundant pattern matching, consider using `is_some()`
266+
--> $DIR/redundant_pattern_matching.rs:182:5
267+
|
268+
LL | / match Some(42) {
269+
LL | | Some(_) => true,
270+
LL | | None => false,
271+
LL | | };
272+
| |_____^ help: try this: `Some(42).is_some()`
273+
274+
error: redundant pattern matching, consider using `is_none()`
275+
--> $DIR/redundant_pattern_matching.rs:187:5
276+
|
277+
LL | / match None::<()> {
278+
LL | | Some(_) => false,
279+
LL | | None => true,
280+
LL | | };
281+
| |_____^ help: try this: `None::<()>.is_none()`
282+
283+
error: aborting due to 41 previous errors
242284

0 commit comments

Comments
 (0)