-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Add new function_casts_as_integer
lint
#141470
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Add new function_casts_as_integer
lint
#141470
Conversation
This comment has been minimized.
This comment has been minimized.
07f2c3c
to
4978962
Compare
This comment has been minimized.
This comment has been minimized.
3db3153
to
d8b1955
Compare
This comment has been minimized.
This comment has been minimized.
compiler/rustc_lint/messages.ftl
Outdated
lint_function_casts_as_integer = casting a function into an integer implicitly | ||
.cast_as_fn = add `{$cast_from_ty} as {$cast_to_ty}` | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lint_function_casts_as_integer = casting a function into an integer implicitly | |
.cast_as_fn = add `{$cast_from_ty} as {$cast_to_ty}` | |
lint_function_casts_as_integer = direct cast of function item into an integer | |
.cast_as_fn = first cast to a function pointer `{$cast_from_ty}` |
how about this wording? it's not really implicit, it may just be unexpected
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good (better) to me!
d8b1955
to
45984df
Compare
This comment has been minimized.
This comment has been minimized.
45984df
to
a6107b4
Compare
This comment has been minimized.
This comment has been minimized.
a6107b4
to
24d757e
Compare
Some changes occurred in src/tools/clippy cc @rust-lang/clippy |
This comment has been minimized.
This comment has been minimized.
24d757e
to
3529162
Compare
The Miri subtree was changed cc @rust-lang/miri |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
/// a cast as `fn` first to make it obvious what's going on. It also allows | ||
/// to prevent confusion with (associated) constants. | ||
pub FUNCTION_CASTS_AS_INTEGER, | ||
Warn, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clippy has a few lints for fn
to integer casts. But they are all restriction or style lints in Clippy. Adding a warn-by-default lint about this to rustc might be a bit aggressive 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know, I implemented one myself. 😉 I think it highlights the fact that this is a big issue and that the compiler should warn about it and eventually even forbid this fn to integer cast (you need to cast to an fn pointer first).
But in any case, it's up to the lang team.
4f843ad
to
1645fd5
Compare
This comment has been minimized.
This comment has been minimized.
1645fd5
to
bc71088
Compare
This comment has been minimized.
This comment has been minimized.
☔ The latest upstream changes (presumably #141668) made this pull request unmergeable. Please resolve the merge conflicts. |
bc71088
to
1adb4d1
Compare
CI finally passed. \o/ |
The
function_casts_as_integer
lint detects cases where users cast a function pointer into an integer.warn-by-default
Example
Explanation
You should never cast a function directly into an integer but go through a cast as
fn
first to make it obvious what's going on. It also allows to prevent confusion with (associated) constants.Related to #81686 and https://stackoverflow.com/questions/68701177/whats-the-meaning-of-casting-a-rust-enum-variant-to-a-numeric-data-type
r? @Urgau