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 } ;
@@ -91,6 +91,29 @@ fn report_single_pattern(cx: &LateContext<'_>, ex: &Expr<'_>, arm: &Arm<'_>, exp
9191 format ! ( " else {}" , expr_block( cx, els, ctxt, ".." , Some ( expr. span) , & mut app) )
9292 } ) ;
9393
94+ if snippet ( cx, ex. span , ".." ) == snippet ( cx, arm. pat . span , ".." ) {
95+ let msg = "this pattern is irrefutable, `match` is useless" ;
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) ;
114+ return ;
115+ }
116+
94117 let ( pat, pat_ref_count) = peel_hir_pat_refs ( arm. pat ) ;
95118 let ( msg, sugg) = if let PatKind :: Path ( _) | PatKind :: Lit ( _) = pat. kind
96119 && let ( ty, ty_ref_count) = peel_middle_ty_refs ( cx. typeck_results ( ) . expr_ty ( ex) )
0 commit comments