|
1 | 1 | 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}; |
3 | 3 | use clippy_utils::ty::is_type_diagnostic_item; |
4 | 4 | use clippy_utils::{iter_input_pats, method_chain_args}; |
5 | 5 | use if_chain::if_chain; |
@@ -217,36 +217,33 @@ fn lint_map_unit_fn(cx: &LateContext<'_>, stmt: &hir::Stmt<'_>, expr: &hir::Expr |
217 | 217 | let fn_arg = &map_args[1]; |
218 | 218 |
|
219 | 219 | if is_unit_function(cx, fn_arg) { |
| 220 | + let mut applicability = Applicability::MachineApplicable; |
220 | 221 | let msg = suggestion_msg("function", map_type); |
221 | 222 | let suggestion = format!( |
222 | 223 | "if let {0}({binding}) = {1} {{ {2}({binding}) }}", |
223 | 224 | 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), |
226 | 227 | binding = let_binding_name(cx, var_arg) |
227 | 228 | ); |
228 | 229 |
|
229 | 230 | 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); |
231 | 232 | }); |
232 | 233 | } else if let Some((binding, closure_expr)) = unit_closure(cx, fn_arg) { |
233 | 234 | let msg = suggestion_msg("closure", map_type); |
234 | 235 |
|
235 | 236 | span_lint_and_then(cx, lint, expr.span, &msg, |diag| { |
236 | 237 | if let Some(reduced_expr_span) = reduce_unit_expression(cx, closure_expr) { |
| 238 | + let mut applicability = Applicability::MachineApplicable; |
237 | 239 | let suggestion = format!( |
238 | 240 | "if let {0}({1}) = {2} {{ {3} }}", |
239 | 241 | 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, |
249 | 245 | ); |
| 246 | + diag.span_suggestion(stmt.span, "try this", suggestion, applicability); |
250 | 247 | } else { |
251 | 248 | let suggestion = format!( |
252 | 249 | "if let {0}({1}) = {2} {{ ... }}", |
|
0 commit comments