- Notifications
You must be signed in to change notification settings - Fork 13.9k
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.
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.
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.
Agreed 👍 Just want to add this information as "prior art" for the lang team to make this decision. Even though it might've sounded like it, I'm not against adding this lint to rustc.
Clippy question: Do you think if this lint gets added to rustc, we can (partially) deprecate Clippy lints?
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.
Hard to say. For example confusing_method_to_numeric_cast
provides extra information about what (likely) went wrong. But with the current lint, they likely would already have seen the problem and fixed it. So by default I'd say yes. But we could eventually uplift part of them to add the extra context clippy has that this lint doesn't provide. Would make it much more interesting and even more useful.
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.
Yeah, a partial uplift might be good then, should this be accepted.
This comment has been minimized.
This comment has been minimized.
The last time we discussed this on lang, one part of the outcome is what @joshtriplett said in #141470 (comment):
To what degree might that be helpful? In prior discussion, one place where the PR in its current form raised questions is that it seems that we'd be asking people to write out full function signatures in order to make these casts (including, then, having to import or otherwise name function argument and return types that may otherwise not be needed there), and there was a feeling that this may seem too onerous. What options might we have to ameliorate that? |
I can realistically see the following options:
While option 3 seems superior, it is likely to take a long time to settle the provenance question and stabilize the API. What I am suggesting is that we could implement option 1 or 2 in the meantime. |
As the main goal of this lint is to make people realize they're likely doing a function pointer cast (and not an integer cast), I think the current approach is the best. |
…nwind and compiler crates
3777126
to 6325073
Compare This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
e813cba
to a47e09f
Compare This comment has been minimized.
This comment has been minimized.
a47e09f
to e813cba
Compare This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
The last time we discussed this on lang, we discussed how special-casing the functions where this is a known problem, e.g. |
It's what we initially did in clippy before realizing there were cases like In short: if you didn't mean to cast a function, then thanks to this lint you will realize right away. If did you mean to cast a function, then either you allow the lint or you apply the suggestion. In the long term, it seems that we want to forbid casting functions to integers directly, so this lint would be a first step into this direction while fixing a very big already existing unnoticed source of bugs. |
☔ The latest upstream changes (presumably #147779) made this pull request unmergeable. Please resolve the merge conflicts. |
What do you think about suggesting that people do this cast via a pointer to unit? I.e., |
But then the readability of the suggested code is pretty bad since you have no tip of why you need this intermediate cast. |
I don't know. I'm not sure that passing it through |
Well, if you think "OK, that makes sense, whatever", then that's an issue. The whole point is to force you to realize what's going on by reading it. Then either it's the intended purpose and you leave things as is (and eventually add a code comment 😄), or it's not and then you clean up. |
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