@@ -2,13 +2,13 @@ use crate::{map_unit_fn::OPTION_MAP_UNIT_FN, matches::MATCH_AS_REF};
2
2
use clippy_utils:: diagnostics:: span_lint_and_sugg;
3
3
use clippy_utils:: source:: { snippet_with_applicability, snippet_with_context} ;
4
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, 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} ;
6
6
use rustc_ast:: util:: parser:: PREC_POSTFIX ;
7
7
use rustc_errors:: Applicability ;
8
8
use rustc_hir:: {
9
9
def:: Res ,
10
10
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 ,
12
12
} ;
13
13
use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
14
14
use rustc_middle:: lint:: in_external_macro;
@@ -51,8 +51,11 @@ impl LateLintPass<'_> for ManualMap {
51
51
return ;
52
52
}
53
53
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
56
59
{
57
60
let ( scrutinee_ty, ty_ref_count, ty_mutability) =
58
61
peel_mid_ty_refs_is_mutable ( cx. typeck_results ( ) . expr_ty ( scrutinee) ) ;
@@ -178,7 +181,12 @@ impl LateLintPass<'_> for ManualMap {
178
181
expr. span ,
179
182
"manual implementation of `Option::map`" ,
180
183
"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
+ } ,
182
190
app,
183
191
) ;
184
192
}
0 commit comments