Skip to content

Commit fdc2255

Browse files
committed
UI test cleanup: Extract or_fun_call tests
1 parent 2278814 commit fdc2255

File tree

4 files changed

+154
-146
lines changed

4 files changed

+154
-146
lines changed

tests/ui/methods.rs

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -287,69 +287,6 @@ fn search_is_some() {
287287
let _ = foo.rposition().is_some();
288288
}
289289

290-
/// Checks implementation of the `OR_FUN_CALL` lint.
291-
fn or_fun_call() {
292-
struct Foo;
293-
294-
impl Foo {
295-
fn new() -> Foo {
296-
Foo
297-
}
298-
}
299-
300-
enum Enum {
301-
A(i32),
302-
}
303-
304-
fn make<T>() -> T {
305-
unimplemented!();
306-
}
307-
308-
let with_enum = Some(Enum::A(1));
309-
with_enum.unwrap_or(Enum::A(5));
310-
311-
let with_const_fn = Some(::std::time::Duration::from_secs(1));
312-
with_const_fn.unwrap_or(::std::time::Duration::from_secs(5));
313-
314-
let with_constructor = Some(vec![1]);
315-
with_constructor.unwrap_or(make());
316-
317-
let with_new = Some(vec![1]);
318-
with_new.unwrap_or(Vec::new());
319-
320-
let with_const_args = Some(vec![1]);
321-
with_const_args.unwrap_or(Vec::with_capacity(12));
322-
323-
let with_err: Result<_, ()> = Ok(vec![1]);
324-
with_err.unwrap_or(make());
325-
326-
let with_err_args: Result<_, ()> = Ok(vec![1]);
327-
with_err_args.unwrap_or(Vec::with_capacity(12));
328-
329-
let with_default_trait = Some(1);
330-
with_default_trait.unwrap_or(Default::default());
331-
332-
let with_default_type = Some(1);
333-
with_default_type.unwrap_or(u64::default());
334-
335-
let with_vec = Some(vec![1]);
336-
with_vec.unwrap_or(vec![]);
337-
338-
// FIXME #944: ~|SUGGESTION with_vec.unwrap_or_else(|| vec![]);
339-
340-
let without_default = Some(Foo);
341-
without_default.unwrap_or(Foo::new());
342-
343-
let mut map = HashMap::<u64, String>::new();
344-
map.entry(42).or_insert(String::new());
345-
346-
let mut btree = BTreeMap::<u64, String>::new();
347-
btree.entry(42).or_insert(String::new());
348-
349-
let stringy = Some(String::from(""));
350-
let _ = stringy.unwrap_or("".to_owned());
351-
}
352-
353290
/// Checks implementation of `ITER_NTH` lint.
354291
fn iter_nth() {
355292
let mut some_vec = vec![0, 1, 2, 3];

tests/ui/methods.stderr

Lines changed: 9 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -227,131 +227,57 @@ LL | | }
227227
LL | | ).is_some();
228228
| |______________________________^
229229

230-
error: use of `unwrap_or` followed by a function call
231-
--> $DIR/methods.rs:315:22
232-
|
233-
LL | with_constructor.unwrap_or(make());
234-
| ^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(make)`
235-
|
236-
= note: `-D clippy::or-fun-call` implied by `-D warnings`
237-
238-
error: use of `unwrap_or` followed by a call to `new`
239-
--> $DIR/methods.rs:318:5
240-
|
241-
LL | with_new.unwrap_or(Vec::new());
242-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `with_new.unwrap_or_default()`
243-
244-
error: use of `unwrap_or` followed by a function call
245-
--> $DIR/methods.rs:321:21
246-
|
247-
LL | with_const_args.unwrap_or(Vec::with_capacity(12));
248-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| Vec::with_capacity(12))`
249-
250-
error: use of `unwrap_or` followed by a function call
251-
--> $DIR/methods.rs:324:14
252-
|
253-
LL | with_err.unwrap_or(make());
254-
| ^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|_| make())`
255-
256-
error: use of `unwrap_or` followed by a function call
257-
--> $DIR/methods.rs:327:19
258-
|
259-
LL | with_err_args.unwrap_or(Vec::with_capacity(12));
260-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|_| Vec::with_capacity(12))`
261-
262-
error: use of `unwrap_or` followed by a call to `default`
263-
--> $DIR/methods.rs:330:5
264-
|
265-
LL | with_default_trait.unwrap_or(Default::default());
266-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `with_default_trait.unwrap_or_default()`
267-
268-
error: use of `unwrap_or` followed by a call to `default`
269-
--> $DIR/methods.rs:333:5
270-
|
271-
LL | with_default_type.unwrap_or(u64::default());
272-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `with_default_type.unwrap_or_default()`
273-
274-
error: use of `unwrap_or` followed by a function call
275-
--> $DIR/methods.rs:336:14
276-
|
277-
LL | with_vec.unwrap_or(vec![]);
278-
| ^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| vec![])`
279-
280-
error: use of `unwrap_or` followed by a function call
281-
--> $DIR/methods.rs:341:21
282-
|
283-
LL | without_default.unwrap_or(Foo::new());
284-
| ^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(Foo::new)`
285-
286-
error: use of `or_insert` followed by a function call
287-
--> $DIR/methods.rs:344:19
288-
|
289-
LL | map.entry(42).or_insert(String::new());
290-
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `or_insert_with(String::new)`
291-
292-
error: use of `or_insert` followed by a function call
293-
--> $DIR/methods.rs:347:21
294-
|
295-
LL | btree.entry(42).or_insert(String::new());
296-
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `or_insert_with(String::new)`
297-
298-
error: use of `unwrap_or` followed by a function call
299-
--> $DIR/methods.rs:350:21
300-
|
301-
LL | let _ = stringy.unwrap_or("".to_owned());
302-
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| "".to_owned())`
303-
304230
error: called `.iter().nth()` on a Vec. Calling `.get()` is both faster and more readable
305-
--> $DIR/methods.rs:361:23
231+
--> $DIR/methods.rs:298:23
306232
|
307233
LL | let bad_vec = some_vec.iter().nth(3);
308234
| ^^^^^^^^^^^^^^^^^^^^^^
309235
|
310236
= note: `-D clippy::iter-nth` implied by `-D warnings`
311237

312238
error: called `.iter().nth()` on a slice. Calling `.get()` is both faster and more readable
313-
--> $DIR/methods.rs:362:26
239+
--> $DIR/methods.rs:299:26
314240
|
315241
LL | let bad_slice = &some_vec[..].iter().nth(3);
316242
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
317243

318244
error: called `.iter().nth()` on a slice. Calling `.get()` is both faster and more readable
319-
--> $DIR/methods.rs:363:31
245+
--> $DIR/methods.rs:300:31
320246
|
321247
LL | let bad_boxed_slice = boxed_slice.iter().nth(3);
322248
| ^^^^^^^^^^^^^^^^^^^^^^^^^
323249

324250
error: called `.iter().nth()` on a VecDeque. Calling `.get()` is both faster and more readable
325-
--> $DIR/methods.rs:364:29
251+
--> $DIR/methods.rs:301:29
326252
|
327253
LL | let bad_vec_deque = some_vec_deque.iter().nth(3);
328254
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
329255

330256
error: called `.iter_mut().nth()` on a Vec. Calling `.get_mut()` is both faster and more readable
331-
--> $DIR/methods.rs:369:23
257+
--> $DIR/methods.rs:306:23
332258
|
333259
LL | let bad_vec = some_vec.iter_mut().nth(3);
334260
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
335261

336262
error: called `.iter_mut().nth()` on a slice. Calling `.get_mut()` is both faster and more readable
337-
--> $DIR/methods.rs:372:26
263+
--> $DIR/methods.rs:309:26
338264
|
339265
LL | let bad_slice = &some_vec[..].iter_mut().nth(3);
340266
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
341267

342268
error: called `.iter_mut().nth()` on a VecDeque. Calling `.get_mut()` is both faster and more readable
343-
--> $DIR/methods.rs:375:29
269+
--> $DIR/methods.rs:312:29
344270
|
345271
LL | let bad_vec_deque = some_vec_deque.iter_mut().nth(3);
346272
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
347273

348274
error: used unwrap() on an Option value. If you don't want to handle the None case gracefully, consider using expect() to provide a better panic message
349-
--> $DIR/methods.rs:387:13
275+
--> $DIR/methods.rs:324:13
350276
|
351277
LL | let _ = opt.unwrap();
352278
| ^^^^^^^^^^^^
353279
|
354280
= note: `-D clippy::option-unwrap-used` implied by `-D warnings`
355281

356-
error: aborting due to 44 previous errors
282+
error: aborting due to 32 previous errors
357283

tests/ui/or_fun_call.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#![warn(clippy::or_fun_call)]
2+
3+
use std::collections::BTreeMap;
4+
use std::collections::HashMap;
5+
6+
/// Checks implementation of the `OR_FUN_CALL` lint.
7+
fn or_fun_call() {
8+
struct Foo;
9+
10+
impl Foo {
11+
fn new() -> Foo {
12+
Foo
13+
}
14+
}
15+
16+
enum Enum {
17+
A(i32),
18+
}
19+
20+
fn make<T>() -> T {
21+
unimplemented!();
22+
}
23+
24+
let with_enum = Some(Enum::A(1));
25+
with_enum.unwrap_or(Enum::A(5));
26+
27+
let with_const_fn = Some(::std::time::Duration::from_secs(1));
28+
with_const_fn.unwrap_or(::std::time::Duration::from_secs(5));
29+
30+
let with_constructor = Some(vec![1]);
31+
with_constructor.unwrap_or(make());
32+
33+
let with_new = Some(vec![1]);
34+
with_new.unwrap_or(Vec::new());
35+
36+
let with_const_args = Some(vec![1]);
37+
with_const_args.unwrap_or(Vec::with_capacity(12));
38+
39+
let with_err: Result<_, ()> = Ok(vec![1]);
40+
with_err.unwrap_or(make());
41+
42+
let with_err_args: Result<_, ()> = Ok(vec![1]);
43+
with_err_args.unwrap_or(Vec::with_capacity(12));
44+
45+
let with_default_trait = Some(1);
46+
with_default_trait.unwrap_or(Default::default());
47+
48+
let with_default_type = Some(1);
49+
with_default_type.unwrap_or(u64::default());
50+
51+
let with_vec = Some(vec![1]);
52+
with_vec.unwrap_or(vec![]);
53+
54+
// FIXME #944: ~|SUGGESTION with_vec.unwrap_or_else(|| vec![]);
55+
56+
let without_default = Some(Foo);
57+
without_default.unwrap_or(Foo::new());
58+
59+
let mut map = HashMap::<u64, String>::new();
60+
map.entry(42).or_insert(String::new());
61+
62+
let mut btree = BTreeMap::<u64, String>::new();
63+
btree.entry(42).or_insert(String::new());
64+
65+
let stringy = Some(String::from(""));
66+
let _ = stringy.unwrap_or("".to_owned());
67+
}
68+
69+
fn main() {}

tests/ui/or_fun_call.stderr

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
error: use of `unwrap_or` followed by a function call
2+
--> $DIR/or_fun_call.rs:31:22
3+
|
4+
LL | with_constructor.unwrap_or(make());
5+
| ^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(make)`
6+
|
7+
= note: `-D clippy::or-fun-call` implied by `-D warnings`
8+
9+
error: use of `unwrap_or` followed by a call to `new`
10+
--> $DIR/or_fun_call.rs:34:5
11+
|
12+
LL | with_new.unwrap_or(Vec::new());
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `with_new.unwrap_or_default()`
14+
15+
error: use of `unwrap_or` followed by a function call
16+
--> $DIR/or_fun_call.rs:37:21
17+
|
18+
LL | with_const_args.unwrap_or(Vec::with_capacity(12));
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| Vec::with_capacity(12))`
20+
21+
error: use of `unwrap_or` followed by a function call
22+
--> $DIR/or_fun_call.rs:40:14
23+
|
24+
LL | with_err.unwrap_or(make());
25+
| ^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|_| make())`
26+
27+
error: use of `unwrap_or` followed by a function call
28+
--> $DIR/or_fun_call.rs:43:19
29+
|
30+
LL | with_err_args.unwrap_or(Vec::with_capacity(12));
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|_| Vec::with_capacity(12))`
32+
33+
error: use of `unwrap_or` followed by a call to `default`
34+
--> $DIR/or_fun_call.rs:46:5
35+
|
36+
LL | with_default_trait.unwrap_or(Default::default());
37+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `with_default_trait.unwrap_or_default()`
38+
39+
error: use of `unwrap_or` followed by a call to `default`
40+
--> $DIR/or_fun_call.rs:49:5
41+
|
42+
LL | with_default_type.unwrap_or(u64::default());
43+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `with_default_type.unwrap_or_default()`
44+
45+
error: use of `unwrap_or` followed by a function call
46+
--> $DIR/or_fun_call.rs:52:14
47+
|
48+
LL | with_vec.unwrap_or(vec![]);
49+
| ^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| vec![])`
50+
51+
error: use of `unwrap_or` followed by a function call
52+
--> $DIR/or_fun_call.rs:57:21
53+
|
54+
LL | without_default.unwrap_or(Foo::new());
55+
| ^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(Foo::new)`
56+
57+
error: use of `or_insert` followed by a function call
58+
--> $DIR/or_fun_call.rs:60:19
59+
|
60+
LL | map.entry(42).or_insert(String::new());
61+
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `or_insert_with(String::new)`
62+
63+
error: use of `or_insert` followed by a function call
64+
--> $DIR/or_fun_call.rs:63:21
65+
|
66+
LL | btree.entry(42).or_insert(String::new());
67+
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `or_insert_with(String::new)`
68+
69+
error: use of `unwrap_or` followed by a function call
70+
--> $DIR/or_fun_call.rs:66:21
71+
|
72+
LL | let _ = stringy.unwrap_or("".to_owned());
73+
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| "".to_owned())`
74+
75+
error: aborting due to 12 previous errors
76+

0 commit comments

Comments
 (0)