Skip to content

Commit d197fc8

Browse files
result-map-unit-fn: use "a" before Result (rust-lang#16593)
changelog: [`result-map-unit-fn`]: use "a" before `Result`
2 parents e63814d + e3d2a31 commit d197fc8

File tree

3 files changed

+33
-31
lines changed

3 files changed

+33
-31
lines changed

clippy_lints/src/map_unit_fn.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,10 @@ fn let_binding_name(cx: &LateContext<'_>, var_arg: &hir::Expr<'_>) -> String {
193193
}
194194

195195
#[must_use]
196-
fn suggestion_msg(function_type: &str, map_type: &str) -> String {
197-
format!("called `map(f)` on an `{map_type}` value where `f` is a {function_type} that returns the unit type `()`")
196+
fn suggestion_msg(function_type: &str, article: &str, map_type: &str) -> String {
197+
format!(
198+
"called `map(f)` on {article} `{map_type}` value where `f` is a {function_type} that returns the unit type `()`"
199+
)
198200
}
199201

200202
fn lint_map_unit_fn(
@@ -205,10 +207,10 @@ fn lint_map_unit_fn(
205207
) {
206208
let var_arg = &map_args.0;
207209

208-
let (map_type, variant, lint) = if cx.typeck_results().expr_ty(var_arg).is_diag_item(cx, sym::Option) {
209-
("Option", "Some", OPTION_MAP_UNIT_FN)
210+
let (article, map_type, variant, lint) = if cx.typeck_results().expr_ty(var_arg).is_diag_item(cx, sym::Option) {
211+
("an", "Option", "Some", OPTION_MAP_UNIT_FN)
210212
} else if cx.typeck_results().expr_ty(var_arg).is_diag_item(cx, sym::Result) {
211-
("Result", "Ok", RESULT_MAP_UNIT_FN)
213+
("a", "Result", "Ok", RESULT_MAP_UNIT_FN)
212214
} else {
213215
return;
214216
};
@@ -219,7 +221,7 @@ fn lint_map_unit_fn(
219221

220222
if is_unit_function(cx, fn_arg) {
221223
let mut applicability = Applicability::MachineApplicable;
222-
let msg = suggestion_msg("function", map_type);
224+
let msg = suggestion_msg("function", article, map_type);
223225
let suggestion = format!(
224226
"if let {0}({binding}) = {1} {{ {2}({binding}) }}",
225227
variant,
@@ -232,7 +234,7 @@ fn lint_map_unit_fn(
232234
diag.span_suggestion_verbose(stmt.span, SUGG_MSG, suggestion, applicability);
233235
});
234236
} else if let Some((binding, closure_expr)) = unit_closure(cx, fn_arg) {
235-
let msg = suggestion_msg("closure", map_type);
237+
let msg = suggestion_msg("closure", article, map_type);
236238

237239
span_lint_and_then(cx, lint, expr.span, msg, |diag| {
238240
if let Some((reduced_expr_span, is_unsafe)) = reduce_unit_expression(cx, closure_expr) {

tests/ui/result_map_unit_fn_fixable.stderr

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: called `map(f)` on an `Result` value where `f` is a function that returns the unit type `()`
1+
error: called `map(f)` on a `Result` value where `f` is a function that returns the unit type `()`
22
--> tests/ui/result_map_unit_fn_fixable.rs:32:5
33
|
44
LL | x.field.map(do_nothing);
@@ -12,7 +12,7 @@ LL - x.field.map(do_nothing);
1212
LL + if let Ok(x_field) = x.field { do_nothing(x_field) }
1313
|
1414

15-
error: called `map(f)` on an `Result` value where `f` is a function that returns the unit type `()`
15+
error: called `map(f)` on a `Result` value where `f` is a function that returns the unit type `()`
1616
--> tests/ui/result_map_unit_fn_fixable.rs:35:5
1717
|
1818
LL | x.field.map(do_nothing);
@@ -24,7 +24,7 @@ LL - x.field.map(do_nothing);
2424
LL + if let Ok(x_field) = x.field { do_nothing(x_field) }
2525
|
2626

27-
error: called `map(f)` on an `Result` value where `f` is a function that returns the unit type `()`
27+
error: called `map(f)` on a `Result` value where `f` is a function that returns the unit type `()`
2828
--> tests/ui/result_map_unit_fn_fixable.rs:38:5
2929
|
3030
LL | x.field.map(diverge);
@@ -36,7 +36,7 @@ LL - x.field.map(diverge);
3636
LL + if let Ok(x_field) = x.field { diverge(x_field) }
3737
|
3838

39-
error: called `map(f)` on an `Result` value where `f` is a closure that returns the unit type `()`
39+
error: called `map(f)` on a `Result` value where `f` is a closure that returns the unit type `()`
4040
--> tests/ui/result_map_unit_fn_fixable.rs:45:5
4141
|
4242
LL | x.field.map(|value| x.do_result_nothing(value + captured));
@@ -48,7 +48,7 @@ LL - x.field.map(|value| x.do_result_nothing(value + captured));
4848
LL + if let Ok(value) = x.field { x.do_result_nothing(value + captured) }
4949
|
5050

51-
error: called `map(f)` on an `Result` value where `f` is a closure that returns the unit type `()`
51+
error: called `map(f)` on a `Result` value where `f` is a closure that returns the unit type `()`
5252
--> tests/ui/result_map_unit_fn_fixable.rs:48:5
5353
|
5454
LL | x.field.map(|value| { x.do_result_plus_one(value + captured); });
@@ -60,7 +60,7 @@ LL - x.field.map(|value| { x.do_result_plus_one(value + captured); });
6060
LL + if let Ok(value) = x.field { x.do_result_plus_one(value + captured); }
6161
|
6262

63-
error: called `map(f)` on an `Result` value where `f` is a closure that returns the unit type `()`
63+
error: called `map(f)` on a `Result` value where `f` is a closure that returns the unit type `()`
6464
--> tests/ui/result_map_unit_fn_fixable.rs:52:5
6565
|
6666
LL | x.field.map(|value| do_nothing(value + captured));
@@ -72,7 +72,7 @@ LL - x.field.map(|value| do_nothing(value + captured));
7272
LL + if let Ok(value) = x.field { do_nothing(value + captured) }
7373
|
7474

75-
error: called `map(f)` on an `Result` value where `f` is a closure that returns the unit type `()`
75+
error: called `map(f)` on a `Result` value where `f` is a closure that returns the unit type `()`
7676
--> tests/ui/result_map_unit_fn_fixable.rs:55:5
7777
|
7878
LL | x.field.map(|value| { do_nothing(value + captured) });
@@ -84,7 +84,7 @@ LL - x.field.map(|value| { do_nothing(value + captured) });
8484
LL + if let Ok(value) = x.field { do_nothing(value + captured) }
8585
|
8686

87-
error: called `map(f)` on an `Result` value where `f` is a closure that returns the unit type `()`
87+
error: called `map(f)` on a `Result` value where `f` is a closure that returns the unit type `()`
8888
--> tests/ui/result_map_unit_fn_fixable.rs:58:5
8989
|
9090
LL | x.field.map(|value| { do_nothing(value + captured); });
@@ -96,7 +96,7 @@ LL - x.field.map(|value| { do_nothing(value + captured); });
9696
LL + if let Ok(value) = x.field { do_nothing(value + captured); }
9797
|
9898

99-
error: called `map(f)` on an `Result` value where `f` is a closure that returns the unit type `()`
99+
error: called `map(f)` on a `Result` value where `f` is a closure that returns the unit type `()`
100100
--> tests/ui/result_map_unit_fn_fixable.rs:61:5
101101
|
102102
LL | x.field.map(|value| { { do_nothing(value + captured); } });
@@ -108,7 +108,7 @@ LL - x.field.map(|value| { { do_nothing(value + captured); } });
108108
LL + if let Ok(value) = x.field { do_nothing(value + captured); }
109109
|
110110

111-
error: called `map(f)` on an `Result` value where `f` is a closure that returns the unit type `()`
111+
error: called `map(f)` on a `Result` value where `f` is a closure that returns the unit type `()`
112112
--> tests/ui/result_map_unit_fn_fixable.rs:65:5
113113
|
114114
LL | x.field.map(|value| diverge(value + captured));
@@ -120,7 +120,7 @@ LL - x.field.map(|value| diverge(value + captured));
120120
LL + if let Ok(value) = x.field { diverge(value + captured) }
121121
|
122122

123-
error: called `map(f)` on an `Result` value where `f` is a closure that returns the unit type `()`
123+
error: called `map(f)` on a `Result` value where `f` is a closure that returns the unit type `()`
124124
--> tests/ui/result_map_unit_fn_fixable.rs:68:5
125125
|
126126
LL | x.field.map(|value| { diverge(value + captured) });
@@ -132,7 +132,7 @@ LL - x.field.map(|value| { diverge(value + captured) });
132132
LL + if let Ok(value) = x.field { diverge(value + captured) }
133133
|
134134

135-
error: called `map(f)` on an `Result` value where `f` is a closure that returns the unit type `()`
135+
error: called `map(f)` on a `Result` value where `f` is a closure that returns the unit type `()`
136136
--> tests/ui/result_map_unit_fn_fixable.rs:71:5
137137
|
138138
LL | x.field.map(|value| { diverge(value + captured); });
@@ -144,7 +144,7 @@ LL - x.field.map(|value| { diverge(value + captured); });
144144
LL + if let Ok(value) = x.field { diverge(value + captured); }
145145
|
146146

147-
error: called `map(f)` on an `Result` value where `f` is a closure that returns the unit type `()`
147+
error: called `map(f)` on a `Result` value where `f` is a closure that returns the unit type `()`
148148
--> tests/ui/result_map_unit_fn_fixable.rs:74:5
149149
|
150150
LL | x.field.map(|value| { { diverge(value + captured); } });
@@ -156,7 +156,7 @@ LL - x.field.map(|value| { { diverge(value + captured); } });
156156
LL + if let Ok(value) = x.field { diverge(value + captured); }
157157
|
158158

159-
error: called `map(f)` on an `Result` value where `f` is a closure that returns the unit type `()`
159+
error: called `map(f)` on a `Result` value where `f` is a closure that returns the unit type `()`
160160
--> tests/ui/result_map_unit_fn_fixable.rs:80:5
161161
|
162162
LL | x.field.map(|value| { let y = plus_one(value + captured); });
@@ -168,7 +168,7 @@ LL - x.field.map(|value| { let y = plus_one(value + captured); });
168168
LL + if let Ok(value) = x.field { let y = plus_one(value + captured); }
169169
|
170170

171-
error: called `map(f)` on an `Result` value where `f` is a closure that returns the unit type `()`
171+
error: called `map(f)` on a `Result` value where `f` is a closure that returns the unit type `()`
172172
--> tests/ui/result_map_unit_fn_fixable.rs:83:5
173173
|
174174
LL | x.field.map(|value| { plus_one(value + captured); });
@@ -180,7 +180,7 @@ LL - x.field.map(|value| { plus_one(value + captured); });
180180
LL + if let Ok(value) = x.field { plus_one(value + captured); }
181181
|
182182

183-
error: called `map(f)` on an `Result` value where `f` is a closure that returns the unit type `()`
183+
error: called `map(f)` on a `Result` value where `f` is a closure that returns the unit type `()`
184184
--> tests/ui/result_map_unit_fn_fixable.rs:86:5
185185
|
186186
LL | x.field.map(|value| { { plus_one(value + captured); } });
@@ -192,7 +192,7 @@ LL - x.field.map(|value| { { plus_one(value + captured); } });
192192
LL + if let Ok(value) = x.field { plus_one(value + captured); }
193193
|
194194

195-
error: called `map(f)` on an `Result` value where `f` is a closure that returns the unit type `()`
195+
error: called `map(f)` on a `Result` value where `f` is a closure that returns the unit type `()`
196196
--> tests/ui/result_map_unit_fn_fixable.rs:90:5
197197
|
198198
LL | x.field.map(|ref value| { do_nothing(value + captured) });
@@ -204,7 +204,7 @@ LL - x.field.map(|ref value| { do_nothing(value + captured) });
204204
LL + if let Ok(ref value) = x.field { do_nothing(value + captured) }
205205
|
206206

207-
error: called `map(f)` on an `Result` value where `f` is a closure that returns the unit type `()`
207+
error: called `map(f)` on a `Result` value where `f` is a closure that returns the unit type `()`
208208
--> tests/ui/result_map_unit_fn_fixable.rs:93:5
209209
|
210210
LL | x.field.map(|value| println!("{value:?}"));

tests/ui/result_map_unit_fn_unfixable.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: called `map(f)` on an `Result` value where `f` is a closure that returns the unit type `()`
1+
error: called `map(f)` on a `Result` value where `f` is a closure that returns the unit type `()`
22
--> tests/ui/result_map_unit_fn_unfixable.rs:24:5
33
|
44
LL | x.field.map(|value| { do_nothing(value); do_nothing(value) });
@@ -12,7 +12,7 @@ LL - x.field.map(|value| { do_nothing(value); do_nothing(value) });
1212
LL + if let Ok(value) = x.field { ... }
1313
|
1414

15-
error: called `map(f)` on an `Result` value where `f` is a closure that returns the unit type `()`
15+
error: called `map(f)` on a `Result` value where `f` is a closure that returns the unit type `()`
1616
--> tests/ui/result_map_unit_fn_unfixable.rs:29:5
1717
|
1818
LL | x.field.map(|value| if value > 0 { do_nothing(value); do_nothing(value) });
@@ -24,7 +24,7 @@ LL - x.field.map(|value| if value > 0 { do_nothing(value); do_nothing(value)
2424
LL + if let Ok(value) = x.field { ... }
2525
|
2626

27-
error: called `map(f)` on an `Result` value where `f` is a closure that returns the unit type `()`
27+
error: called `map(f)` on a `Result` value where `f` is a closure that returns the unit type `()`
2828
--> tests/ui/result_map_unit_fn_unfixable.rs:35:5
2929
|
3030
LL | / x.field.map(|value| {
@@ -46,7 +46,7 @@ LL - });
4646
LL + if let Ok(value) = x.field { ... }
4747
|
4848

49-
error: called `map(f)` on an `Result` value where `f` is a closure that returns the unit type `()`
49+
error: called `map(f)` on a `Result` value where `f` is a closure that returns the unit type `()`
5050
--> tests/ui/result_map_unit_fn_unfixable.rs:41:5
5151
|
5252
LL | x.field.map(|value| { do_nothing(value); do_nothing(value); });
@@ -58,7 +58,7 @@ LL - x.field.map(|value| { do_nothing(value); do_nothing(value); });
5858
LL + if let Ok(value) = x.field { ... }
5959
|
6060

61-
error: called `map(f)` on an `Result` value where `f` is a function that returns the unit type `()`
61+
error: called `map(f)` on a `Result` value where `f` is a function that returns the unit type `()`
6262
--> tests/ui/result_map_unit_fn_unfixable.rs:47:5
6363
|
6464
LL | "12".parse::<i32>().map(diverge);
@@ -70,7 +70,7 @@ LL - "12".parse::<i32>().map(diverge);
7070
LL + if let Ok(a) = "12".parse::<i32>() { diverge(a) }
7171
|
7272

73-
error: called `map(f)` on an `Result` value where `f` is a function that returns the unit type `()`
73+
error: called `map(f)` on a `Result` value where `f` is a function that returns the unit type `()`
7474
--> tests/ui/result_map_unit_fn_unfixable.rs:55:5
7575
|
7676
LL | y.map(do_nothing);

0 commit comments

Comments
 (0)