Skip to content

Commit 0d05b6c

Browse files
committed
make the suggestions verbose
1 parent 5f2b4e5 commit 0d05b6c

File tree

3 files changed

+210
-93
lines changed

3 files changed

+210
-93
lines changed

clippy_lints/src/map_unit_fn.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ fn lint_map_unit_fn(
226226
);
227227

228228
span_lint_and_then(cx, lint, expr.span, msg, |diag| {
229-
diag.span_suggestion(stmt.span, "try", suggestion, applicability);
229+
diag.span_suggestion_verbose(stmt.span, "try", suggestion, applicability);
230230
});
231231
} else if let Some((binding, closure_expr)) = unit_closure(cx, fn_arg) {
232232
let msg = suggestion_msg("closure", map_type);
@@ -242,15 +242,15 @@ fn lint_map_unit_fn(
242242
snippet_with_applicability(cx, var_arg.span, "_", &mut applicability),
243243
snippet_with_context(cx, reduced_expr_span, var_arg.span.ctxt(), "_", &mut applicability).0,
244244
);
245-
diag.span_suggestion(stmt.span, "try", suggestion, applicability);
245+
diag.span_suggestion_verbose(stmt.span, "try", suggestion, applicability);
246246
} else {
247247
let suggestion = format!(
248248
"if let {0}({1}) = {2} {{ ... }}",
249249
variant,
250250
snippet(cx, binding.pat.span, "_"),
251251
snippet(cx, var_arg.span, "_"),
252252
);
253-
diag.span_suggestion(stmt.span, "try", suggestion, Applicability::HasPlaceholders);
253+
diag.span_suggestion_verbose(stmt.span, "try", suggestion, Applicability::HasPlaceholders);
254254
}
255255
});
256256
}

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: try
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: try
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: try
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: try
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: try
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: try
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: try
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: try
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: try
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: try
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: try
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: try
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: try
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: try
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: try
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: try
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: try
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: try
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: try
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: try
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: try
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)