Skip to content

Commit 64b7624

Browse files
committed
Auto merge of #5292 - jpospychala:map-placeholder, r=flip1995
Improve placeholder in map_unit_fn Instead of using `_` as a default placeholder use `a`. fixes #5180 changelog: Improve default placeholder in map_unit_fn
2 parents d8f64b6 + 697e3c8 commit 64b7624

File tree

4 files changed

+43
-23
lines changed

4 files changed

+43
-23
lines changed

clippy_lints/src/map_unit_fn.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,12 @@ fn unit_closure<'a, 'tcx>(
186186
/// `x.field` => `x_field`
187187
/// `y` => `_y`
188188
///
189-
/// Anything else will return `_`.
189+
/// Anything else will return `a`.
190190
fn let_binding_name(cx: &LateContext<'_, '_>, var_arg: &hir::Expr<'_>) -> String {
191191
match &var_arg.kind {
192192
hir::ExprKind::Field(_, _) => snippet(cx, var_arg.span, "_").replace(".", "_"),
193193
hir::ExprKind::Path(_) => format!("_{}", snippet(cx, var_arg.span, "")),
194-
_ => "_".to_string(),
194+
_ => "a".to_string(),
195195
}
196196
}
197197

tests/ui/option_map_unit_fn_fixable.fixed

+7-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ fn plus_one(value: usize) -> usize {
1313
value + 1
1414
}
1515

16+
fn option() -> Option<usize> {
17+
Some(10)
18+
}
19+
1620
struct HasOption {
1721
field: Option<usize>,
1822
}
@@ -73,6 +77,8 @@ fn option_map_unit_fn() {
7377
if let Some(value) = x.field { plus_one(value + captured); }
7478

7579

76-
if let Some(ref value) = x.field { do_nothing(value + captured) }}
80+
if let Some(ref value) = x.field { do_nothing(value + captured) }
81+
82+
if let Some(a) = option() { do_nothing(a) }}
7783

7884
fn main() {}

tests/ui/option_map_unit_fn_fixable.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ fn plus_one(value: usize) -> usize {
1313
value + 1
1414
}
1515

16+
fn option() -> Option<usize> {
17+
Some(10)
18+
}
19+
1620
struct HasOption {
1721
field: Option<usize>,
1822
}
@@ -73,6 +77,8 @@ fn option_map_unit_fn() {
7377
x.field.map(|value| { { plus_one(value + captured); } });
7478

7579

76-
x.field.map(|ref value| { do_nothing(value + captured) });}
80+
x.field.map(|ref value| { do_nothing(value + captured) });
81+
82+
option().map(do_nothing);}
7783

7884
fn main() {}
+27-19
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 function that returns the unit type
2-
--> $DIR/option_map_unit_fn_fixable.rs:34:5
2+
--> $DIR/option_map_unit_fn_fixable.rs:38:5
33
|
44
LL | x.field.map(do_nothing);
55
| ^^^^^^^^^^^^^^^^^^^^^^^-
@@ -9,132 +9,140 @@ 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 function that returns the unit type
12-
--> $DIR/option_map_unit_fn_fixable.rs:36:5
12+
--> $DIR/option_map_unit_fn_fixable.rs:40: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 function that returns the unit type
20-
--> $DIR/option_map_unit_fn_fixable.rs:38:5
20+
--> $DIR/option_map_unit_fn_fixable.rs:42: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 closure that returns the unit type
28-
--> $DIR/option_map_unit_fn_fixable.rs:44:5
28+
--> $DIR/option_map_unit_fn_fixable.rs:48: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 closure that returns the unit type
36-
--> $DIR/option_map_unit_fn_fixable.rs:46:5
36+
--> $DIR/option_map_unit_fn_fixable.rs:50: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 closure that returns the unit type
44-
--> $DIR/option_map_unit_fn_fixable.rs:49:5
44+
--> $DIR/option_map_unit_fn_fixable.rs:53: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 closure that returns the unit type
52-
--> $DIR/option_map_unit_fn_fixable.rs:51:5
52+
--> $DIR/option_map_unit_fn_fixable.rs:55: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 closure that returns the unit type
60-
--> $DIR/option_map_unit_fn_fixable.rs:53:5
60+
--> $DIR/option_map_unit_fn_fixable.rs:57: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 closure that returns the unit type
68-
--> $DIR/option_map_unit_fn_fixable.rs:55:5
68+
--> $DIR/option_map_unit_fn_fixable.rs:59: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 closure that returns the unit type
76-
--> $DIR/option_map_unit_fn_fixable.rs:58:5
76+
--> $DIR/option_map_unit_fn_fixable.rs:62: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 closure that returns the unit type
84-
--> $DIR/option_map_unit_fn_fixable.rs:60:5
84+
--> $DIR/option_map_unit_fn_fixable.rs:64: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 closure that returns the unit type
92-
--> $DIR/option_map_unit_fn_fixable.rs:62:5
92+
--> $DIR/option_map_unit_fn_fixable.rs:66: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 closure that returns the unit type
100-
--> $DIR/option_map_unit_fn_fixable.rs:64:5
100+
--> $DIR/option_map_unit_fn_fixable.rs:68: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 closure that returns the unit type
108-
--> $DIR/option_map_unit_fn_fixable.rs:69:5
108+
--> $DIR/option_map_unit_fn_fixable.rs:73: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 closure that returns the unit type
116-
--> $DIR/option_map_unit_fn_fixable.rs:71:5
116+
--> $DIR/option_map_unit_fn_fixable.rs:75: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 closure that returns the unit type
124-
--> $DIR/option_map_unit_fn_fixable.rs:73:5
124+
--> $DIR/option_map_unit_fn_fixable.rs:77: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 closure that returns the unit type
132-
--> $DIR/option_map_unit_fn_fixable.rs:76:5
132+
--> $DIR/option_map_unit_fn_fixable.rs:80: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: aborting due to 17 previous errors
139+
error: called `map(f)` on an `Option` value where `f` is a function that returns the unit type
140+
--> $DIR/option_map_unit_fn_fixable.rs:82:5
141+
|
142+
LL | option().map(do_nothing);}
143+
| ^^^^^^^^^^^^^^^^^^^^^^^^-
144+
| |
145+
| help: try this: `if let Some(a) = option() { do_nothing(a) }`
146+
147+
error: aborting due to 18 previous errors
140148

0 commit comments

Comments
 (0)