|
1 | | -use clippy_utils::diagnostics::span_lint_and_sugg; |
| 1 | +use clippy_utils::diagnostics::span_lint_and_then; |
2 | 2 | use clippy_utils::source::snippet_with_applicability; |
3 | 3 | use clippy_utils::ty::is_type_diagnostic_item; |
4 | 4 | use clippy_utils::{get_parent_expr, is_res_lang_ctor, path_res}; |
@@ -48,29 +48,28 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, scrutine |
48 | 48 | return; |
49 | 49 | }; |
50 | 50 |
|
51 | | - let expr_err_ty = cx.typeck_results().expr_ty(err_arg); |
52 | | - let span = hygiene::walk_chain(err_arg.span, try_arg.span.ctxt()); |
53 | | - let mut applicability = Applicability::MachineApplicable; |
54 | | - let origin_snippet = snippet_with_applicability(cx, span, "_", &mut applicability); |
55 | | - let ret_prefix = if get_parent_expr(cx, expr).map_or(false, |e| matches!(e.kind, ExprKind::Ret(_))) { |
56 | | - "" // already returns |
57 | | - } else { |
58 | | - "return " |
59 | | - }; |
60 | | - let suggestion = if err_ty == expr_err_ty { |
61 | | - format!("{ret_prefix}{prefix}{origin_snippet}{suffix}") |
62 | | - } else { |
63 | | - format!("{ret_prefix}{prefix}{origin_snippet}.into(){suffix}") |
64 | | - }; |
65 | | - |
66 | | - span_lint_and_sugg( |
| 51 | + span_lint_and_then( |
67 | 52 | cx, |
68 | 53 | TRY_ERR, |
69 | 54 | expr.span, |
70 | 55 | "returning an `Err(_)` with the `?` operator", |
71 | | - "try", |
72 | | - suggestion, |
73 | | - applicability, |
| 56 | + |diag| { |
| 57 | + let expr_err_ty = cx.typeck_results().expr_ty(err_arg); |
| 58 | + let span = hygiene::walk_chain(err_arg.span, try_arg.span.ctxt()); |
| 59 | + let mut applicability = Applicability::MachineApplicable; |
| 60 | + let origin_snippet = snippet_with_applicability(cx, span, "_", &mut applicability); |
| 61 | + let ret_prefix = if get_parent_expr(cx, expr).map_or(false, |e| matches!(e.kind, ExprKind::Ret(_))) { |
| 62 | + "" // already returns |
| 63 | + } else { |
| 64 | + "return " |
| 65 | + }; |
| 66 | + let suggestion = if err_ty == expr_err_ty { |
| 67 | + format!("{ret_prefix}{prefix}{origin_snippet}{suffix}") |
| 68 | + } else { |
| 69 | + format!("{ret_prefix}{prefix}{origin_snippet}.into(){suffix}") |
| 70 | + }; |
| 71 | + diag.span_suggestion(expr.span, "try", suggestion, applicability); |
| 72 | + }, |
74 | 73 | ); |
75 | 74 | } |
76 | 75 | } |
|
0 commit comments