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