Skip to content

Commit 98db098

Browse files
committed
improve the suggestion
- make them `verbose` -- the ones spanning multiple lines were especially egregious - give a more descriptive help message
1 parent b0ecbdf commit 98db098

File tree

5 files changed

+389
-170
lines changed

5 files changed

+389
-170
lines changed

clippy_lints/src/map_unit_fn.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,9 @@ fn lint_map_unit_fn(
214214
};
215215
let fn_arg = &map_args.1[0];
216216

217+
#[expect(clippy::items_after_statements, reason = "the const is only used below")]
218+
const SUGG_MSG: &str = "use `if let` instead";
219+
217220
if is_unit_function(cx, fn_arg) {
218221
let mut applicability = Applicability::MachineApplicable;
219222
let msg = suggestion_msg("function", map_type);
@@ -226,7 +229,7 @@ fn lint_map_unit_fn(
226229
);
227230

228231
span_lint_and_then(cx, lint, expr.span, msg, |diag| {
229-
diag.span_suggestion(stmt.span, "try", suggestion, applicability);
232+
diag.span_suggestion_verbose(stmt.span, SUGG_MSG, suggestion, applicability);
230233
});
231234
} else if let Some((binding, closure_expr)) = unit_closure(cx, fn_arg) {
232235
let msg = suggestion_msg("closure", map_type);
@@ -242,15 +245,15 @@ fn lint_map_unit_fn(
242245
snippet_with_applicability(cx, var_arg.span, "_", &mut applicability),
243246
snippet_with_context(cx, reduced_expr_span, var_arg.span.ctxt(), "_", &mut applicability).0,
244247
);
245-
diag.span_suggestion(stmt.span, "try", suggestion, applicability);
248+
diag.span_suggestion_verbose(stmt.span, SUGG_MSG, suggestion, applicability);
246249
} else {
247250
let suggestion = format!(
248251
"if let {0}({1}) = {2} {{ ... }}",
249252
variant,
250253
snippet(cx, binding.pat.span, "_"),
251254
snippet(cx, var_arg.span, "_"),
252255
);
253-
diag.span_suggestion(stmt.span, "try", suggestion, Applicability::HasPlaceholders);
256+
diag.span_suggestion_verbose(stmt.span, SUGG_MSG, suggestion, Applicability::HasPlaceholders);
254257
}
255258
});
256259
}

tests/ui/option_map_unit_fn_fixable.stderr

Lines changed: 146 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2,172 +2,255 @@ error: called `map(f)` on an `Option` value where `f` is a function that returns
22
--> tests/ui/option_map_unit_fn_fixable.rs:36:5
33
|
44
LL | x.field.map(do_nothing);
5-
| ^^^^^^^^^^^^^^^^^^^^^^^-
6-
| |
7-
| help: try: `if let Some(x_field) = x.field { do_nothing(x_field) }`
5+
| ^^^^^^^^^^^^^^^^^^^^^^^
86
|
97
= note: `-D clippy::option-map-unit-fn` implied by `-D warnings`
108
= help: to override `-D warnings` add `#[allow(clippy::option_map_unit_fn)]`
9+
help: use `if let` instead
10+
|
11+
LL - x.field.map(do_nothing);
12+
LL + if let Some(x_field) = x.field { do_nothing(x_field) }
13+
|
1114

1215
error: called `map(f)` on an `Option` value where `f` is a function that returns the unit type `()`
1316
--> tests/ui/option_map_unit_fn_fixable.rs:39:5
1417
|
1518
LL | x.field.map(do_nothing);
16-
| ^^^^^^^^^^^^^^^^^^^^^^^-
17-
| |
18-
| help: try: `if let Some(x_field) = x.field { do_nothing(x_field) }`
19+
| ^^^^^^^^^^^^^^^^^^^^^^^
20+
|
21+
help: use `if let` instead
22+
|
23+
LL - x.field.map(do_nothing);
24+
LL + if let Some(x_field) = x.field { do_nothing(x_field) }
25+
|
1926

2027
error: called `map(f)` on an `Option` value where `f` is a function that returns the unit type `()`
2128
--> tests/ui/option_map_unit_fn_fixable.rs:42:5
2229
|
2330
LL | x.field.map(diverge);
24-
| ^^^^^^^^^^^^^^^^^^^^-
25-
| |
26-
| help: try: `if let Some(x_field) = x.field { diverge(x_field) }`
31+
| ^^^^^^^^^^^^^^^^^^^^
32+
|
33+
help: use `if let` instead
34+
|
35+
LL - x.field.map(diverge);
36+
LL + if let Some(x_field) = x.field { diverge(x_field) }
37+
|
2738

2839
error: called `map(f)` on an `Option` value where `f` is a closure that returns the unit type `()`
2940
--> tests/ui/option_map_unit_fn_fixable.rs:49:5
3041
|
3142
LL | x.field.map(|value| x.do_option_nothing(value + captured));
32-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
33-
| |
34-
| help: try: `if let Some(value) = x.field { x.do_option_nothing(value + captured) }`
43+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
44+
|
45+
help: use `if let` instead
46+
|
47+
LL - x.field.map(|value| x.do_option_nothing(value + captured));
48+
LL + if let Some(value) = x.field { x.do_option_nothing(value + captured) }
49+
|
3550

3651
error: called `map(f)` on an `Option` value where `f` is a closure that returns the unit type `()`
3752
--> tests/ui/option_map_unit_fn_fixable.rs:52:5
3853
|
3954
LL | x.field.map(|value| { x.do_option_plus_one(value + captured); });
40-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
41-
| |
42-
| help: try: `if let Some(value) = x.field { x.do_option_plus_one(value + captured); }`
55+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
56+
|
57+
help: use `if let` instead
58+
|
59+
LL - x.field.map(|value| { x.do_option_plus_one(value + captured); });
60+
LL + if let Some(value) = x.field { x.do_option_plus_one(value + captured); }
61+
|
4362

4463
error: called `map(f)` on an `Option` value where `f` is a closure that returns the unit type `()`
4564
--> tests/ui/option_map_unit_fn_fixable.rs:56:5
4665
|
4766
LL | x.field.map(|value| do_nothing(value + captured));
48-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
49-
| |
50-
| help: try: `if let Some(value) = x.field { do_nothing(value + captured) }`
67+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
68+
|
69+
help: use `if let` instead
70+
|
71+
LL - x.field.map(|value| do_nothing(value + captured));
72+
LL + if let Some(value) = x.field { do_nothing(value + captured) }
73+
|
5174

5275
error: called `map(f)` on an `Option` value where `f` is a closure that returns the unit type `()`
5376
--> tests/ui/option_map_unit_fn_fixable.rs:59:5
5477
|
5578
LL | x.field.map(|value| { do_nothing(value + captured) });
56-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
57-
| |
58-
| help: try: `if let Some(value) = x.field { do_nothing(value + captured) }`
79+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
80+
|
81+
help: use `if let` instead
82+
|
83+
LL - x.field.map(|value| { do_nothing(value + captured) });
84+
LL + if let Some(value) = x.field { do_nothing(value + captured) }
85+
|
5986

6087
error: called `map(f)` on an `Option` value where `f` is a closure that returns the unit type `()`
6188
--> tests/ui/option_map_unit_fn_fixable.rs:62:5
6289
|
6390
LL | x.field.map(|value| { do_nothing(value + captured); });
64-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
65-
| |
66-
| help: try: `if let Some(value) = x.field { do_nothing(value + captured); }`
91+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
92+
|
93+
help: use `if let` instead
94+
|
95+
LL - x.field.map(|value| { do_nothing(value + captured); });
96+
LL + if let Some(value) = x.field { do_nothing(value + captured); }
97+
|
6798

6899
error: called `map(f)` on an `Option` value where `f` is a closure that returns the unit type `()`
69100
--> tests/ui/option_map_unit_fn_fixable.rs:65:5
70101
|
71102
LL | x.field.map(|value| { { do_nothing(value + captured); } });
72-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
73-
| |
74-
| help: try: `if let Some(value) = x.field { do_nothing(value + captured); }`
103+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
104+
|
105+
help: use `if let` instead
106+
|
107+
LL - x.field.map(|value| { { do_nothing(value + captured); } });
108+
LL + if let Some(value) = x.field { do_nothing(value + captured); }
109+
|
75110

76111
error: called `map(f)` on an `Option` value where `f` is a closure that returns the unit type `()`
77112
--> tests/ui/option_map_unit_fn_fixable.rs:69:5
78113
|
79114
LL | x.field.map(|value| diverge(value + captured));
80-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
81-
| |
82-
| help: try: `if let Some(value) = x.field { diverge(value + captured) }`
115+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
116+
|
117+
help: use `if let` instead
118+
|
119+
LL - x.field.map(|value| diverge(value + captured));
120+
LL + if let Some(value) = x.field { diverge(value + captured) }
121+
|
83122

84123
error: called `map(f)` on an `Option` value where `f` is a closure that returns the unit type `()`
85124
--> tests/ui/option_map_unit_fn_fixable.rs:72:5
86125
|
87126
LL | x.field.map(|value| { diverge(value + captured) });
88-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
89-
| |
90-
| help: try: `if let Some(value) = x.field { diverge(value + captured) }`
127+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
128+
|
129+
help: use `if let` instead
130+
|
131+
LL - x.field.map(|value| { diverge(value + captured) });
132+
LL + if let Some(value) = x.field { diverge(value + captured) }
133+
|
91134

92135
error: called `map(f)` on an `Option` value where `f` is a closure that returns the unit type `()`
93136
--> tests/ui/option_map_unit_fn_fixable.rs:75:5
94137
|
95138
LL | x.field.map(|value| { diverge(value + captured); });
96-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
97-
| |
98-
| help: try: `if let Some(value) = x.field { diverge(value + captured); }`
139+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
140+
|
141+
help: use `if let` instead
142+
|
143+
LL - x.field.map(|value| { diverge(value + captured); });
144+
LL + if let Some(value) = x.field { diverge(value + captured); }
145+
|
99146

100147
error: called `map(f)` on an `Option` value where `f` is a closure that returns the unit type `()`
101148
--> tests/ui/option_map_unit_fn_fixable.rs:78:5
102149
|
103150
LL | x.field.map(|value| { { diverge(value + captured); } });
104-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
105-
| |
106-
| help: try: `if let Some(value) = x.field { diverge(value + captured); }`
151+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
152+
|
153+
help: use `if let` instead
154+
|
155+
LL - x.field.map(|value| { { diverge(value + captured); } });
156+
LL + if let Some(value) = x.field { diverge(value + captured); }
157+
|
107158

108159
error: called `map(f)` on an `Option` value where `f` is a closure that returns the unit type `()`
109160
--> tests/ui/option_map_unit_fn_fixable.rs:84:5
110161
|
111162
LL | x.field.map(|value| { let y = plus_one(value + captured); });
112-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
113-
| |
114-
| help: try: `if let Some(value) = x.field { let y = plus_one(value + captured); }`
163+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
164+
|
165+
help: use `if let` instead
166+
|
167+
LL - x.field.map(|value| { let y = plus_one(value + captured); });
168+
LL + if let Some(value) = x.field { let y = plus_one(value + captured); }
169+
|
115170

116171
error: called `map(f)` on an `Option` value where `f` is a closure that returns the unit type `()`
117172
--> tests/ui/option_map_unit_fn_fixable.rs:87:5
118173
|
119174
LL | x.field.map(|value| { plus_one(value + captured); });
120-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
121-
| |
122-
| help: try: `if let Some(value) = x.field { plus_one(value + captured); }`
175+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
176+
|
177+
help: use `if let` instead
178+
|
179+
LL - x.field.map(|value| { plus_one(value + captured); });
180+
LL + if let Some(value) = x.field { plus_one(value + captured); }
181+
|
123182

124183
error: called `map(f)` on an `Option` value where `f` is a closure that returns the unit type `()`
125184
--> tests/ui/option_map_unit_fn_fixable.rs:90:5
126185
|
127186
LL | x.field.map(|value| { { plus_one(value + captured); } });
128-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
129-
| |
130-
| help: try: `if let Some(value) = x.field { plus_one(value + captured); }`
187+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
188+
|
189+
help: use `if let` instead
190+
|
191+
LL - x.field.map(|value| { { plus_one(value + captured); } });
192+
LL + if let Some(value) = x.field { plus_one(value + captured); }
193+
|
131194

132195
error: called `map(f)` on an `Option` value where `f` is a closure that returns the unit type `()`
133196
--> tests/ui/option_map_unit_fn_fixable.rs:94:5
134197
|
135198
LL | x.field.map(|ref value| { do_nothing(value + captured) });
136-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
137-
| |
138-
| help: try: `if let Some(ref value) = x.field { do_nothing(value + captured) }`
199+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
200+
|
201+
help: use `if let` instead
202+
|
203+
LL - x.field.map(|ref value| { do_nothing(value + captured) });
204+
LL + if let Some(ref value) = x.field { do_nothing(value + captured) }
205+
|
139206

140207
error: called `map(f)` on an `Option` value where `f` is a function that returns the unit type `()`
141208
--> tests/ui/option_map_unit_fn_fixable.rs:97:5
142209
|
143210
LL | option().map(do_nothing);
144-
| ^^^^^^^^^^^^^^^^^^^^^^^^-
145-
| |
146-
| help: try: `if let Some(a) = option() { do_nothing(a) }`
211+
| ^^^^^^^^^^^^^^^^^^^^^^^^
212+
|
213+
help: use `if let` instead
214+
|
215+
LL - option().map(do_nothing);
216+
LL + if let Some(a) = option() { do_nothing(a) }
217+
|
147218

148219
error: called `map(f)` on an `Option` value where `f` is a closure that returns the unit type `()`
149220
--> tests/ui/option_map_unit_fn_fixable.rs:100:5
150221
|
151222
LL | option().map(|value| println!("{value:?}"));
152-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
153-
| |
154-
| help: try: `if let Some(value) = option() { println!("{value:?}") }`
223+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
224+
|
225+
help: use `if let` instead
226+
|
227+
LL - option().map(|value| println!("{value:?}"));
228+
LL + if let Some(value) = option() { println!("{value:?}") }
229+
|
155230

156231
error: called `map(f)` on an `Option` value where `f` is a closure that returns the unit type `()`
157232
--> tests/ui/option_map_unit_fn_fixable.rs:107:5
158233
|
159234
LL | x.map(|x| unsafe { f(x) });
160-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^-
161-
| |
162-
| help: try: `if let Some(x) = x { unsafe { f(x) } }`
235+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
236+
|
237+
help: use `if let` instead
238+
|
239+
LL - x.map(|x| unsafe { f(x) });
240+
LL + if let Some(x) = x { unsafe { f(x) } }
241+
|
163242

164243
error: called `map(f)` on an `Option` value where `f` is a closure that returns the unit type `()`
165244
--> tests/ui/option_map_unit_fn_fixable.rs:109:5
166245
|
167246
LL | x.map(|x| unsafe { { f(x) } });
168-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
169-
| |
170-
| help: try: `if let Some(x) = x { unsafe { f(x) } }`
247+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
248+
|
249+
help: use `if let` instead
250+
|
251+
LL - x.map(|x| unsafe { { f(x) } });
252+
LL + if let Some(x) = x { unsafe { f(x) } }
253+
|
171254

172255
error: aborting due to 21 previous errors
173256

0 commit comments

Comments
 (0)