@@ -9,6 +9,7 @@ use rustc_hir::{Expr, ExprKind, HirId, HirIdSet, LetStmt, MatchSource, Node, Pat
9
9
use rustc_lint:: { LateContext , LintContext } ;
10
10
use rustc_middle:: lint:: { in_external_macro, is_from_async_await} ;
11
11
use rustc_middle:: ty;
12
+ use std:: cmp:: Ordering ;
12
13
13
14
use super :: LET_UNIT_VALUE ;
14
15
@@ -82,7 +83,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, local: &'tcx LetStmt<'_>) {
82
83
83
84
// If this is a binding pattern, we need to add suggestions to remove any usages
84
85
// 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
86
87
&& let Some ( body_id) = cx. enclosing_body . as_ref ( )
87
88
{
88
89
let body = cx. tcx . hir ( ) . body ( * body_id) ;
@@ -96,19 +97,25 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, local: &'tcx LetStmt<'_>) {
96
97
}
97
98
98
99
// 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
+ } ;
112
119
} ,
113
120
) ;
114
121
}
0 commit comments