Skip to content

Commit 5038877

Browse files
Convert function_cast_as_integer lint suggestion to plain *const () cast
1 parent 488f720 commit 5038877

File tree

6 files changed

+13
-14
lines changed

6 files changed

+13
-14
lines changed

compiler/rustc_lint/messages.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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
268268
lint_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
271271
lint_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

compiler/rustc_lint/src/function_cast_as_integer.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff 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
},

compiler/rustc_lint/src/lints.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff 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
)]
30363036
pub(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

tests/ui/lint/function_casts_as_integer.fixed

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
fn foo() {}
77

88
fn 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
}

tests/ui/lint/function_casts_as_integer.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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
}

tests/ui/lint/function_casts_as_integer.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@ note: the lint level is defined here
99
|
1010
LL | #![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

1717
error: direct cast of function item into an integer
1818
--> $DIR/function_casts_as_integer.rs:10:25
1919
|
2020
LL | 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

2828
error: aborting due to 2 previous errors
2929

0 commit comments

Comments
 (0)