@@ -324,13 +324,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
324
324
& format ! ( "`{}: {}`" , sup, sub) ,
325
325
) ;
326
326
// We should only suggest rewriting the `where` clause if the predicate is within that `where` clause
327
- if self
328
- . tcx
329
- . hir ( )
330
- . get_generics ( impl_item_def_id)
331
- . unwrap ( )
332
- . where_clause_span
333
- . contains ( span)
327
+ if let Some ( generics) = self . tcx . hir ( ) . get_generics ( impl_item_def_id)
328
+ && generics. where_clause_span . contains ( span)
334
329
{
335
330
self . suggest_copy_trait_method_bounds (
336
331
trait_item_def_id,
@@ -390,52 +385,51 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
390
385
// but right now it's not really very smart when it comes to implicit `Sized`
391
386
// predicates and bounds on the trait itself.
392
387
393
- let impl_def_id =
394
- self . tcx . associated_item ( impl_item_def_id) . impl_container ( self . tcx ) . unwrap ( ) ;
395
- let trait_substs = self
388
+ let Some ( impl_def_id) =
389
+ self . tcx . associated_item ( impl_item_def_id) . impl_container ( self . tcx ) else { return ; } ;
390
+ let Some ( trait_ref ) = self
396
391
. tcx
397
392
. impl_trait_ref ( impl_def_id)
398
- . unwrap ( )
393
+ else { return ; } ;
394
+ let trait_substs = trait_ref
399
395
// Replace the explicit self type with `Self` for better suggestion rendering
400
396
. with_self_ty ( self . tcx , self . tcx . mk_ty_param ( 0 , kw:: SelfUpper ) )
401
397
. substs ;
402
398
let trait_item_substs =
403
399
ty:: InternalSubsts :: identity_for_item ( self . tcx , impl_item_def_id. to_def_id ( ) )
404
400
. rebase_onto ( self . tcx , impl_def_id, trait_substs) ;
405
401
406
- let mut is_suggestable = true ;
407
- let trait_predicates = self
402
+ let Ok ( trait_predicates) = self
408
403
. tcx
409
404
. bound_explicit_predicates_of ( trait_item_def_id)
410
405
. map_bound ( |p| p. predicates )
411
406
. subst_iter_copied ( self . tcx , trait_item_substs)
412
407
. map ( |( pred, _) | {
413
- if !pred. is_suggestable ( self . tcx , false ) {
414
- is_suggestable = false ;
408
+ if pred. is_suggestable ( self . tcx , false ) {
409
+ Ok ( pred. to_string ( ) )
410
+ } else {
411
+ Err ( ( ) )
415
412
}
416
- pred. to_string ( )
417
413
} )
418
- . collect :: < Vec < _ > > ( ) ;
414
+ . collect :: < Result < Vec < _ > , ( ) > > ( ) else { return ; } ;
419
415
420
- let generics = self . tcx . hir ( ) . get_generics ( impl_item_def_id) . unwrap ( ) ;
416
+ let Some ( generics) = self . tcx . hir ( ) . get_generics ( impl_item_def_id) else { return ; } ;
421
417
422
- if is_suggestable {
423
- if trait_predicates. is_empty ( ) {
424
- err. span_suggestion_verbose (
425
- generics. where_clause_span ,
426
- "remove the `where` clause" ,
427
- String :: new ( ) ,
428
- Applicability :: MachineApplicable ,
429
- ) ;
430
- } else {
431
- let space = if generics. where_clause_span . is_empty ( ) { " " } else { "" } ;
432
- err. span_suggestion_verbose (
433
- generics. where_clause_span ,
434
- "copy the `where` clause predicates from the trait" ,
435
- format ! ( "{space}where {}" , trait_predicates. join( ", " ) ) ,
436
- Applicability :: MachineApplicable ,
437
- ) ;
438
- }
418
+ if trait_predicates. is_empty ( ) {
419
+ err. span_suggestion_verbose (
420
+ generics. where_clause_span ,
421
+ "remove the `where` clause" ,
422
+ String :: new ( ) ,
423
+ Applicability :: MachineApplicable ,
424
+ ) ;
425
+ } else {
426
+ let space = if generics. where_clause_span . is_empty ( ) { " " } else { "" } ;
427
+ err. span_suggestion_verbose (
428
+ generics. where_clause_span ,
429
+ "copy the `where` clause predicates from the trait" ,
430
+ format ! ( "{space}where {}" , trait_predicates. join( ", " ) ) ,
431
+ Applicability :: MachineApplicable ,
432
+ ) ;
439
433
}
440
434
}
441
435
0 commit comments