@@ -285,7 +285,8 @@ declare_clippy_lint! {
285
285
///
286
286
/// **Why is this bad?** Readability and needless complexity.
287
287
///
288
- /// **Known problems:** None.
288
+ /// **Known problems:** Suggested replacements may be incorrect when `match`
289
+ /// is actually binding temporary value, bringing a 'dropped while borrowed' error.
289
290
///
290
291
/// **Example:**
291
292
/// ```rust
@@ -835,23 +836,22 @@ fn check_match_single_binding(cx: &LateContext<'_, '_>, ex: &Expr<'_>, arms: &[A
835
836
} ;
836
837
837
838
// Do we need to add ';' to suggestion ?
838
- if_chain ! {
839
- if let ExprKind :: Block ( block, _) = & arms[ 0 ] . body. kind;
840
- if block. stmts. len( ) == 1 ;
841
- if let StmtKind :: Semi ( s) = block. stmts. get( 0 ) . unwrap( ) . kind;
842
- then {
843
- match s. kind {
844
- ExprKind :: Block ( _, _) => ( ) ,
845
- _ => {
846
- // expr_ty(body) == ()
847
- if cx. tables. expr_ty( & arms[ 0 ] . body) . is_unit( ) {
848
- snippet_body. push( ';' ) ;
849
- }
850
- }
839
+ match match_body. kind {
840
+ ExprKind :: Block ( block, _) => {
841
+ // macro + expr_ty(body) == ()
842
+ if block. span . from_expansion ( ) && cx. tables . expr_ty ( & match_body) . is_unit ( ) {
843
+ snippet_body. push ( ';' ) ;
851
844
}
852
- }
845
+ } ,
846
+ _ => {
847
+ // expr_ty(body) == ()
848
+ if cx. tables . expr_ty ( & match_body) . is_unit ( ) {
849
+ snippet_body. push ( ';' ) ;
850
+ }
851
+ } ,
853
852
}
854
853
854
+ let mut applicability = Applicability :: MaybeIncorrect ;
855
855
match arms[ 0 ] . pat . kind {
856
856
PatKind :: Binding ( ..) | PatKind :: Tuple ( _, _) | PatKind :: Struct ( ..) => {
857
857
span_lint_and_sugg (
@@ -862,11 +862,11 @@ fn check_match_single_binding(cx: &LateContext<'_, '_>, ex: &Expr<'_>, arms: &[A
862
862
"consider using `let` statement" ,
863
863
format ! (
864
864
"let {} = {};\n {}" ,
865
- snippet ( cx, bind_names, ".." ) ,
866
- snippet ( cx, matched_vars, ".." ) ,
865
+ snippet_with_applicability ( cx, bind_names, ".." , & mut applicability ) ,
866
+ snippet_with_applicability ( cx, matched_vars, ".." , & mut applicability ) ,
867
867
snippet_body
868
868
) ,
869
- Applicability :: MachineApplicable ,
869
+ applicability ,
870
870
) ;
871
871
} ,
872
872
PatKind :: Wild => {
0 commit comments