1
1
use 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 } ;
3
3
use clippy_utils:: ty:: implements_trait;
4
4
use clippy_utils:: {
5
5
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;
9
9
use rustc_errors:: Applicability ;
10
10
use rustc_hir:: def:: { DefKind , Res } ;
11
11
use 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 } ;
13
13
use rustc_lint:: LateContext ;
14
14
use rustc_middle:: ty:: { self , AdtDef , ParamEnv , TyCtxt , TypeckResults , VariantDef } ;
15
15
use rustc_span:: { sym, Span } ;
@@ -91,6 +91,29 @@ fn report_single_pattern(cx: &LateContext<'_>, ex: &Expr<'_>, arm: &Arm<'_>, exp
91
91
format ! ( " else {}" , expr_block( cx, els, ctxt, ".." , Some ( expr. span) , & mut app) )
92
92
} ) ;
93
93
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
+
94
117
let ( pat, pat_ref_count) = peel_hir_pat_refs ( arm. pat ) ;
95
118
let ( msg, sugg) = if let PatKind :: Path ( _) | PatKind :: Lit ( _) = pat. kind
96
119
&& let ( ty, ty_ref_count) = peel_middle_ty_refs ( cx. typeck_results ( ) . expr_ty ( ex) )
0 commit comments