Skip to content

Commit b1a345e

Browse files
committed
map_unit_fn: make test rustfixable
1 parent 95756b2 commit b1a345e

6 files changed

+197
-34
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// run-rustfix
2+
3+
#![warn(clippy::option_map_unit_fn)]
4+
#![allow(unused)]
5+
6+
fn do_nothing<T>(_: T) {}
7+
8+
fn diverge<T>(_: T) -> ! {
9+
panic!()
10+
}
11+
12+
fn plus_one(value: usize) -> usize {
13+
value + 1
14+
}
15+
16+
struct HasOption {
17+
field: Option<usize>,
18+
}
19+
20+
impl HasOption {
21+
fn do_option_nothing(self: &Self, value: usize) {}
22+
23+
fn do_option_plus_one(self: &Self, value: usize) -> usize {
24+
value + 1
25+
}
26+
}
27+
#[rustfmt::skip]
28+
fn option_map_unit_fn() {
29+
let x = HasOption { field: Some(10) };
30+
31+
x.field.map(plus_one);
32+
let _ : Option<()> = x.field.map(do_nothing);
33+
34+
if let Some(x_field) = x.field { do_nothing(x_field) }
35+
36+
if let Some(x_field) = x.field { do_nothing(x_field) }
37+
38+
if let Some(x_field) = x.field { diverge(x_field) }
39+
40+
let captured = 10;
41+
if let Some(value) = x.field { do_nothing(value + captured) };
42+
let _ : Option<()> = x.field.map(|value| do_nothing(value + captured));
43+
44+
if let Some(value) = x.field { x.do_option_nothing(value + captured) }
45+
46+
if let Some(value) = x.field { x.do_option_plus_one(value + captured); }
47+
48+
49+
if let Some(value) = x.field { do_nothing(value + captured) }
50+
51+
if let Some(value) = x.field { do_nothing(value + captured) }
52+
53+
if let Some(value) = x.field { do_nothing(value + captured); }
54+
55+
if let Some(value) = x.field { do_nothing(value + captured); }
56+
57+
58+
if let Some(value) = x.field { diverge(value + captured) }
59+
60+
if let Some(value) = x.field { diverge(value + captured) }
61+
62+
if let Some(value) = x.field { diverge(value + captured); }
63+
64+
if let Some(value) = x.field { diverge(value + captured); }
65+
66+
67+
x.field.map(|value| plus_one(value + captured));
68+
x.field.map(|value| { plus_one(value + captured) });
69+
if let Some(value) = x.field { let y = plus_one(value + captured); }
70+
71+
if let Some(value) = x.field { plus_one(value + captured); }
72+
73+
if let Some(value) = x.field { plus_one(value + captured); }
74+
75+
76+
if let Some(ref value) = x.field { do_nothing(value + captured) }}
77+
78+
fn main() {}

tests/ui/option_map_unit_fn_fixable.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// run-rustfix
2+
13
#![warn(clippy::option_map_unit_fn)]
24
#![allow(unused)]
35

tests/ui/option_map_unit_fn_fixable.stderr

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: called `map(f)` on an Option value where `f` is a unit function
2-
--> $DIR/option_map_unit_fn_fixable.rs:32:5
2+
--> $DIR/option_map_unit_fn_fixable.rs:34:5
33
|
44
LL | x.field.map(do_nothing);
55
| ^^^^^^^^^^^^^^^^^^^^^^^-
@@ -9,127 +9,127 @@ LL | x.field.map(do_nothing);
99
= note: `-D clippy::option-map-unit-fn` implied by `-D warnings`
1010

1111
error: called `map(f)` on an Option value where `f` is a unit function
12-
--> $DIR/option_map_unit_fn_fixable.rs:34:5
12+
--> $DIR/option_map_unit_fn_fixable.rs:36:5
1313
|
1414
LL | x.field.map(do_nothing);
1515
| ^^^^^^^^^^^^^^^^^^^^^^^-
1616
| |
1717
| help: try this: `if let Some(x_field) = x.field { do_nothing(x_field) }`
1818

1919
error: called `map(f)` on an Option value where `f` is a unit function
20-
--> $DIR/option_map_unit_fn_fixable.rs:36:5
20+
--> $DIR/option_map_unit_fn_fixable.rs:38:5
2121
|
2222
LL | x.field.map(diverge);
2323
| ^^^^^^^^^^^^^^^^^^^^-
2424
| |
2525
| help: try this: `if let Some(x_field) = x.field { diverge(x_field) }`
2626

2727
error: called `map(f)` on an Option value where `f` is a unit closure
28-
--> $DIR/option_map_unit_fn_fixable.rs:42:5
28+
--> $DIR/option_map_unit_fn_fixable.rs:44:5
2929
|
3030
LL | x.field.map(|value| x.do_option_nothing(value + captured));
3131
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
3232
| |
3333
| help: try this: `if let Some(value) = x.field { x.do_option_nothing(value + captured) }`
3434

3535
error: called `map(f)` on an Option value where `f` is a unit closure
36-
--> $DIR/option_map_unit_fn_fixable.rs:44:5
36+
--> $DIR/option_map_unit_fn_fixable.rs:46:5
3737
|
3838
LL | x.field.map(|value| { x.do_option_plus_one(value + captured); });
3939
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
4040
| |
4141
| help: try this: `if let Some(value) = x.field { x.do_option_plus_one(value + captured); }`
4242

4343
error: called `map(f)` on an Option value where `f` is a unit closure
44-
--> $DIR/option_map_unit_fn_fixable.rs:47:5
44+
--> $DIR/option_map_unit_fn_fixable.rs:49:5
4545
|
4646
LL | x.field.map(|value| do_nothing(value + captured));
4747
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
4848
| |
4949
| help: try this: `if let Some(value) = x.field { do_nothing(value + captured) }`
5050

5151
error: called `map(f)` on an Option value where `f` is a unit closure
52-
--> $DIR/option_map_unit_fn_fixable.rs:49:5
52+
--> $DIR/option_map_unit_fn_fixable.rs:51:5
5353
|
5454
LL | x.field.map(|value| { do_nothing(value + captured) });
5555
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
5656
| |
5757
| help: try this: `if let Some(value) = x.field { do_nothing(value + captured) }`
5858

5959
error: called `map(f)` on an Option value where `f` is a unit closure
60-
--> $DIR/option_map_unit_fn_fixable.rs:51:5
60+
--> $DIR/option_map_unit_fn_fixable.rs:53:5
6161
|
6262
LL | x.field.map(|value| { do_nothing(value + captured); });
6363
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
6464
| |
6565
| help: try this: `if let Some(value) = x.field { do_nothing(value + captured); }`
6666

6767
error: called `map(f)` on an Option value where `f` is a unit closure
68-
--> $DIR/option_map_unit_fn_fixable.rs:53:5
68+
--> $DIR/option_map_unit_fn_fixable.rs:55:5
6969
|
7070
LL | x.field.map(|value| { { do_nothing(value + captured); } });
7171
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
7272
| |
7373
| help: try this: `if let Some(value) = x.field { do_nothing(value + captured); }`
7474

7575
error: called `map(f)` on an Option value where `f` is a unit closure
76-
--> $DIR/option_map_unit_fn_fixable.rs:56:5
76+
--> $DIR/option_map_unit_fn_fixable.rs:58:5
7777
|
7878
LL | x.field.map(|value| diverge(value + captured));
7979
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
8080
| |
8181
| help: try this: `if let Some(value) = x.field { diverge(value + captured) }`
8282

8383
error: called `map(f)` on an Option value where `f` is a unit closure
84-
--> $DIR/option_map_unit_fn_fixable.rs:58:5
84+
--> $DIR/option_map_unit_fn_fixable.rs:60:5
8585
|
8686
LL | x.field.map(|value| { diverge(value + captured) });
8787
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
8888
| |
8989
| help: try this: `if let Some(value) = x.field { diverge(value + captured) }`
9090

9191
error: called `map(f)` on an Option value where `f` is a unit closure
92-
--> $DIR/option_map_unit_fn_fixable.rs:60:5
92+
--> $DIR/option_map_unit_fn_fixable.rs:62:5
9393
|
9494
LL | x.field.map(|value| { diverge(value + captured); });
9595
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
9696
| |
9797
| help: try this: `if let Some(value) = x.field { diverge(value + captured); }`
9898

9999
error: called `map(f)` on an Option value where `f` is a unit closure
100-
--> $DIR/option_map_unit_fn_fixable.rs:62:5
100+
--> $DIR/option_map_unit_fn_fixable.rs:64:5
101101
|
102102
LL | x.field.map(|value| { { diverge(value + captured); } });
103103
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
104104
| |
105105
| help: try this: `if let Some(value) = x.field { diverge(value + captured); }`
106106

107107
error: called `map(f)` on an Option value where `f` is a unit closure
108-
--> $DIR/option_map_unit_fn_fixable.rs:67:5
108+
--> $DIR/option_map_unit_fn_fixable.rs:69:5
109109
|
110110
LL | x.field.map(|value| { let y = plus_one(value + captured); });
111111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
112112
| |
113113
| help: try this: `if let Some(value) = x.field { let y = plus_one(value + captured); }`
114114

115115
error: called `map(f)` on an Option value where `f` is a unit closure
116-
--> $DIR/option_map_unit_fn_fixable.rs:69:5
116+
--> $DIR/option_map_unit_fn_fixable.rs:71:5
117117
|
118118
LL | x.field.map(|value| { plus_one(value + captured); });
119119
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
120120
| |
121121
| help: try this: `if let Some(value) = x.field { plus_one(value + captured); }`
122122

123123
error: called `map(f)` on an Option value where `f` is a unit closure
124-
--> $DIR/option_map_unit_fn_fixable.rs:71:5
124+
--> $DIR/option_map_unit_fn_fixable.rs:73:5
125125
|
126126
LL | x.field.map(|value| { { plus_one(value + captured); } });
127127
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
128128
| |
129129
| help: try this: `if let Some(value) = x.field { plus_one(value + captured); }`
130130

131131
error: called `map(f)` on an Option value where `f` is a unit closure
132-
--> $DIR/option_map_unit_fn_fixable.rs:74:5
132+
--> $DIR/option_map_unit_fn_fixable.rs:76:5
133133
|
134134
LL | x.field.map(|ref value| { do_nothing(value + captured) });}
135135
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// run-rustfix
2+
3+
#![feature(never_type)]
4+
#![warn(clippy::result_map_unit_fn)]
5+
#![allow(unused)]
6+
7+
fn do_nothing<T>(_: T) {}
8+
9+
fn diverge<T>(_: T) -> ! {
10+
panic!()
11+
}
12+
13+
fn plus_one(value: usize) -> usize {
14+
value + 1
15+
}
16+
17+
struct HasResult {
18+
field: Result<usize, usize>,
19+
}
20+
21+
impl HasResult {
22+
fn do_result_nothing(self: &Self, value: usize) {}
23+
24+
fn do_result_plus_one(self: &Self, value: usize) -> usize {
25+
value + 1
26+
}
27+
}
28+
29+
#[rustfmt::skip]
30+
fn result_map_unit_fn() {
31+
let x = HasResult { field: Ok(10) };
32+
33+
x.field.map(plus_one);
34+
let _: Result<(), usize> = x.field.map(do_nothing);
35+
36+
if let Ok(x_field) = x.field { do_nothing(x_field) }
37+
38+
if let Ok(x_field) = x.field { do_nothing(x_field) }
39+
40+
if let Ok(x_field) = x.field { diverge(x_field) }
41+
42+
let captured = 10;
43+
if let Ok(value) = x.field { do_nothing(value + captured) };
44+
let _: Result<(), usize> = x.field.map(|value| do_nothing(value + captured));
45+
46+
if let Ok(value) = x.field { x.do_result_nothing(value + captured) }
47+
48+
if let Ok(value) = x.field { x.do_result_plus_one(value + captured); }
49+
50+
51+
if let Ok(value) = x.field { do_nothing(value + captured) }
52+
53+
if let Ok(value) = x.field { do_nothing(value + captured) }
54+
55+
if let Ok(value) = x.field { do_nothing(value + captured); }
56+
57+
if let Ok(value) = x.field { do_nothing(value + captured); }
58+
59+
60+
if let Ok(value) = x.field { diverge(value + captured) }
61+
62+
if let Ok(value) = x.field { diverge(value + captured) }
63+
64+
if let Ok(value) = x.field { diverge(value + captured); }
65+
66+
if let Ok(value) = x.field { diverge(value + captured); }
67+
68+
69+
x.field.map(|value| plus_one(value + captured));
70+
x.field.map(|value| { plus_one(value + captured) });
71+
if let Ok(value) = x.field { let y = plus_one(value + captured); }
72+
73+
if let Ok(value) = x.field { plus_one(value + captured); }
74+
75+
if let Ok(value) = x.field { plus_one(value + captured); }
76+
77+
78+
if let Ok(ref value) = x.field { do_nothing(value + captured) }
79+
}
80+
81+
fn main() {}

tests/ui/result_map_unit_fn_fixable.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// run-rustfix
2+
13
#![feature(never_type)]
24
#![warn(clippy::result_map_unit_fn)]
35
#![allow(unused)]

0 commit comments

Comments
 (0)