@@ -6,8 +6,9 @@ use rustc_ast::ast::LitKind;
6
6
use rustc_errors:: Applicability ;
7
7
use rustc_hir:: def_id:: DefIdSet ;
8
8
use rustc_hir:: {
9
- def_id:: DefId , AssocItemKind , BinOpKind , Expr , ExprKind , FnRetTy , ImplItem , ImplItemKind , ImplicitSelfKind , Item ,
10
- ItemKind , Mutability , Node , TraitItemRef , TyKind , UnOp ,
9
+ def:: Res , def_id:: DefId , lang_items:: LangItem , AssocItemKind , BinOpKind , Expr , ExprKind , FnRetTy , GenericArg ,
10
+ GenericBound , ImplItem , ImplItemKind , ImplicitSelfKind , Item , ItemKind , Mutability , Node , PathSegment , PrimTy ,
11
+ QPath , TraitItemRef , TyKind , TypeBindingKind , UnOp ,
11
12
} ;
12
13
use rustc_lint:: { LateContext , LateLintPass } ;
13
14
use rustc_middle:: ty:: { self , AssocKind , FnSig , Ty } ;
@@ -251,33 +252,98 @@ fn check_trait_items(cx: &LateContext<'_>, visited_trait: &Item<'_>, trait_items
251
252
}
252
253
253
254
#[ derive( Debug , Clone , Copy ) ]
254
- enum LenOutput < ' tcx > {
255
+ enum LenOutput {
255
256
Integral ,
256
257
Option ( DefId ) ,
257
- Result ( DefId , Ty < ' tcx > ) ,
258
+ Result ( DefId ) ,
258
259
}
259
- fn parse_len_output < ' tcx > ( cx : & LateContext < ' _ > , sig : FnSig < ' tcx > ) -> Option < LenOutput < ' tcx > > {
260
+
261
+ fn extract_future_output < ' tcx > ( cx : & LateContext < ' tcx > , ty : Ty < ' tcx > ) -> Option < & ' tcx PathSegment < ' tcx > > {
262
+ if let ty:: Alias ( _, alias_ty) = ty. kind ( ) &&
263
+ let Some ( Node :: Item ( item) ) = cx. tcx . hir ( ) . get_if_local ( alias_ty. def_id ) &&
264
+ let Item { kind : ItemKind :: OpaqueTy ( opaque) , .. } = item &&
265
+ opaque. bounds . len ( ) == 1 &&
266
+ let GenericBound :: LangItemTrait ( LangItem :: Future , _, _, generic_args) = & opaque. bounds [ 0 ] &&
267
+ generic_args. bindings . len ( ) == 1 &&
268
+ let TypeBindingKind :: Equality {
269
+ term : rustc_hir:: Term :: Ty ( rustc_hir:: Ty { kind : TyKind :: Path ( QPath :: Resolved ( _, path) ) , .. } ) ,
270
+ } = & generic_args. bindings [ 0 ] . kind &&
271
+ path. segments . len ( ) == 1 {
272
+ return Some ( & path. segments [ 0 ] ) ;
273
+ }
274
+
275
+ None
276
+ }
277
+
278
+ fn is_first_generic_integral < ' tcx > ( segment : & ' tcx PathSegment < ' tcx > ) -> bool {
279
+ if let Some ( generic_args) = segment. args {
280
+ if generic_args. args . is_empty ( ) {
281
+ return false ;
282
+ }
283
+ let arg = & generic_args. args [ 0 ] ;
284
+ if let GenericArg :: Type ( rustc_hir:: Ty {
285
+ kind : TyKind :: Path ( QPath :: Resolved ( _, path) ) ,
286
+ ..
287
+ } ) = arg
288
+ {
289
+ let segments = & path. segments ;
290
+ let segment = & segments[ 0 ] ;
291
+ let res = & segment. res ;
292
+ if matches ! ( res, Res :: PrimTy ( PrimTy :: Uint ( _) ) ) || matches ! ( res, Res :: PrimTy ( PrimTy :: Int ( _) ) ) {
293
+ return true ;
294
+ }
295
+ }
296
+ }
297
+
298
+ false
299
+ }
300
+
301
+ fn parse_len_output < ' tcx > ( cx : & LateContext < ' tcx > , sig : FnSig < ' tcx > ) -> Option < LenOutput > {
302
+ if let Some ( segment) = extract_future_output ( cx, sig. output ( ) ) {
303
+ let res = segment. res ;
304
+
305
+ if matches ! ( res, Res :: PrimTy ( PrimTy :: Uint ( _) ) ) || matches ! ( res, Res :: PrimTy ( PrimTy :: Int ( _) ) ) {
306
+ return Some ( LenOutput :: Integral ) ;
307
+ }
308
+
309
+ if let Res :: Def ( _, def_id) = res {
310
+ if cx. tcx . is_diagnostic_item ( sym:: Option , def_id) && is_first_generic_integral ( segment) {
311
+ return Some ( LenOutput :: Option ( def_id) ) ;
312
+ } else if cx. tcx . is_diagnostic_item ( sym:: Result , def_id) && is_first_generic_integral ( segment) {
313
+ return Some ( LenOutput :: Result ( def_id) ) ;
314
+ }
315
+ }
316
+
317
+ return None ;
318
+ }
319
+
260
320
match * sig. output ( ) . kind ( ) {
261
321
ty:: Int ( _) | ty:: Uint ( _) => Some ( LenOutput :: Integral ) ,
262
322
ty:: Adt ( adt, subs) if cx. tcx . is_diagnostic_item ( sym:: Option , adt. did ( ) ) => {
263
323
subs. type_at ( 0 ) . is_integral ( ) . then ( || LenOutput :: Option ( adt. did ( ) ) )
264
324
} ,
265
- ty:: Adt ( adt, subs) if cx. tcx . is_diagnostic_item ( sym:: Result , adt. did ( ) ) => subs
266
- . type_at ( 0 )
267
- . is_integral ( )
268
- . then ( || LenOutput :: Result ( adt. did ( ) , subs. type_at ( 1 ) ) ) ,
325
+ ty:: Adt ( adt, subs) if cx. tcx . is_diagnostic_item ( sym:: Result , adt. did ( ) ) => {
326
+ subs. type_at ( 0 ) . is_integral ( ) . then ( || LenOutput :: Result ( adt. did ( ) ) )
327
+ } ,
269
328
_ => None ,
270
329
}
271
330
}
272
331
273
- impl < ' tcx > LenOutput < ' tcx > {
274
- fn matches_is_empty_output ( self , ty : Ty < ' tcx > ) -> bool {
332
+ impl LenOutput {
333
+ fn matches_is_empty_output < ' tcx > ( self , cx : & LateContext < ' tcx > , ty : Ty < ' tcx > ) -> bool {
334
+ if let Some ( segment) = extract_future_output ( cx, ty) {
335
+ return match ( self , segment. res ) {
336
+ ( _, Res :: PrimTy ( PrimTy :: Bool ) ) => true ,
337
+ ( Self :: Option ( _) , Res :: Def ( _, def_id) ) if cx. tcx . is_diagnostic_item ( sym:: Option , def_id) => true ,
338
+ ( Self :: Result ( _) , Res :: Def ( _, def_id) ) if cx. tcx . is_diagnostic_item ( sym:: Result , def_id) => true ,
339
+ _ => false ,
340
+ } ;
341
+ }
342
+
275
343
match ( self , ty. kind ( ) ) {
276
344
( _, & ty:: Bool ) => true ,
277
345
( Self :: Option ( id) , & ty:: Adt ( adt, subs) ) if id == adt. did ( ) => subs. type_at ( 0 ) . is_bool ( ) ,
278
- ( Self :: Result ( id, err_ty) , & ty:: Adt ( adt, subs) ) if id == adt. did ( ) => {
279
- subs. type_at ( 0 ) . is_bool ( ) && subs. type_at ( 1 ) == err_ty
280
- } ,
346
+ ( Self :: Result ( id) , & ty:: Adt ( adt, subs) ) if id == adt. did ( ) => subs. type_at ( 0 ) . is_bool ( ) ,
281
347
_ => false ,
282
348
}
283
349
}
@@ -301,9 +367,14 @@ impl<'tcx> LenOutput<'tcx> {
301
367
}
302
368
303
369
/// Checks if the given signature matches the expectations for `is_empty`
304
- fn check_is_empty_sig < ' tcx > ( sig : FnSig < ' tcx > , self_kind : ImplicitSelfKind , len_output : LenOutput < ' tcx > ) -> bool {
370
+ fn check_is_empty_sig < ' tcx > (
371
+ cx : & LateContext < ' tcx > ,
372
+ sig : FnSig < ' tcx > ,
373
+ self_kind : ImplicitSelfKind ,
374
+ len_output : LenOutput ,
375
+ ) -> bool {
305
376
match & * * sig. inputs_and_output {
306
- [ arg, res] if len_output. matches_is_empty_output ( * res) => {
377
+ [ arg, res] if len_output. matches_is_empty_output ( cx , * res) => {
307
378
matches ! (
308
379
( arg. kind( ) , self_kind) ,
309
380
( ty:: Ref ( _, _, Mutability :: Not ) , ImplicitSelfKind :: ImmRef )
@@ -315,11 +386,11 @@ fn check_is_empty_sig<'tcx>(sig: FnSig<'tcx>, self_kind: ImplicitSelfKind, len_o
315
386
}
316
387
317
388
/// Checks if the given type has an `is_empty` method with the appropriate signature.
318
- fn check_for_is_empty < ' tcx > (
319
- cx : & LateContext < ' tcx > ,
389
+ fn check_for_is_empty (
390
+ cx : & LateContext < ' _ > ,
320
391
span : Span ,
321
392
self_kind : ImplicitSelfKind ,
322
- output : LenOutput < ' tcx > ,
393
+ output : LenOutput ,
323
394
impl_ty : DefId ,
324
395
item_name : Symbol ,
325
396
item_kind : & str ,
@@ -352,6 +423,7 @@ fn check_for_is_empty<'tcx>(
352
423
Some ( is_empty)
353
424
if !( is_empty. fn_has_self_parameter
354
425
&& check_is_empty_sig (
426
+ cx,
355
427
cx. tcx . fn_sig ( is_empty. def_id ) . subst_identity ( ) . skip_binder ( ) ,
356
428
self_kind,
357
429
output,
0 commit comments