@@ -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,133 @@ 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 def_id = alias_ty. def_id ;
264
+ let hir_map = cx. tcx . hir ( ) ;
265
+ let item = hir_map. get_if_local ( def_id) ;
266
+ let item = item. unwrap ( ) ;
267
+
268
+ if let Node :: Item ( item) = item {
269
+ if let ItemKind :: OpaqueTy ( opaque) = & item. kind {
270
+ let bounds = opaque. bounds ;
271
+ if bounds. len ( ) != 1 {
272
+ return None ;
273
+ }
274
+ let bound = & bounds[ 0 ] ;
275
+ if let GenericBound :: LangItemTrait ( item, _, _, generic_args) = bound {
276
+ if item != & LangItem :: Future {
277
+ return None ;
278
+ }
279
+
280
+ let bindings = generic_args. bindings ;
281
+ if bindings. len ( ) != 1 {
282
+ return None ;
283
+ }
284
+
285
+ let binding = & bindings[ 0 ] ;
286
+ let kind = & binding. kind ;
287
+
288
+ if let TypeBindingKind :: Equality {
289
+ term : rustc_hir:: Term :: Ty ( term_ty) ,
290
+ } = kind
291
+ {
292
+ if let TyKind :: Path ( QPath :: Resolved ( _, path) ) = & term_ty. kind {
293
+ let segments = & path. segments ;
294
+ if segments. len ( ) != 1 {
295
+ return None ;
296
+ }
297
+ let segment = & segments[ 0 ] ;
298
+ return Some ( segment) ;
299
+ }
300
+ }
301
+ }
302
+ }
303
+ }
304
+ }
305
+
306
+ None
307
+ }
308
+
309
+ fn is_first_generic_integral < ' tcx > ( segment : & ' tcx PathSegment < ' tcx > ) -> bool {
310
+ if let Some ( generic_args) = segment. args {
311
+ if generic_args. args . is_empty ( ) {
312
+ return false ;
313
+ }
314
+ let arg = & generic_args. args [ 0 ] ;
315
+ if let GenericArg :: Type ( rustc_hir:: Ty {
316
+ kind : TyKind :: Path ( QPath :: Resolved ( _, path) ) ,
317
+ ..
318
+ } ) = arg
319
+ {
320
+ let segments = & path. segments ;
321
+ let segment = & segments[ 0 ] ;
322
+ let res = & segment. res ;
323
+ if matches ! ( res, Res :: PrimTy ( PrimTy :: Uint ( _) ) ) || matches ! ( res, Res :: PrimTy ( PrimTy :: Int ( _) ) ) {
324
+ return true ;
325
+ }
326
+ }
327
+ }
328
+
329
+ false
330
+ }
331
+
332
+ fn parse_len_output < ' tcx > ( cx : & LateContext < ' tcx > , sig : FnSig < ' tcx > ) -> Option < LenOutput > {
333
+ if let Some ( segment) = extract_future_output ( cx, sig. output ( ) ) {
334
+ let res = segment. res ;
335
+
336
+ if matches ! ( res, Res :: PrimTy ( PrimTy :: Uint ( _) ) ) || matches ! ( res, Res :: PrimTy ( PrimTy :: Int ( _) ) ) {
337
+ return Some ( LenOutput :: Integral ) ;
338
+ }
339
+
340
+ if let Res :: Def ( _, def_id) = res {
341
+ if cx. tcx . is_diagnostic_item ( sym:: Option , def_id) {
342
+ if is_first_generic_integral ( segment) {
343
+ return Some ( LenOutput :: Option ( def_id) ) ;
344
+ }
345
+
346
+ return Some ( LenOutput :: Option ( def_id) ) ;
347
+ } else if cx. tcx . is_diagnostic_item ( sym:: Result , def_id) && is_first_generic_integral ( segment) {
348
+ return Some ( LenOutput :: Option ( def_id) ) ;
349
+ }
350
+ }
351
+
352
+ return None ;
353
+ }
354
+
260
355
match * sig. output ( ) . kind ( ) {
261
356
ty:: Int ( _) | ty:: Uint ( _) => Some ( LenOutput :: Integral ) ,
262
357
ty:: Adt ( adt, subs) if cx. tcx . is_diagnostic_item ( sym:: Option , adt. did ( ) ) => {
263
358
subs. type_at ( 0 ) . is_integral ( ) . then ( || LenOutput :: Option ( adt. did ( ) ) )
264
359
} ,
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 ) ) ) ,
360
+ ty:: Adt ( adt, subs) if cx. tcx . is_diagnostic_item ( sym:: Result , adt. did ( ) ) => {
361
+ subs. type_at ( 0 ) . is_integral ( ) . then ( || LenOutput :: Result ( adt. did ( ) ) )
362
+ } ,
269
363
_ => None ,
270
364
}
271
365
}
272
366
273
- impl < ' tcx > LenOutput < ' tcx > {
274
- fn matches_is_empty_output ( self , ty : Ty < ' tcx > ) -> bool {
367
+ impl LenOutput {
368
+ fn matches_is_empty_output < ' tcx > ( self , cx : & LateContext < ' tcx > , ty : Ty < ' tcx > ) -> bool {
369
+ if let Some ( segment) = extract_future_output ( cx, ty) {
370
+ return match ( self , segment. res ) {
371
+ ( _, Res :: PrimTy ( PrimTy :: Bool ) ) => true ,
372
+ ( Self :: Option ( _) , Res :: Def ( _, def_id) ) if cx. tcx . is_diagnostic_item ( sym:: Option , def_id) => true ,
373
+ ( Self :: Result ( _) , Res :: Def ( _, def_id) ) if cx. tcx . is_diagnostic_item ( sym:: Result , def_id) => true ,
374
+ _ => false ,
375
+ } ;
376
+ }
377
+
275
378
match ( self , ty. kind ( ) ) {
276
379
( _, & ty:: Bool ) => true ,
277
380
( 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
- } ,
381
+ ( Self :: Result ( id) , & ty:: Adt ( adt, subs) ) if id == adt. did ( ) => subs. type_at ( 0 ) . is_bool ( ) ,
281
382
_ => false ,
282
383
}
283
384
}
@@ -301,9 +402,14 @@ impl<'tcx> LenOutput<'tcx> {
301
402
}
302
403
303
404
/// 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 {
405
+ fn check_is_empty_sig < ' tcx > (
406
+ cx : & LateContext < ' tcx > ,
407
+ sig : FnSig < ' tcx > ,
408
+ self_kind : ImplicitSelfKind ,
409
+ len_output : LenOutput ,
410
+ ) -> bool {
305
411
match & * * sig. inputs_and_output {
306
- [ arg, res] if len_output. matches_is_empty_output ( * res) => {
412
+ [ arg, res] if len_output. matches_is_empty_output ( cx , * res) => {
307
413
matches ! (
308
414
( arg. kind( ) , self_kind) ,
309
415
( ty:: Ref ( _, _, Mutability :: Not ) , ImplicitSelfKind :: ImmRef )
@@ -315,11 +421,11 @@ fn check_is_empty_sig<'tcx>(sig: FnSig<'tcx>, self_kind: ImplicitSelfKind, len_o
315
421
}
316
422
317
423
/// 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 > ,
424
+ fn check_for_is_empty (
425
+ cx : & LateContext < ' _ > ,
320
426
span : Span ,
321
427
self_kind : ImplicitSelfKind ,
322
- output : LenOutput < ' tcx > ,
428
+ output : LenOutput ,
323
429
impl_ty : DefId ,
324
430
item_name : Symbol ,
325
431
item_kind : & str ,
@@ -352,6 +458,7 @@ fn check_for_is_empty<'tcx>(
352
458
Some ( is_empty)
353
459
if !( is_empty. fn_has_self_parameter
354
460
&& check_is_empty_sig (
461
+ cx,
355
462
cx. tcx . fn_sig ( is_empty. def_id ) . subst_identity ( ) . skip_binder ( ) ,
356
463
self_kind,
357
464
output,
0 commit comments