|
1 | 1 | use clippy_utils::diagnostics::span_lint_and_sugg; |
2 | 2 | use clippy_utils::source::{snippet, snippet_with_macro_callsite}; |
3 | 3 | use clippy_utils::ty::is_type_diagnostic_item; |
4 | | -use clippy_utils::{differing_macro_contexts, in_macro, is_lang_ctor, match_def_path, paths}; |
| 4 | +use clippy_utils::{differing_macro_contexts, get_parent_expr, in_macro, is_lang_ctor, match_def_path, paths}; |
5 | 5 | use if_chain::if_chain; |
6 | 6 | use rustc_errors::Applicability; |
7 | 7 | use rustc_hir::LangItem::ResultErr; |
@@ -102,10 +102,15 @@ impl<'tcx> LateLintPass<'tcx> for TryErr { |
102 | 102 | } else { |
103 | 103 | snippet(cx, err_arg.span, "_") |
104 | 104 | }; |
| 105 | + let ret_prefix = if get_parent_expr(cx, expr).map_or(false, |e| matches!(e.kind, ExprKind::Ret(_))) { |
| 106 | + "" // already returns |
| 107 | + } else { |
| 108 | + "return " |
| 109 | + }; |
105 | 110 | let suggestion = if err_ty == expr_err_ty { |
106 | | - format!("return {}{}{}", prefix, origin_snippet, suffix) |
| 111 | + format!("{}{}{}{}", ret_prefix, prefix, origin_snippet, suffix) |
107 | 112 | } else { |
108 | | - format!("return {}{}.into(){}", prefix, origin_snippet, suffix) |
| 113 | + format!("{}{}{}.into(){}", ret_prefix, prefix, origin_snippet, suffix) |
109 | 114 | }; |
110 | 115 |
|
111 | 116 | span_lint_and_sugg( |
|
0 commit comments