11use clippy_utils:: diagnostics:: span_lint_and_sugg;
2- use clippy_utils:: source:: { expr_block, snippet, SpanRangeExt } ;
2+ use clippy_utils:: source:: { expr_block, snippet, snippet_block_with_context , SpanRangeExt } ;
33use clippy_utils:: ty:: implements_trait;
44use clippy_utils:: {
55 is_lint_allowed, is_unit_expr, peel_blocks, peel_hir_pat_refs, peel_middle_ty_refs, peel_n_hir_expr_refs,
@@ -9,7 +9,7 @@ use rustc_arena::DroplessArena;
99use rustc_errors:: Applicability ;
1010use rustc_hir:: def:: { DefKind , Res } ;
1111use rustc_hir:: intravisit:: { walk_pat, Visitor } ;
12- use rustc_hir:: { Arm , Expr , ExprKind , HirId , Pat , PatKind , QPath } ;
12+ use rustc_hir:: { Arm , Expr , ExprKind , HirId , Node , Pat , PatKind , QPath , StmtKind } ;
1313use rustc_lint:: LateContext ;
1414use rustc_middle:: ty:: { self , AdtDef , ParamEnv , TyCtxt , TypeckResults , VariantDef } ;
1515use rustc_span:: { sym, Span } ;
@@ -93,8 +93,24 @@ fn report_single_pattern(cx: &LateContext<'_>, ex: &Expr<'_>, arm: &Arm<'_>, exp
9393
9494 if snippet ( cx, ex. span , ".." ) == snippet ( cx, arm. pat . span , ".." ) {
9595 let msg = "this pattern is irrefutable, `match` is useless" ;
96- let sugg = expr_block ( cx, arm. body , ctxt, ".." , Some ( expr. span ) , & mut app) ;
97- span_lint_and_sugg ( cx, lint, expr. span , msg, "try" , sugg, app) ;
96+ let ( sugg, help) = if is_unit_expr ( arm. body ) {
97+ ( String :: new ( ) , "`match` expression can be removed" )
98+ } else {
99+ let mut sugg = snippet_block_with_context ( cx, arm. body . span , ctxt, ".." , Some ( expr. span ) , & mut app)
100+ . 0
101+ . to_string ( ) ;
102+ if let Node :: Stmt ( stmt) = cx. tcx . parent_hir_node ( expr. hir_id )
103+ && let StmtKind :: Expr ( _) = stmt. kind
104+ && match arm. body . kind {
105+ ExprKind :: Block ( block, _) => block. span . from_expansion ( ) ,
106+ _ => true ,
107+ }
108+ {
109+ sugg. push ( ';' ) ;
110+ }
111+ ( sugg, "try" )
112+ } ;
113+ span_lint_and_sugg ( cx, lint, expr. span , msg, help, sugg. to_string ( ) , app) ;
98114 return ;
99115 }
100116
0 commit comments