@@ -9,6 +9,7 @@ use rustc_hir::{Expr, ExprKind, HirId, HirIdSet, LetStmt, MatchSource, Node, Pat
99use  rustc_lint:: { LateContext ,  LintContext } ; 
1010use  rustc_middle:: lint:: { in_external_macro,  is_from_async_await} ; 
1111use  rustc_middle:: ty; 
12+ use  std:: cmp:: Ordering ; 
1213
1314use  super :: LET_UNIT_VALUE ; 
1415
@@ -82,7 +83,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, local: &'tcx LetStmt<'_>) {
8283
8384                    // If this is a binding pattern, we need to add suggestions to remove any usages 
8485                    // of the variable 
85-                     if  let  PatKind :: Binding ( _,  binding_hir_id,  _ ,   ..)  = local. pat . kind 
86+                     if  let  PatKind :: Binding ( _,  binding_hir_id,  ..)  = local. pat . kind 
8687                        && let  Some ( body_id)  = cx. enclosing_body . as_ref ( ) 
8788                    { 
8889                        let  body = cx. tcx . hir ( ) . body ( * body_id) ; 
@@ -96,19 +97,25 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, local: &'tcx LetStmt<'_>) {
9697                    } 
9798
9899                    // Emit appropriate diagnostic based on whether there are usages of the let binding 
99-                     if  suggestions. len ( )  == 1  { 
100-                         diag. multipart_suggestion ( 
101-                             "omit the `let` binding" , 
102-                             suggestions, 
103-                             Applicability :: MachineApplicable , 
104-                         ) ; 
105-                     }  else  if  suggestions. len ( )  > 1  { 
106-                         diag. multipart_suggestion ( 
107-                             "omit the `let` binding and replace variable usages with `()`" , 
108-                             suggestions, 
109-                             Applicability :: MachineApplicable , 
110-                         ) ; 
111-                     } 
100+                     match  suggestions. len ( ) . cmp ( & 1 )  { 
101+                         Ordering :: Equal  => { 
102+                             diag. multipart_suggestion ( 
103+                                 "omit the `let` binding" , 
104+                                 suggestions, 
105+                                 Applicability :: MachineApplicable , 
106+                             ) ; 
107+                             ( ) 
108+                         } , 
109+                         Ordering :: Greater  => { 
110+                             diag. multipart_suggestion ( 
111+                                 "omit the `let` binding and replace variable usages with `()`" , 
112+                                 suggestions, 
113+                                 Applicability :: MachineApplicable , 
114+                             ) ; 
115+                             ( ) 
116+                         } , 
117+                         Ordering :: Less  => ( ) , 
118+                     } ; 
112119                } , 
113120            ) ; 
114121        } 
0 commit comments