Skip to content

Commit b0ecbdf

Browse files
committed
fix option_map_unit_fn_unfixable.rs
The errors were unrelated to the lint...
1 parent 4463ba7 commit b0ecbdf

File tree

2 files changed

+75
-19
lines changed

2 files changed

+75
-19
lines changed
Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
//@no-rustfix
12
#![warn(clippy::option_map_unit_fn)]
3+
#![allow(clippy::unnecessary_wraps, clippy::unnecessary_map_on_constructor)] // only fires before the fix
24

35
fn do_nothing<T>(_: T) {}
46

@@ -10,33 +12,42 @@ fn plus_one(value: usize) -> usize {
1012
value + 1
1113
}
1214

15+
struct HasOption {
16+
field: Option<usize>,
17+
}
18+
1319
#[rustfmt::skip]
1420
fn option_map_unit_fn() {
21+
let x = HasOption { field: Some(10) };
1522

1623
x.field.map(|value| { do_nothing(value); do_nothing(value) });
17-
//~^ ERROR: cannot find value
24+
//~^ option_map_unit_fn
1825

1926
x.field.map(|value| if value > 0 { do_nothing(value); do_nothing(value) });
20-
//~^ ERROR: cannot find value
27+
//~^ option_map_unit_fn
2128

2229
// Suggestion for the let block should be `{ ... }` as it's too difficult to build a
2330
// proper suggestion for these cases
2431
x.field.map(|value| {
2532
do_nothing(value);
2633
do_nothing(value)
2734
});
28-
//~^^^^ ERROR: cannot find value
35+
//~^^^^ option_map_unit_fn
2936
x.field.map(|value| { do_nothing(value); do_nothing(value); });
30-
//~^ ERROR: cannot find value
37+
//~^ option_map_unit_fn
3138

3239
// The following should suggest `if let Some(_X) ...` as it's difficult to generate a proper let variable name for them
3340
Some(42).map(diverge);
41+
//~^ option_map_unit_fn
3442
"12".parse::<i32>().ok().map(diverge);
43+
//~^ option_map_unit_fn
3544
Some(plus_one(1)).map(do_nothing);
45+
//~^ option_map_unit_fn
3646

3747
// Should suggest `if let Some(_y) ...` to not override the existing foo variable
3848
let y = Some(42);
3949
y.map(do_nothing);
50+
//~^ option_map_unit_fn
4051
}
4152

4253
fn main() {}

tests/ui/option_map_unit_fn_unfixable.stderr

Lines changed: 60 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,72 @@
1-
error[E0425]: cannot find value `x` in this scope
2-
--> tests/ui/option_map_unit_fn_unfixable.rs:16:5
1+
error: called `map(f)` on an `Option` value where `f` is a closure that returns the unit type `()`
2+
--> tests/ui/option_map_unit_fn_unfixable.rs:23:5
33
|
44
LL | x.field.map(|value| { do_nothing(value); do_nothing(value) });
5-
| ^ not found in this scope
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
6+
| |
7+
| help: try: `if let Some(value) = x.field { ... }`
8+
|
9+
= note: `-D clippy::option-map-unit-fn` implied by `-D warnings`
10+
= help: to override `-D warnings` add `#[allow(clippy::option_map_unit_fn)]`
611

7-
error[E0425]: cannot find value `x` in this scope
8-
--> tests/ui/option_map_unit_fn_unfixable.rs:19:5
12+
error: called `map(f)` on an `Option` value where `f` is a closure that returns the unit type `()`
13+
--> tests/ui/option_map_unit_fn_unfixable.rs:26:5
914
|
1015
LL | x.field.map(|value| if value > 0 { do_nothing(value); do_nothing(value) });
11-
| ^ not found in this scope
16+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
17+
| |
18+
| help: try: `if let Some(value) = x.field { ... }`
1219

13-
error[E0425]: cannot find value `x` in this scope
14-
--> tests/ui/option_map_unit_fn_unfixable.rs:24:5
20+
error: called `map(f)` on an `Option` value where `f` is a closure that returns the unit type `()`
21+
--> tests/ui/option_map_unit_fn_unfixable.rs:31:5
22+
|
23+
LL | // x.field.map(|value| {
24+
LL | || do_nothing(value);
25+
LL | || do_nothing(value)
26+
LL | || });
27+
| ||______^- help: try: `if let Some(value) = x.field { ... }`
28+
| |______|
1529
|
16-
LL | x.field.map(|value| {
17-
| ^ not found in this scope
1830

19-
error[E0425]: cannot find value `x` in this scope
20-
--> tests/ui/option_map_unit_fn_unfixable.rs:29:5
31+
error: called `map(f)` on an `Option` value where `f` is a closure that returns the unit type `()`
32+
--> tests/ui/option_map_unit_fn_unfixable.rs:36:5
2133
|
2234
LL | x.field.map(|value| { do_nothing(value); do_nothing(value); });
23-
| ^ not found in this scope
35+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
36+
| |
37+
| help: try: `if let Some(value) = x.field { ... }`
38+
39+
error: called `map(f)` on an `Option` value where `f` is a function that returns the unit type `()`
40+
--> tests/ui/option_map_unit_fn_unfixable.rs:40:5
41+
|
42+
LL | Some(42).map(diverge);
43+
| ^^^^^^^^^^^^^^^^^^^^^-
44+
| |
45+
| help: try: `if let Some(a) = Some(42) { diverge(a) }`
46+
47+
error: called `map(f)` on an `Option` value where `f` is a function that returns the unit type `()`
48+
--> tests/ui/option_map_unit_fn_unfixable.rs:42:5
49+
|
50+
LL | "12".parse::<i32>().ok().map(diverge);
51+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
52+
| |
53+
| help: try: `if let Some(a) = "12".parse::<i32>().ok() { diverge(a) }`
54+
55+
error: called `map(f)` on an `Option` value where `f` is a function that returns the unit type `()`
56+
--> tests/ui/option_map_unit_fn_unfixable.rs:44:5
57+
|
58+
LL | Some(plus_one(1)).map(do_nothing);
59+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
60+
| |
61+
| help: try: `if let Some(a) = Some(plus_one(1)) { do_nothing(a) }`
62+
63+
error: called `map(f)` on an `Option` value where `f` is a function that returns the unit type `()`
64+
--> tests/ui/option_map_unit_fn_unfixable.rs:49:5
65+
|
66+
LL | y.map(do_nothing);
67+
| ^^^^^^^^^^^^^^^^^-
68+
| |
69+
| help: try: `if let Some(_y) = y { do_nothing(_y) }`
2470

25-
error: aborting due to 4 previous errors
71+
error: aborting due to 8 previous errors
2672

27-
For more information about this error, try `rustc --explain E0425`.

0 commit comments

Comments
 (0)