@@ -2,13 +2,13 @@ use crate::{map_unit_fn::OPTION_MAP_UNIT_FN, matches::MATCH_AS_REF};
22use clippy_utils:: diagnostics:: span_lint_and_sugg;
33use clippy_utils:: source:: { snippet_with_applicability, snippet_with_context} ;
44use clippy_utils:: ty:: { can_partially_move_ty, is_type_diagnostic_item, peel_mid_ty_refs_is_mutable} ;
5- use clippy_utils:: { is_allowed, match_def_path, match_var, paths, peel_hir_expr_refs} ;
5+ use clippy_utils:: { is_allowed, is_else_clause_of_if_let_else , match_def_path, match_var, paths, peel_hir_expr_refs} ;
66use rustc_ast:: util:: parser:: PREC_POSTFIX ;
77use rustc_errors:: Applicability ;
88use rustc_hir:: {
99 def:: Res ,
1010 intravisit:: { walk_expr, ErasedMap , NestedVisitorMap , Visitor } ,
11- Arm , BindingAnnotation , Block , Expr , ExprKind , Mutability , Pat , PatKind , Path , QPath ,
11+ Arm , BindingAnnotation , Block , Expr , ExprKind , MatchSource , Mutability , Pat , PatKind , Path , QPath ,
1212} ;
1313use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
1414use rustc_middle:: lint:: in_external_macro;
@@ -51,8 +51,11 @@ impl LateLintPass<'_> for ManualMap {
5151 return ;
5252 }
5353
54- if let ExprKind :: Match ( scrutinee, [ arm1 @ Arm { guard : None , .. } , arm2 @ Arm { guard : None , .. } ] , _) =
55- expr. kind
54+ if let ExprKind :: Match (
55+ scrutinee,
56+ [ arm1 @ Arm { guard : None , .. } , arm2 @ Arm { guard : None , .. } ] ,
57+ match_kind,
58+ ) = expr. kind
5659 {
5760 let ( scrutinee_ty, ty_ref_count, ty_mutability) =
5861 peel_mid_ty_refs_is_mutable ( cx. typeck_results ( ) . expr_ty ( scrutinee) ) ;
@@ -178,7 +181,12 @@ impl LateLintPass<'_> for ManualMap {
178181 expr. span ,
179182 "manual implementation of `Option::map`" ,
180183 "try this" ,
181- 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+ } ,
182190 app,
183191 ) ;
184192 }
0 commit comments