File tree Expand file tree Collapse file tree 6 files changed +13
-14
lines changed Expand file tree Collapse file tree 6 files changed +13
-14
lines changed Original file line number Diff line number Diff line change @@ -266,7 +266,7 @@ lint_forgetting_references = calls to `std::mem::forget` with a reference instea
266266 .label = argument has type `{ $arg_ty } `
267267
268268lint_function_casts_as_integer = direct cast of function item into an integer
269- .cast_as_fn = first cast to a function pointer `{ $cast_from_ty } `
269+ .cast_as_fn = first cast to a pointer `as *const () `
270270
271271lint_hidden_glob_reexport = private item shadows public glob re-export
272272 .note_glob_reexport = the name `{ $name } ` in the { $namespace } namespace is supposed to be publicly re-exported here
Original file line number Diff line number Diff line change @@ -39,7 +39,7 @@ impl<'tcx> LateLintPass<'tcx> for FunctionCastsAsInteger {
3939 let hir:: ExprKind :: Cast ( cast_from_expr, cast_to_expr) = expr. kind else { return } ;
4040 let cast_to_ty = cx. typeck_results ( ) . expr_ty ( expr) ;
4141 // Casting to a function (pointer?), so all good.
42- if matches ! ( cast_to_ty. kind( ) , ty:: FnDef ( ..) | ty:: FnPtr ( ..) ) {
42+ if matches ! ( cast_to_ty. kind( ) , ty:: FnDef ( ..) | ty:: FnPtr ( ..) | ty :: RawPtr ( .. ) ) {
4343 return ;
4444 }
4545 let cast_from_ty = cx. typeck_results ( ) . expr_ty ( cast_from_expr) ;
@@ -51,8 +51,6 @@ impl<'tcx> LateLintPass<'tcx> for FunctionCastsAsInteger {
5151 FunctionCastsAsIntegerDiag {
5252 sugg : FunctionCastsAsIntegerSugg {
5353 suggestion : cast_from_expr. span . shrink_to_hi ( ) ,
54- // We get the function pointer to have a nice display.
55- cast_from_ty : cx. typeck_results ( ) . expr_ty_adjusted ( cast_from_expr) ,
5654 cast_to_ty,
5755 } ,
5856 } ,
Original file line number Diff line number Diff line change @@ -3029,14 +3029,13 @@ pub(crate) struct FunctionCastsAsIntegerDiag<'tcx> {
30293029#[ derive( Subdiagnostic ) ]
30303030#[ suggestion(
30313031 lint_cast_as_fn,
3032- code = " as {cast_from_ty} " ,
3032+ code = " as *const () " ,
30333033 applicability = "machine-applicable" ,
30343034 style = "verbose"
30353035) ]
30363036pub ( crate ) struct FunctionCastsAsIntegerSugg < ' tcx > {
30373037 #[ primary_span]
30383038 pub suggestion : Span ,
3039- pub cast_from_ty : Ty < ' tcx > ,
30403039 pub cast_to_ty : Ty < ' tcx > ,
30413040}
30423041
Original file line number Diff line number Diff line change 66fn foo() {}
77
88fn main() {
9- let x = foo as fn () as usize; //~ ERROR: function_casts_as_integer
10- let x = String::len as for<'a> fn(&'a String) -> usize as usize; //~ ERROR: function_casts_as_integer
9+ let x = foo as *const () as usize; //~ ERROR: function_casts_as_integer
10+ let x = String::len as *const () as usize; //~ ERROR: function_casts_as_integer
1111 // Ok.
1212 let x = foo as fn() as usize;
13+ let x = foo as *const () as usize;
1314}
Original file line number Diff line number Diff line change @@ -10,4 +10,5 @@ fn main() {
1010 let x = String :: len as usize ; //~ ERROR: function_casts_as_integer
1111 // Ok.
1212 let x = foo as fn ( ) as usize ;
13+ let x = foo as * const ( ) as usize ;
1314}
Original file line number Diff line number Diff line change @@ -9,21 +9,21 @@ note: the lint level is defined here
99 |
1010LL | #![deny(function_casts_as_integer)]
1111 | ^^^^^^^^^^^^^^^^^^^^^^^^^
12- help: first cast to a function pointer `fn ()`
12+ help: first cast to a pointer `as *const ()`
1313 |
14- LL | let x = foo as fn () as usize;
15- | +++++++
14+ LL | let x = foo as *const () as usize;
15+ | ++++++++++++
1616
1717error: direct cast of function item into an integer
1818 --> $DIR/function_casts_as_integer.rs:10:25
1919 |
2020LL | let x = String::len as usize;
2121 | ^^^^^^^^
2222 |
23- help: first cast to a function pointer `for<'a> fn(&'a String) -> usize `
23+ help: first cast to a pointer `as *const () `
2424 |
25- LL | let x = String::len as for<'a> fn(&'a String) -> usize as usize;
26- | ++++++++++++++++++++++++++++++++++
25+ LL | let x = String::len as *const () as usize;
26+ | ++++++++++++
2727
2828error: aborting due to 2 previous errors
2929
You can’t perform that action at this time.
0 commit comments