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 } ;
@@ -93,8 +93,24 @@ fn report_single_pattern(cx: &LateContext<'_>, ex: &Expr<'_>, arm: &Arm<'_>, exp
93
93
94
94
if snippet ( cx, ex. span , ".." ) == snippet ( cx, arm. pat . span , ".." ) {
95
95
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) ;
98
114
return ;
99
115
}
100
116
0 commit comments