@@ -7,11 +7,10 @@ use rustc_ast::ast::LitKind;
7
7
use rustc_errors:: Applicability ;
8
8
use rustc_hir:: def:: Res ;
9
9
use rustc_hir:: def_id:: { DefId , DefIdSet } ;
10
- use rustc_hir:: lang_items:: LangItem ;
11
10
use rustc_hir:: {
12
11
AssocItemKind , BinOpKind , Expr , ExprKind , FnRetTy , GenericArg , GenericBound , ImplItem , ImplItemKind ,
13
- ImplicitSelfKind , Item , ItemKind , Mutability , Node , PathSegment , PrimTy , QPath , TraitItemRef , TyKind ,
14
- TypeBindingKind ,
12
+ ImplicitSelfKind , Item , ItemKind , LangItem , Mutability , Node , PatKind , PathSegment , PrimTy , QPath , TraitItemRef ,
13
+ TyKind , TypeBindingKind ,
15
14
} ;
16
15
use rustc_lint:: { LateContext , LateLintPass } ;
17
16
use rustc_middle:: ty:: { self , AssocKind , FnSig , Ty } ;
@@ -168,6 +167,31 @@ impl<'tcx> LateLintPass<'tcx> for LenZero {
168
167
return ;
169
168
}
170
169
170
+ if let ExprKind :: Let ( lt) = expr. kind
171
+ && has_is_empty ( cx, lt. init )
172
+ && match lt. pat . kind {
173
+ PatKind :: Slice ( [ ] , _, [ ] ) => true ,
174
+ PatKind :: Lit ( lit) if is_empty_string ( lit) => true ,
175
+ _ => false ,
176
+ }
177
+ {
178
+ let mut applicability = Applicability :: MachineApplicable ;
179
+
180
+ let lit1 = peel_ref_operators ( cx, lt. init ) ;
181
+ let lit_str =
182
+ Sugg :: hir_with_context ( cx, lit1, lt. span . ctxt ( ) , "_" , & mut applicability) . maybe_par ( ) ;
183
+
184
+ span_lint_and_sugg (
185
+ cx,
186
+ COMPARISON_TO_EMPTY ,
187
+ lt. span ,
188
+ "comparison to empty slice using `if let`" ,
189
+ "using `is_empty` is clearer and more explicit" ,
190
+ format ! ( "{lit_str}.is_empty()" ) ,
191
+ applicability,
192
+ ) ;
193
+ }
194
+
171
195
if let ExprKind :: Binary ( Spanned { node : cmp, .. } , left, right) = expr. kind {
172
196
// expr.span might contains parenthesis, see issue #10529
173
197
let actual_span = left. span . with_hi ( right. span . hi ( ) ) ;
0 commit comments