@@ -322,7 +322,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
322
322
err. span_suggestion (
323
323
self . span ,
324
324
"compare with zero instead" ,
325
- format ! ( "{} != 0" , snippet ) ,
325
+ format ! ( "{snippet } != 0" ) ,
326
326
Applicability :: MachineApplicable ,
327
327
) ;
328
328
}
@@ -373,8 +373,8 @@ impl<'a, 'tcx> CastCheck<'tcx> {
373
373
let mut sugg = None ;
374
374
let mut sugg_mutref = false ;
375
375
if let ty:: Ref ( reg, cast_ty, mutbl) = * self . cast_ty . kind ( ) {
376
- if let ty:: RawPtr ( TypeAndMut { ty : expr_ty, .. } ) = * self . expr_ty . kind ( ) {
377
- if fcx
376
+ if let ty:: RawPtr ( TypeAndMut { ty : expr_ty, .. } ) = * self . expr_ty . kind ( )
377
+ && fcx
378
378
. try_coerce (
379
379
self . expr ,
380
380
fcx. tcx . mk_ref (
@@ -386,27 +386,25 @@ impl<'a, 'tcx> CastCheck<'tcx> {
386
386
None ,
387
387
)
388
388
. is_ok ( )
389
- {
390
- sugg = Some ( ( format ! ( "&{}*" , mutbl. prefix_str( ) ) , cast_ty == expr_ty) ) ;
391
- }
392
- } else if let ty:: Ref ( expr_reg, expr_ty, expr_mutbl) = * self . expr_ty . kind ( ) {
393
- if expr_mutbl == Mutability :: Not
394
- && mutbl == Mutability :: Mut
395
- && fcx
396
- . try_coerce (
397
- self . expr ,
398
- fcx. tcx . mk_ref (
399
- expr_reg,
400
- TypeAndMut { ty : expr_ty, mutbl : Mutability :: Mut } ,
401
- ) ,
402
- self . cast_ty ,
403
- AllowTwoPhase :: No ,
404
- None ,
405
- )
406
- . is_ok ( )
407
- {
408
- sugg_mutref = true ;
409
- }
389
+ {
390
+ sugg = Some ( ( format ! ( "&{}*" , mutbl. prefix_str( ) ) , cast_ty == expr_ty) ) ;
391
+ } else if let ty:: Ref ( expr_reg, expr_ty, expr_mutbl) = * self . expr_ty . kind ( )
392
+ && expr_mutbl == Mutability :: Not
393
+ && mutbl == Mutability :: Mut
394
+ && fcx
395
+ . try_coerce (
396
+ self . expr ,
397
+ fcx. tcx . mk_ref (
398
+ expr_reg,
399
+ TypeAndMut { ty : expr_ty, mutbl : Mutability :: Mut } ,
400
+ ) ,
401
+ self . cast_ty ,
402
+ AllowTwoPhase :: No ,
403
+ None ,
404
+ )
405
+ . is_ok ( )
406
+ {
407
+ sugg_mutref = true ;
410
408
}
411
409
412
410
if !sugg_mutref
@@ -423,8 +421,8 @@ impl<'a, 'tcx> CastCheck<'tcx> {
423
421
{
424
422
sugg = Some ( ( format ! ( "&{}" , mutbl. prefix_str( ) ) , false ) ) ;
425
423
}
426
- } else if let ty:: RawPtr ( TypeAndMut { mutbl, .. } ) = * self . cast_ty . kind ( ) {
427
- if fcx
424
+ } else if let ty:: RawPtr ( TypeAndMut { mutbl, .. } ) = * self . cast_ty . kind ( )
425
+ && fcx
428
426
. try_coerce (
429
427
self . expr ,
430
428
fcx. tcx . mk_ref (
@@ -436,9 +434,8 @@ impl<'a, 'tcx> CastCheck<'tcx> {
436
434
None ,
437
435
)
438
436
. is_ok ( )
439
- {
440
- sugg = Some ( ( format ! ( "&{}" , mutbl. prefix_str( ) ) , false ) ) ;
441
- }
437
+ {
438
+ sugg = Some ( ( format ! ( "&{}" , mutbl. prefix_str( ) ) , false ) ) ;
442
439
}
443
440
if sugg_mutref {
444
441
err. span_label ( self . span , "invalid cast" ) ;
@@ -483,28 +480,28 @@ impl<'a, 'tcx> CastCheck<'tcx> {
483
480
) {
484
481
let mut label = true ;
485
482
// Check `impl From<self.expr_ty> for self.cast_ty {}` for accurate suggestion:
486
- if let Ok ( snippet) = fcx. tcx . sess . source_map ( ) . span_to_snippet ( self . expr_span ) {
487
- if let Some ( from_trait) = fcx. tcx . get_diagnostic_item ( sym:: From ) {
488
- let ty = fcx . resolve_vars_if_possible ( self . cast_ty ) ;
489
- // Erase regions to avoid panic in `prove_value` when calling
490
- // `type_implements_trait`.
491
- let ty = fcx . tcx . erase_regions ( ty ) ;
492
- let expr_ty = fcx. resolve_vars_if_possible ( self . expr_ty ) ;
493
- let expr_ty = fcx. tcx . erase_regions ( expr_ty) ;
494
- let ty_params = fcx. tcx . mk_substs_trait ( expr_ty, & [ ] ) ;
495
- if fcx
496
- . infcx
497
- . type_implements_trait ( from_trait , ty , ty_params , fcx . param_env )
498
- . must_apply_modulo_regions ( )
499
- {
500
- label = false ;
501
- err . span_suggestion (
502
- self . span ,
503
- "consider using the `From` trait instead" ,
504
- format ! ( "{}::from({})" , self . cast_ty , snippet ) ,
505
- Applicability :: MaybeIncorrect ,
506
- ) ;
507
- }
483
+ if let Ok ( snippet) = fcx. tcx . sess . source_map ( ) . span_to_snippet ( self . expr_span )
484
+ && let Some ( from_trait) = fcx. tcx . get_diagnostic_item ( sym:: From )
485
+ {
486
+ let ty = fcx . resolve_vars_if_possible ( self . cast_ty ) ;
487
+ // Erase regions to avoid panic in `prove_value` when calling
488
+ // `type_implements_trait`.
489
+ let ty = fcx. tcx . erase_regions ( ty ) ;
490
+ let expr_ty = fcx. resolve_vars_if_possible ( self . expr_ty ) ;
491
+ let expr_ty = fcx. tcx . erase_regions ( expr_ty) ;
492
+ let ty_params = fcx. tcx . mk_substs_trait ( expr_ty , & [ ] ) ;
493
+ if fcx
494
+ . infcx
495
+ . type_implements_trait ( from_trait , ty , ty_params , fcx . param_env )
496
+ . must_apply_modulo_regions ( )
497
+ {
498
+ label = false ;
499
+ err . span_suggestion (
500
+ self . span ,
501
+ "consider using the `From` trait instead" ,
502
+ format ! ( "{}::from({})" , self . cast_ty , snippet ) ,
503
+ Applicability :: MaybeIncorrect ,
504
+ ) ;
508
505
}
509
506
}
510
507
let msg = "an `as` expression can only be used to convert between primitive \
@@ -627,10 +624,8 @@ impl<'a, 'tcx> CastCheck<'tcx> {
627
624
}
628
625
}
629
626
} else {
630
- let msg = & format ! (
631
- "consider using an implicit coercion to `&{}{}` instead" ,
632
- mtstr, tstr
633
- ) ;
627
+ let msg =
628
+ & format ! ( "consider using an implicit coercion to `&{mtstr}{tstr}` instead" ) ;
634
629
err. span_help ( self . span , msg) ;
635
630
}
636
631
}
@@ -640,14 +635,14 @@ impl<'a, 'tcx> CastCheck<'tcx> {
640
635
err. span_suggestion (
641
636
self . cast_span ,
642
637
"you can cast to a `Box` instead" ,
643
- format ! ( "Box<{}>" , s ) ,
638
+ format ! ( "Box<{s }>" ) ,
644
639
Applicability :: MachineApplicable ,
645
640
) ;
646
641
}
647
642
Err ( _) => {
648
643
err. span_help (
649
644
self . cast_span ,
650
- & format ! ( "you might have meant `Box<{}>`" , tstr ) ,
645
+ & format ! ( "you might have meant `Box<{tstr }>`" ) ,
651
646
) ;
652
647
}
653
648
}
@@ -678,8 +673,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
678
673
) )
679
674
. help ( & format ! (
680
675
"cast can be replaced by coercion; this might \
681
- require {}a temporary variable",
682
- type_asc_or
676
+ require {type_asc_or}a temporary variable"
683
677
) )
684
678
. emit ( ) ;
685
679
} ) ;
@@ -969,21 +963,21 @@ impl<'a, 'tcx> CastCheck<'tcx> {
969
963
}
970
964
971
965
fn cenum_impl_drop_lint ( & self , fcx : & FnCtxt < ' a , ' tcx > ) {
972
- if let ty:: Adt ( d, _) = self . expr_ty . kind ( ) {
973
- if d. has_dtor ( fcx. tcx ) {
974
- fcx . tcx . struct_span_lint_hir (
975
- lint :: builtin :: CENUM_IMPL_DROP_CAST ,
976
- self . expr . hir_id ,
977
- self . span ,
978
- |err| {
979
- err. build ( & format ! (
980
- "cannot cast enum `{}` into integer `{}` because it implements `Drop`" ,
981
- self . expr_ty , self . cast_ty
982
- ) )
983
- . emit ( ) ;
984
- } ,
985
- ) ;
986
- }
966
+ if let ty:: Adt ( d, _) = self . expr_ty . kind ( )
967
+ && d. has_dtor ( fcx. tcx )
968
+ {
969
+ fcx . tcx . struct_span_lint_hir (
970
+ lint :: builtin :: CENUM_IMPL_DROP_CAST ,
971
+ self . expr . hir_id ,
972
+ self . span ,
973
+ | err| {
974
+ err . build ( & format ! (
975
+ "cannot cast enum `{}` into integer `{}` because it implements `Drop`" ,
976
+ self . expr_ty , self . cast_ty
977
+ ) )
978
+ . emit ( ) ;
979
+ } ,
980
+ ) ;
987
981
}
988
982
}
989
983
@@ -1007,7 +1001,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
1007
1001
err. span_suggestion (
1008
1002
self . span ,
1009
1003
msg,
1010
- format ! ( "({}).addr(){}" , snippet , scalar_cast ) ,
1004
+ format ! ( "({snippet }).addr(){scalar_cast}" ) ,
1011
1005
Applicability :: MaybeIncorrect
1012
1006
) ;
1013
1007
} else {
@@ -1038,7 +1032,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
1038
1032
err. span_suggestion (
1039
1033
self . span ,
1040
1034
msg,
1041
- format ! ( "(...).with_addr({})" , snippet ) ,
1035
+ format ! ( "(...).with_addr({snippet })" ) ,
1042
1036
Applicability :: HasPlaceholders ,
1043
1037
) ;
1044
1038
} else {
0 commit comments