@@ -33,7 +33,7 @@ use super::FnCtxt;
3333use crate :: hir:: def_id:: DefId ;
3434use crate :: type_error_struct;
3535use hir:: def_id:: LOCAL_CRATE ;
36- use rustc_errors:: { struct_span_err, Applicability , DiagnosticBuilder , ErrorGuaranteed } ;
36+ use rustc_errors:: { struct_span_err, Applicability , DelayDm , DiagnosticBuilder , ErrorGuaranteed } ;
3737use rustc_hir as hir;
3838use rustc_infer:: traits:: { Obligation , ObligationCause , ObligationCauseCode } ;
3939use rustc_middle:: mir:: Mutability ;
@@ -754,19 +754,25 @@ impl<'a, 'tcx> CastCheck<'tcx> {
754754 } else {
755755 ( "" , lint:: builtin:: TRIVIAL_CASTS )
756756 } ;
757- fcx. tcx . struct_span_lint_hir ( lint, self . expr . hir_id , self . span , |err| {
758- err. build ( & format ! (
759- "trivial {}cast: `{}` as `{}`" ,
760- adjective,
761- fcx. ty_to_string( t_expr) ,
762- fcx. ty_to_string( t_cast)
763- ) )
764- . help ( & format ! (
765- "cast can be replaced by coercion; this might \
766- require {type_asc_or}a temporary variable"
767- ) )
768- . emit ( ) ;
769- } ) ;
757+ fcx. tcx . struct_span_lint_hir (
758+ lint,
759+ self . expr . hir_id ,
760+ self . span ,
761+ DelayDm ( || {
762+ format ! (
763+ "trivial {}cast: `{}` as `{}`" ,
764+ adjective,
765+ fcx. ty_to_string( t_expr) ,
766+ fcx. ty_to_string( t_cast)
767+ )
768+ } ) ,
769+ |lint| {
770+ lint. help ( format ! (
771+ "cast can be replaced by coercion; this might \
772+ require {type_asc_or}a temporary variable"
773+ ) )
774+ } ,
775+ ) ;
770776 }
771777
772778 #[ instrument( skip( fcx) , level = "debug" ) ]
@@ -1074,12 +1080,12 @@ impl<'a, 'tcx> CastCheck<'tcx> {
10741080 lint:: builtin:: CENUM_IMPL_DROP_CAST ,
10751081 self . expr . hir_id ,
10761082 self . span ,
1077- |err| {
1078- err . build ( & format ! (
1079- "cannot cast enum `{}` into integer `{}` because it implements `Drop`" ,
1080- self . expr_ty , self . cast_ty
1081- ) )
1082- . emit ( ) ;
1083+ DelayDm ( || format ! (
1084+ "cannot cast enum `{}` into integer `{}` because it implements `Drop`" ,
1085+ self . expr_ty , self . cast_ty
1086+ ) ) ,
1087+ |lint| {
1088+ lint
10831089 } ,
10841090 ) ;
10851091 }
@@ -1090,12 +1096,11 @@ impl<'a, 'tcx> CastCheck<'tcx> {
10901096 lint:: builtin:: LOSSY_PROVENANCE_CASTS ,
10911097 self . expr . hir_id ,
10921098 self . span ,
1093- |err| {
1094- let mut err = err. build ( & format ! (
1099+ DelayDm ( || format ! (
10951100 "under strict provenance it is considered bad style to cast pointer `{}` to integer `{}`" ,
10961101 self . expr_ty, self . cast_ty
1097- ) ) ;
1098-
1102+ ) ) ,
1103+ |lint| {
10991104 let msg = "use `.addr()` to obtain the address of a pointer" ;
11001105
11011106 let expr_prec = self . expr . precedence ( ) . order ( ) ;
@@ -1114,22 +1119,22 @@ impl<'a, 'tcx> CastCheck<'tcx> {
11141119 ( cast_span, format!( ").addr(){scalar_cast}" ) ) ,
11151120 ] ;
11161121
1117- err . multipart_suggestion ( msg, suggestions, Applicability :: MaybeIncorrect ) ;
1122+ lint . multipart_suggestion ( msg, suggestions, Applicability :: MaybeIncorrect ) ;
11181123 } else {
1119- err . span_suggestion (
1124+ lint . span_suggestion (
11201125 cast_span,
11211126 msg,
11221127 format ! ( ".addr(){scalar_cast}" ) ,
11231128 Applicability :: MaybeIncorrect ,
11241129 ) ;
11251130 }
11261131
1127- err . help (
1132+ lint . help (
11281133 "if you can't comply with strict provenance and need to expose the pointer \
11291134 provenance you can use `.expose_addr()` instead"
11301135 ) ;
11311136
1132- err . emit ( ) ;
1137+ lint
11331138 } ,
11341139 ) ;
11351140 }
@@ -1139,24 +1144,24 @@ impl<'a, 'tcx> CastCheck<'tcx> {
11391144 lint:: builtin:: FUZZY_PROVENANCE_CASTS ,
11401145 self . expr . hir_id ,
11411146 self . span ,
1142- |err| {
1143- let mut err = err . build ( & format ! (
1144- "strict provenance disallows casting integer `{}` to pointer `{}`" ,
1145- self . expr_ty , self . cast_ty
1146- ) ) ;
1147+ DelayDm ( || format ! (
1148+ "strict provenance disallows casting integer `{}` to pointer `{}`" ,
1149+ self . expr_ty , self . cast_ty
1150+ ) ) ,
1151+ |lint| {
11471152 let msg = "use `.with_addr()` to adjust a valid pointer in the same allocation, to this address" ;
11481153 let suggestions = vec ! [
11491154 ( self . expr_span. shrink_to_lo( ) , String :: from( "(...).with_addr(" ) ) ,
11501155 ( self . expr_span. shrink_to_hi( ) . to( self . cast_span) , String :: from( ")" ) ) ,
11511156 ] ;
11521157
1153- err . multipart_suggestion ( msg, suggestions, Applicability :: MaybeIncorrect ) ;
1154- err . help (
1158+ lint . multipart_suggestion ( msg, suggestions, Applicability :: MaybeIncorrect ) ;
1159+ lint . help (
11551160 "if you can't comply with strict provenance and don't have a pointer with \
11561161 the correct provenance you can use `std::ptr::from_exposed_addr()` instead"
11571162 ) ;
11581163
1159- err . emit ( ) ;
1164+ lint
11601165 } ,
11611166 ) ;
11621167 }
0 commit comments