@@ -7,11 +7,10 @@ use rustc_ast::ast::LitKind;
77use rustc_errors:: Applicability ;
88use rustc_hir:: def:: Res ;
99use rustc_hir:: def_id:: { DefId , DefIdSet } ;
10- use rustc_hir:: lang_items:: LangItem ;
1110use rustc_hir:: {
1211 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 ,
1514} ;
1615use rustc_lint:: { LateContext , LateLintPass } ;
1716use rustc_middle:: ty:: { self , AssocKind , FnSig , Ty } ;
@@ -168,6 +167,31 @@ impl<'tcx> LateLintPass<'tcx> for LenZero {
168167 return ;
169168 }
170169
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+
171195 if let ExprKind :: Binary ( Spanned { node : cmp, .. } , left, right) = expr. kind {
172196 // expr.span might contains parenthesis, see issue #10529
173197 let actual_span = left. span . with_hi ( right. span . hi ( ) ) ;
0 commit comments