Skip to content

Commit 1f03567

Browse files
committed
option_map_unit_fn: Split into fixable/unfixable
1 parent 4f3474e commit 1f03567

8 files changed

+137
-174
lines changed

tests/ui/option_map_unit_fn_fixable.rs

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -71,29 +71,6 @@ fn option_map_unit_fn() {
7171
x.field.map(|value| { { plus_one(value + captured); } });
7272

7373

74-
x.field.map(|ref value| { do_nothing(value + captured) });
75-
76-
77-
x.field.map(|value| { do_nothing(value); do_nothing(value) });
78-
79-
x.field.map(|value| if value > 0 { do_nothing(value); do_nothing(value) });
80-
81-
// Suggestion for the let block should be `{ ... }` as it's too difficult to build a
82-
// proper suggestion for these cases
83-
x.field.map(|value| {
84-
do_nothing(value);
85-
do_nothing(value)
86-
});
87-
x.field.map(|value| { do_nothing(value); do_nothing(value); });
88-
89-
// The following should suggest `if let Some(_X) ...` as it's difficult to generate a proper let variable name for them
90-
Some(42).map(diverge);
91-
"12".parse::<i32>().ok().map(diverge);
92-
Some(plus_one(1)).map(do_nothing);
93-
94-
// Should suggest `if let Some(_y) ...` to not override the existing foo variable
95-
let y = Some(42);
96-
y.map(do_nothing);
97-
}
74+
x.field.map(|ref value| { do_nothing(value + captured) });}
9875

9976
fn main() {}

tests/ui/option_map_unit_fn_fixable.stderr

Lines changed: 2 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -131,80 +131,10 @@ LL | x.field.map(|value| { { plus_one(value + captured); } });
131131
error: called `map(f)` on an Option value where `f` is a unit closure
132132
--> $DIR/option_map_unit_fn_fixable.rs:74:5
133133
|
134-
LL | x.field.map(|ref value| { do_nothing(value + captured) });
134+
LL | x.field.map(|ref value| { do_nothing(value + captured) });}
135135
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
136136
| |
137137
| help: try this: `if let Some(ref value) = x.field { do_nothing(value + captured) }`
138138

139-
error: called `map(f)` on an Option value where `f` is a unit closure
140-
--> $DIR/option_map_unit_fn_fixable.rs:77:5
141-
|
142-
LL | x.field.map(|value| { do_nothing(value); do_nothing(value) });
143-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
144-
| |
145-
| help: try this: `if let Some(value) = x.field { ... }`
146-
147-
error: called `map(f)` on an Option value where `f` is a unit closure
148-
--> $DIR/option_map_unit_fn_fixable.rs:79:5
149-
|
150-
LL | x.field.map(|value| if value > 0 { do_nothing(value); do_nothing(value) });
151-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
152-
| |
153-
| help: try this: `if let Some(value) = x.field { ... }`
154-
155-
error: called `map(f)` on an Option value where `f` is a unit closure
156-
--> $DIR/option_map_unit_fn_fixable.rs:83:5
157-
|
158-
LL | x.field.map(|value| {
159-
| _____^
160-
| |_____|
161-
| ||
162-
LL | || do_nothing(value);
163-
LL | || do_nothing(value)
164-
LL | || });
165-
| ||______^- help: try this: `if let Some(value) = x.field { ... }`
166-
| |_______|
167-
|
168-
169-
error: called `map(f)` on an Option value where `f` is a unit closure
170-
--> $DIR/option_map_unit_fn_fixable.rs:87:5
171-
|
172-
LL | x.field.map(|value| { do_nothing(value); do_nothing(value); });
173-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
174-
| |
175-
| help: try this: `if let Some(value) = x.field { ... }`
176-
177-
error: called `map(f)` on an Option value where `f` is a unit function
178-
--> $DIR/option_map_unit_fn_fixable.rs:90:5
179-
|
180-
LL | Some(42).map(diverge);
181-
| ^^^^^^^^^^^^^^^^^^^^^-
182-
| |
183-
| help: try this: `if let Some(_) = Some(42) { diverge(...) }`
184-
185-
error: called `map(f)` on an Option value where `f` is a unit function
186-
--> $DIR/option_map_unit_fn_fixable.rs:91:5
187-
|
188-
LL | "12".parse::<i32>().ok().map(diverge);
189-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
190-
| |
191-
| help: try this: `if let Some(_) = "12".parse::<i32>().ok() { diverge(...) }`
192-
193-
error: called `map(f)` on an Option value where `f` is a unit function
194-
--> $DIR/option_map_unit_fn_fixable.rs:92:5
195-
|
196-
LL | Some(plus_one(1)).map(do_nothing);
197-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
198-
| |
199-
| help: try this: `if let Some(_) = Some(plus_one(1)) { do_nothing(...) }`
200-
201-
error: called `map(f)` on an Option value where `f` is a unit function
202-
--> $DIR/option_map_unit_fn_fixable.rs:96:5
203-
|
204-
LL | y.map(do_nothing);
205-
| ^^^^^^^^^^^^^^^^^-
206-
| |
207-
| help: try this: `if let Some(_y) = y { do_nothing(...) }`
208-
209-
error: aborting due to 25 previous errors
139+
error: aborting due to 17 previous errors
210140

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#![warn(clippy::option_map_unit_fn)]
2+
#![allow(unused)]
3+
4+
fn do_nothing<T>(_: T) {}
5+
6+
fn diverge<T>(_: T) -> ! {
7+
panic!()
8+
}
9+
10+
fn plus_one(value: usize) -> usize {
11+
value + 1
12+
}
13+
14+
#[rustfmt::skip]
15+
fn option_map_unit_fn() {
16+
17+
x.field.map(|value| { do_nothing(value); do_nothing(value) });
18+
19+
x.field.map(|value| if value > 0 { do_nothing(value); do_nothing(value) });
20+
21+
// Suggestion for the let block should be `{ ... }` as it's too difficult to build a
22+
// proper suggestion for these cases
23+
x.field.map(|value| {
24+
do_nothing(value);
25+
do_nothing(value)
26+
});
27+
x.field.map(|value| { do_nothing(value); do_nothing(value); });
28+
29+
// The following should suggest `if let Some(_X) ...` as it's difficult to generate a proper let variable name for them
30+
Some(42).map(diverge);
31+
"12".parse::<i32>().ok().map(diverge);
32+
Some(plus_one(1)).map(do_nothing);
33+
34+
// Should suggest `if let Some(_y) ...` to not override the existing foo variable
35+
let y = Some(42);
36+
y.map(do_nothing);
37+
}
38+
39+
fn main() {}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error[E0425]: cannot find value `x` in this scope
2+
--> $DIR/option_map_unit_fn_unfixable.rs:17:5
3+
|
4+
LL | x.field.map(|value| { do_nothing(value); do_nothing(value) });
5+
| ^ not found in this scope
6+
7+
error[E0425]: cannot find value `x` in this scope
8+
--> $DIR/option_map_unit_fn_unfixable.rs:19:5
9+
|
10+
LL | x.field.map(|value| if value > 0 { do_nothing(value); do_nothing(value) });
11+
| ^ not found in this scope
12+
13+
error[E0425]: cannot find value `x` in this scope
14+
--> $DIR/option_map_unit_fn_unfixable.rs:23:5
15+
|
16+
LL | x.field.map(|value| {
17+
| ^ not found in this scope
18+
19+
error[E0425]: cannot find value `x` in this scope
20+
--> $DIR/option_map_unit_fn_unfixable.rs:27:5
21+
|
22+
LL | x.field.map(|value| { do_nothing(value); do_nothing(value); });
23+
| ^ not found in this scope
24+
25+
error: aborting due to 4 previous errors
26+
27+
For more information about this error, try `rustc --explain E0425`.

tests/ui/result_map_unit_fn_fixable.rs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -74,29 +74,6 @@ fn result_map_unit_fn() {
7474

7575

7676
x.field.map(|ref value| { do_nothing(value + captured) });
77-
78-
79-
x.field.map(|value| { do_nothing(value); do_nothing(value) });
80-
81-
x.field.map(|value| if value > 0 { do_nothing(value); do_nothing(value) });
82-
83-
// Suggestion for the let block should be `{ ... }` as it's too difficult to build a
84-
// proper suggestion for these cases
85-
x.field.map(|value| {
86-
do_nothing(value);
87-
do_nothing(value)
88-
});
89-
x.field.map(|value| { do_nothing(value); do_nothing(value); });
90-
91-
// The following should suggest `if let Ok(_X) ...` as it's difficult to generate a proper let variable name for them
92-
let res: Result<!, usize> = Ok(42).map(diverge);
93-
"12".parse::<i32>().map(diverge);
94-
95-
let res: Result<(), usize> = Ok(plus_one(1)).map(do_nothing);
96-
97-
// Should suggest `if let Ok(_y) ...` to not override the existing foo variable
98-
let y: Result<usize, usize> = Ok(42);
99-
y.map(do_nothing);
10077
}
10178

10279
fn main() {}

tests/ui/result_map_unit_fn_fixable.stderr

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -136,59 +136,5 @@ LL | x.field.map(|ref value| { do_nothing(value + captured) });
136136
| |
137137
| help: try this: `if let Ok(ref value) = x.field { do_nothing(value + captured) }`
138138

139-
error: called `map(f)` on an Result value where `f` is a unit closure
140-
--> $DIR/result_map_unit_fn_fixable.rs:79:5
141-
|
142-
LL | x.field.map(|value| { do_nothing(value); do_nothing(value) });
143-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
144-
| |
145-
| help: try this: `if let Ok(value) = x.field { ... }`
146-
147-
error: called `map(f)` on an Result value where `f` is a unit closure
148-
--> $DIR/result_map_unit_fn_fixable.rs:81:5
149-
|
150-
LL | x.field.map(|value| if value > 0 { do_nothing(value); do_nothing(value) });
151-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
152-
| |
153-
| help: try this: `if let Ok(value) = x.field { ... }`
154-
155-
error: called `map(f)` on an Result value where `f` is a unit closure
156-
--> $DIR/result_map_unit_fn_fixable.rs:85:5
157-
|
158-
LL | x.field.map(|value| {
159-
| _____^
160-
| |_____|
161-
| ||
162-
LL | || do_nothing(value);
163-
LL | || do_nothing(value)
164-
LL | || });
165-
| ||______^- help: try this: `if let Ok(value) = x.field { ... }`
166-
| |_______|
167-
|
168-
169-
error: called `map(f)` on an Result value where `f` is a unit closure
170-
--> $DIR/result_map_unit_fn_fixable.rs:89:5
171-
|
172-
LL | x.field.map(|value| { do_nothing(value); do_nothing(value); });
173-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
174-
| |
175-
| help: try this: `if let Ok(value) = x.field { ... }`
176-
177-
error: called `map(f)` on an Result value where `f` is a unit function
178-
--> $DIR/result_map_unit_fn_fixable.rs:93:5
179-
|
180-
LL | "12".parse::<i32>().map(diverge);
181-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
182-
| |
183-
| help: try this: `if let Ok(_) = "12".parse::<i32>() { diverge(...) }`
184-
185-
error: called `map(f)` on an Result value where `f` is a unit function
186-
--> $DIR/result_map_unit_fn_fixable.rs:99:5
187-
|
188-
LL | y.map(do_nothing);
189-
| ^^^^^^^^^^^^^^^^^-
190-
| |
191-
| help: try this: `if let Ok(_y) = y { do_nothing(...) }`
192-
193-
error: aborting due to 23 previous errors
139+
error: aborting due to 17 previous errors
194140

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#![feature(never_type)]
2+
#![warn(clippy::result_map_unit_fn)]
3+
#![allow(unused)]
4+
5+
fn do_nothing<T>(_: T) {}
6+
7+
fn diverge<T>(_: T) -> ! {
8+
panic!()
9+
}
10+
11+
fn plus_one(value: usize) -> usize {
12+
value + 1
13+
}
14+
15+
#[rustfmt::skip]
16+
fn result_map_unit_fn() {
17+
x.field.map(|value| { do_nothing(value); do_nothing(value) });
18+
19+
x.field.map(|value| if value > 0 { do_nothing(value); do_nothing(value) });
20+
21+
// Suggestion for the let block should be `{ ... }` as it's too difficult to build a
22+
// proper suggestion for these cases
23+
x.field.map(|value| {
24+
do_nothing(value);
25+
do_nothing(value)
26+
});
27+
x.field.map(|value| { do_nothing(value); do_nothing(value); });
28+
29+
// The following should suggest `if let Ok(_X) ...` as it's difficult to generate a proper let variable name for them
30+
let res: Result<!, usize> = Ok(42).map(diverge);
31+
"12".parse::<i32>().map(diverge);
32+
33+
let res: Result<(), usize> = Ok(plus_one(1)).map(do_nothing);
34+
35+
// Should suggest `if let Ok(_y) ...` to not override the existing foo variable
36+
let y: Result<usize, usize> = Ok(42);
37+
y.map(do_nothing);
38+
}
39+
40+
fn main() {}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error[E0425]: cannot find value `x` in this scope
2+
--> $DIR/result_map_unit_fn_unfixable.rs:17:5
3+
|
4+
LL | x.field.map(|value| { do_nothing(value); do_nothing(value) });
5+
| ^ not found in this scope
6+
7+
error[E0425]: cannot find value `x` in this scope
8+
--> $DIR/result_map_unit_fn_unfixable.rs:19:5
9+
|
10+
LL | x.field.map(|value| if value > 0 { do_nothing(value); do_nothing(value) });
11+
| ^ not found in this scope
12+
13+
error[E0425]: cannot find value `x` in this scope
14+
--> $DIR/result_map_unit_fn_unfixable.rs:23:5
15+
|
16+
LL | x.field.map(|value| {
17+
| ^ not found in this scope
18+
19+
error[E0425]: cannot find value `x` in this scope
20+
--> $DIR/result_map_unit_fn_unfixable.rs:27:5
21+
|
22+
LL | x.field.map(|value| { do_nothing(value); do_nothing(value); });
23+
| ^ not found in this scope
24+
25+
error: aborting due to 4 previous errors
26+
27+
For more information about this error, try `rustc --explain E0425`.

0 commit comments

Comments
 (0)