Skip to content

Commit d9819c3

Browse files
committed
Auto merge of #8584 - Alexendoo:map-unit-fn-context, r=Manishearth
Provide suggestion context in map_unit_fn Fixes #8569 changelog: Fix incorrect suggestion for `option_map_unit_fn` , `result_map_unit_fn`
2 parents 6206086 + 610db04 commit d9819c3

7 files changed

+41
-18
lines changed

clippy_lints/src/map_unit_fn.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
2-
use clippy_utils::source::snippet;
2+
use clippy_utils::source::{snippet, snippet_with_applicability, snippet_with_context};
33
use clippy_utils::ty::is_type_diagnostic_item;
44
use clippy_utils::{iter_input_pats, method_chain_args};
55
use if_chain::if_chain;
@@ -217,36 +217,33 @@ fn lint_map_unit_fn(cx: &LateContext<'_>, stmt: &hir::Stmt<'_>, expr: &hir::Expr
217217
let fn_arg = &map_args[1];
218218

219219
if is_unit_function(cx, fn_arg) {
220+
let mut applicability = Applicability::MachineApplicable;
220221
let msg = suggestion_msg("function", map_type);
221222
let suggestion = format!(
222223
"if let {0}({binding}) = {1} {{ {2}({binding}) }}",
223224
variant,
224-
snippet(cx, var_arg.span, "_"),
225-
snippet(cx, fn_arg.span, "_"),
225+
snippet_with_applicability(cx, var_arg.span, "_", &mut applicability),
226+
snippet_with_applicability(cx, fn_arg.span, "_", &mut applicability),
226227
binding = let_binding_name(cx, var_arg)
227228
);
228229

229230
span_lint_and_then(cx, lint, expr.span, &msg, |diag| {
230-
diag.span_suggestion(stmt.span, "try this", suggestion, Applicability::MachineApplicable);
231+
diag.span_suggestion(stmt.span, "try this", suggestion, applicability);
231232
});
232233
} else if let Some((binding, closure_expr)) = unit_closure(cx, fn_arg) {
233234
let msg = suggestion_msg("closure", map_type);
234235

235236
span_lint_and_then(cx, lint, expr.span, &msg, |diag| {
236237
if let Some(reduced_expr_span) = reduce_unit_expression(cx, closure_expr) {
238+
let mut applicability = Applicability::MachineApplicable;
237239
let suggestion = format!(
238240
"if let {0}({1}) = {2} {{ {3} }}",
239241
variant,
240-
snippet(cx, binding.pat.span, "_"),
241-
snippet(cx, var_arg.span, "_"),
242-
snippet(cx, reduced_expr_span, "_")
243-
);
244-
diag.span_suggestion(
245-
stmt.span,
246-
"try this",
247-
suggestion,
248-
Applicability::MachineApplicable, // snippet
242+
snippet_with_applicability(cx, binding.pat.span, "_", &mut applicability),
243+
snippet_with_applicability(cx, var_arg.span, "_", &mut applicability),
244+
snippet_with_context(cx, reduced_expr_span, var_arg.span.ctxt(), "_", &mut applicability).0,
249245
);
246+
diag.span_suggestion(stmt.span, "try this", suggestion, applicability);
250247
} else {
251248
let suggestion = format!(
252249
"if let {0}({1}) = {2} {{ ... }}",

tests/ui/option_map_unit_fn_fixable.fixed

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ fn option_map_unit_fn() {
8080

8181
if let Some(ref value) = x.field { do_nothing(value + captured) }
8282

83-
if let Some(a) = option() { do_nothing(a) }}
83+
if let Some(a) = option() { do_nothing(a) }
84+
85+
if let Some(value) = option() { println!("{:?}", value) }
86+
}
8487

8588
fn main() {}

tests/ui/option_map_unit_fn_fixable.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ fn option_map_unit_fn() {
8080

8181
x.field.map(|ref value| { do_nothing(value + captured) });
8282

83-
option().map(do_nothing);}
83+
option().map(do_nothing);
84+
85+
option().map(|value| println!("{:?}", value));
86+
}
8487

8588
fn main() {}

tests/ui/option_map_unit_fn_fixable.stderr

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,18 @@ LL | x.field.map(|ref value| { do_nothing(value + captured) });
139139
error: called `map(f)` on an `Option` value where `f` is a function that returns the unit type `()`
140140
--> $DIR/option_map_unit_fn_fixable.rs:83:5
141141
|
142-
LL | option().map(do_nothing);}
142+
LL | option().map(do_nothing);
143143
| ^^^^^^^^^^^^^^^^^^^^^^^^-
144144
| |
145145
| help: try this: `if let Some(a) = option() { do_nothing(a) }`
146146

147-
error: aborting due to 18 previous errors
147+
error: called `map(f)` on an `Option` value where `f` is a closure that returns the unit type `()`
148+
--> $DIR/option_map_unit_fn_fixable.rs:85:5
149+
|
150+
LL | option().map(|value| println!("{:?}", value));
151+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
152+
| |
153+
| help: try this: `if let Some(value) = option() { println!("{:?}", value) }`
154+
155+
error: aborting due to 19 previous errors
148156

tests/ui/result_map_unit_fn_fixable.fixed

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ fn result_map_unit_fn() {
7575

7676

7777
if let Ok(ref value) = x.field { do_nothing(value + captured) }
78+
79+
if let Ok(value) = x.field { println!("{:?}", value) }
7880
}
7981

8082
fn main() {}

tests/ui/result_map_unit_fn_fixable.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ fn result_map_unit_fn() {
7575

7676

7777
x.field.map(|ref value| { do_nothing(value + captured) });
78+
79+
x.field.map(|value| println!("{:?}", value));
7880
}
7981

8082
fn main() {}

tests/ui/result_map_unit_fn_fixable.stderr

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,5 +136,13 @@ LL | x.field.map(|ref value| { do_nothing(value + captured) });
136136
| |
137137
| help: try this: `if let Ok(ref value) = x.field { do_nothing(value + captured) }`
138138

139-
error: aborting due to 17 previous errors
139+
error: called `map(f)` on an `Result` value where `f` is a closure that returns the unit type `()`
140+
--> $DIR/result_map_unit_fn_fixable.rs:79:5
141+
|
142+
LL | x.field.map(|value| println!("{:?}", value));
143+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-
144+
| |
145+
| help: try this: `if let Ok(value) = x.field { println!("{:?}", value) }`
146+
147+
error: aborting due to 18 previous errors
140148

0 commit comments

Comments
 (0)