|
1 |
| -use crate::{ |
2 |
| - map_unit_fn::OPTION_MAP_UNIT_FN, |
3 |
| - matches::MATCH_AS_REF, |
4 |
| - utils::{is_allowed, match_def_path, match_var, paths, peel_hir_expr_refs}, |
5 |
| -}; |
| 1 | +use crate::{map_unit_fn::OPTION_MAP_UNIT_FN, matches::MATCH_AS_REF}; |
6 | 2 | use clippy_utils::diagnostics::span_lint_and_sugg;
|
7 | 3 | use clippy_utils::source::{snippet_with_applicability, snippet_with_context};
|
8 | 4 | use clippy_utils::ty::{can_partially_move_ty, is_type_diagnostic_item, peel_mid_ty_refs_is_mutable};
|
| 5 | +use clippy_utils::{is_allowed, is_else_clause_of_if_let_else, match_def_path, match_var, paths, peel_hir_expr_refs}; |
9 | 6 | use rustc_ast::util::parser::PREC_POSTFIX;
|
10 | 7 | use rustc_errors::Applicability;
|
11 | 8 | use rustc_hir::{
|
12 | 9 | def::Res,
|
13 | 10 | intravisit::{walk_expr, ErasedMap, NestedVisitorMap, Visitor},
|
14 |
| - Arm, BindingAnnotation, Block, Expr, ExprKind, Mutability, Pat, PatKind, Path, QPath, |
| 11 | + Arm, BindingAnnotation, Block, Expr, ExprKind, MatchSource, Mutability, Pat, PatKind, Path, QPath, |
15 | 12 | };
|
16 | 13 | use rustc_lint::{LateContext, LateLintPass, LintContext};
|
17 | 14 | use rustc_middle::lint::in_external_macro;
|
@@ -54,8 +51,11 @@ impl LateLintPass<'_> for ManualMap {
|
54 | 51 | return;
|
55 | 52 | }
|
56 | 53 |
|
57 |
| - if let ExprKind::Match(scrutinee, [arm1 @ Arm { guard: None, .. }, arm2 @ Arm { guard: None, .. }], _) = |
58 |
| - expr.kind |
| 54 | + if let ExprKind::Match( |
| 55 | + scrutinee, |
| 56 | + [arm1 @ Arm { guard: None, .. }, arm2 @ Arm { guard: None, .. }], |
| 57 | + match_kind, |
| 58 | + ) = expr.kind |
59 | 59 | {
|
60 | 60 | let (scrutinee_ty, ty_ref_count, ty_mutability) =
|
61 | 61 | peel_mid_ty_refs_is_mutable(cx.typeck_results().expr_ty(scrutinee));
|
@@ -181,7 +181,12 @@ impl LateLintPass<'_> for ManualMap {
|
181 | 181 | expr.span,
|
182 | 182 | "manual implementation of `Option::map`",
|
183 | 183 | "try this",
|
184 |
| - format!("{}{}.map({})", scrutinee_str, as_ref_str, body_str), |
| 184 | + if matches!(match_kind, MatchSource::IfLetDesugar { .. }) && is_else_clause_of_if_let_else(cx.tcx, expr) |
| 185 | + { |
| 186 | + format!("{{ {}{}.map({}) }}", scrutinee_str, as_ref_str, body_str) |
| 187 | + } else { |
| 188 | + format!("{}{}.map({})", scrutinee_str, as_ref_str, body_str) |
| 189 | + }, |
185 | 190 | app,
|
186 | 191 | );
|
187 | 192 | }
|
|
0 commit comments