Skip to content

Conversation

@GuillaumeGomez
Copy link
Member

@GuillaumeGomez GuillaumeGomez commented May 23, 2025

The function_casts_as_integer lint detects cases where users cast a function pointer into an integer.

warn-by-default

Example

fn foo() {}
let x = foo as usize;
warning: casting a function into an integer implicitly
  --> $DIR/function_casts_as_integer.rs:9:17
   |
LL |     let x = foo as usize;
   |                 ^^^^^^^^
   |
help: add `fn() as usize`
   |
LL |     let x = foo as fn() as usize;
   |                 +++++++

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

@rustbot rustbot added O-unix Operating system: Unix-like S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels May 23, 2025
@rust-log-analyzer

This comment has been minimized.

@GuillaumeGomez GuillaumeGomez force-pushed the function_casts_as_integer branch 2 times, most recently from 07f2c3c to 4978962 Compare May 23, 2025 20:54
@rust-log-analyzer

This comment has been minimized.

@GuillaumeGomez GuillaumeGomez force-pushed the function_casts_as_integer branch 2 times, most recently from 3db3153 to d8b1955 Compare May 24, 2025 10:10
@rust-log-analyzer

This comment has been minimized.

@Urgau Urgau added T-lang Relevant to the language team and removed T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels May 24, 2025
@GuillaumeGomez GuillaumeGomez force-pushed the function_casts_as_integer branch from d8b1955 to 45984df Compare May 24, 2025 18:45
@rust-log-analyzer

This comment has been minimized.

@GuillaumeGomez GuillaumeGomez force-pushed the function_casts_as_integer branch from 45984df to a6107b4 Compare May 24, 2025 19:09
@rust-log-analyzer

This comment has been minimized.

@GuillaumeGomez GuillaumeGomez force-pushed the function_casts_as_integer branch from a6107b4 to 24d757e Compare May 24, 2025 22:47
@rustbot
Copy link
Collaborator

rustbot commented May 24, 2025

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

@rust-log-analyzer

This comment has been minimized.

@Urgau Urgau added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 26, 2025
@GuillaumeGomez GuillaumeGomez force-pushed the function_casts_as_integer branch from 24d757e to 3529162 Compare May 27, 2025 14:12
@rustbot
Copy link
Collaborator

rustbot commented May 27, 2025

The Miri subtree was changed

cc @rust-lang/miri

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

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,
Copy link
Member

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 🤔

Copy link
Member Author

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.

Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

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.

@GuillaumeGomez GuillaumeGomez force-pushed the function_casts_as_integer branch from 76b37b2 to d05f297 Compare November 10, 2025 15:38
@rustbot
Copy link
Collaborator

rustbot commented Nov 10, 2025

This PR was rebased onto a different main 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.

@GuillaumeGomez
Copy link
Member Author

Applied @Urgau's suggestions.

@Urgau
Copy link
Member

Urgau commented Nov 10, 2025

r=me once CI is green

@Urgau
Copy link
Member

Urgau commented Nov 10, 2025

@bors r+

@bors
Copy link
Collaborator

bors commented Nov 10, 2025

📌 Commit 66b4e93 has been approved by Urgau

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 10, 2025
@GuillaumeGomez
Copy link
Member Author

@bors r=urgau rollup

@bors
Copy link
Collaborator

bors commented Nov 10, 2025

💡 This pull request was already approved, no need to approve it again.

@bors
Copy link
Collaborator

bors commented Nov 10, 2025

📌 Commit 66b4e93 has been approved by urgau

It is now in the queue for this repository.

Zalathar added a commit to Zalathar/rust that referenced this pull request Nov 11, 2025
…nteger, r=urgau

Add new `function_casts_as_integer` lint

The `function_casts_as_integer` lint detects cases where users cast a function pointer into an integer.

*warn-by-default*

### Example

```rust
fn foo() {}
let x = foo as usize;
```

```
warning: casting a function into an integer implicitly
  --> $DIR/function_casts_as_integer.rs:9:17
   |
LL |     let x = foo as usize;
   |                 ^^^^^^^^
   |
help: add `fn() as usize`
   |
LL |     let x = foo as fn() as usize;
   |                 +++++++
```

### 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 rust-lang#81686 and https://stackoverflow.com/questions/68701177/whats-the-meaning-of-casting-a-rust-enum-variant-to-a-numeric-data-type

r? `@urgau`
Zalathar added a commit to Zalathar/rust that referenced this pull request Nov 11, 2025
…nteger, r=urgau

Add new `function_casts_as_integer` lint

The `function_casts_as_integer` lint detects cases where users cast a function pointer into an integer.

*warn-by-default*

### Example

```rust
fn foo() {}
let x = foo as usize;
```

```
warning: casting a function into an integer implicitly
  --> $DIR/function_casts_as_integer.rs:9:17
   |
LL |     let x = foo as usize;
   |                 ^^^^^^^^
   |
help: add `fn() as usize`
   |
LL |     let x = foo as fn() as usize;
   |                 +++++++
```

### 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 rust-lang#81686 and https://stackoverflow.com/questions/68701177/whats-the-meaning-of-casting-a-rust-enum-variant-to-a-numeric-data-type

r? ``@urgau``
Zalathar added a commit to Zalathar/rust that referenced this pull request Nov 11, 2025
…nteger, r=urgau

Add new `function_casts_as_integer` lint

The `function_casts_as_integer` lint detects cases where users cast a function pointer into an integer.

*warn-by-default*

### Example

```rust
fn foo() {}
let x = foo as usize;
```

```
warning: casting a function into an integer implicitly
  --> $DIR/function_casts_as_integer.rs:9:17
   |
LL |     let x = foo as usize;
   |                 ^^^^^^^^
   |
help: add `fn() as usize`
   |
LL |     let x = foo as fn() as usize;
   |                 +++++++
```

### 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 rust-lang#81686 and https://stackoverflow.com/questions/68701177/whats-the-meaning-of-casting-a-rust-enum-variant-to-a-numeric-data-type

r? ```@urgau```
bors added a commit that referenced this pull request Nov 11, 2025
Rollup of 16 pull requests

Successful merges:

 - #141470 (Add new `function_casts_as_integer` lint)
 - #143619 (`c_variadic`: Add future-incompatibility warning for `...` arguments without a pattern outside of `extern` blocks)
 - #146495 (rustdoc: Erase `#![doc(document_private_items)]`)
 - #147771 (Rename `*exact_{div,shr,shl}` to `*{div,shr,shl}_exact`)
 - #147833 (rustdoc-json: move `target` to `json::conversions`)
 - #147952 (Add a timeout to the `remote-test-client` connection)
 - #147955 (compiletest: Migrate `TestProps` directive handling to a system of named handlers)
 - #148480 (Add `Steal::risky_hack_borrow_mut`)
 - #148506 (Special case detecting `'static` lifetime requirement coming from `-> Box<dyn Trait>`)
 - #148508 (Provide more context when mutably borrowing an imutably borrowed value)
 - #148530 (update the bootstrap readme)
 - #148608 (Add test for --test-builder success path)
 - #148636 (bootstrap: respect `build.python` on macOS)
 - #148639 (test(rustdoc): move tests into jump-to-def)
 - #148647 (Check unsafety for non-macro attributes in `validate_attr`)
 - #148667 (a few small clippy fixes)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit that referenced this pull request Nov 11, 2025
Rollup of 16 pull requests

Successful merges:

 - #141470 (Add new `function_casts_as_integer` lint)
 - #143619 (`c_variadic`: Add future-incompatibility warning for `...` arguments without a pattern outside of `extern` blocks)
 - #146495 (rustdoc: Erase `#![doc(document_private_items)]`)
 - #147771 (Rename `*exact_{div,shr,shl}` to `*{div,shr,shl}_exact`)
 - #147833 (rustdoc-json: move `target` to `json::conversions`)
 - #147952 (Add a timeout to the `remote-test-client` connection)
 - #147955 (compiletest: Migrate `TestProps` directive handling to a system of named handlers)
 - #148480 (Add `Steal::risky_hack_borrow_mut`)
 - #148506 (Special case detecting `'static` lifetime requirement coming from `-> Box<dyn Trait>`)
 - #148508 (Provide more context when mutably borrowing an imutably borrowed value)
 - #148530 (update the bootstrap readme)
 - #148608 (Add test for --test-builder success path)
 - #148636 (bootstrap: respect `build.python` on macOS)
 - #148639 (test(rustdoc): move tests into jump-to-def)
 - #148647 (Check unsafety for non-macro attributes in `validate_attr`)
 - #148667 (a few small clippy fixes)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit that referenced this pull request Nov 11, 2025
Rollup of 15 pull requests

Successful merges:

 - #141470 (Add new `function_casts_as_integer` lint)
 - #143619 (`c_variadic`: Add future-incompatibility warning for `...` arguments without a pattern outside of `extern` blocks)
 - #146495 (rustdoc: Erase `#![doc(document_private_items)]`)
 - #147771 (Rename `*exact_{div,shr,shl}` to `*{div,shr,shl}_exact`)
 - #147833 (rustdoc-json: move `target` to `json::conversions`)
 - #147955 (compiletest: Migrate `TestProps` directive handling to a system of named handlers)
 - #148480 (Add `Steal::risky_hack_borrow_mut`)
 - #148506 (Special case detecting `'static` lifetime requirement coming from `-> Box<dyn Trait>`)
 - #148508 (Provide more context when mutably borrowing an imutably borrowed value)
 - #148530 (update the bootstrap readme)
 - #148608 (Add test for --test-builder success path)
 - #148636 (bootstrap: respect `build.python` on macOS)
 - #148639 (test(rustdoc): move tests into jump-to-def)
 - #148647 (Check unsafety for non-macro attributes in `validate_attr`)
 - #148667 (a few small clippy fixes)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 5b9211c into rust-lang:main Nov 11, 2025
11 checks passed
@rustbot rustbot added this to the 1.93.0 milestone Nov 11, 2025
rust-timer added a commit that referenced this pull request Nov 11, 2025
Rollup merge of #141470 - GuillaumeGomez:function_casts_as_integer, r=urgau

Add new `function_casts_as_integer` lint

The `function_casts_as_integer` lint detects cases where users cast a function pointer into an integer.

*warn-by-default*

### Example

```rust
fn foo() {}
let x = foo as usize;
```

```
warning: casting a function into an integer implicitly
  --> $DIR/function_casts_as_integer.rs:9:17
   |
LL |     let x = foo as usize;
   |                 ^^^^^^^^
   |
help: add `fn() as usize`
   |
LL |     let x = foo as fn() as usize;
   |                 +++++++
```

### 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````
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-run-make Area: port run-make Makefiles to rmake.rs disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. I-lang-radar Items that are on lang's radar and will need eventual work or consideration. needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. O-unix Operating system: Unix-like O-windows Operating system: Windows S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-clippy Relevant to the Clippy team. T-lang Relevant to the language team to-announce Announce this issue on triage meeting

Projects

None yet

Development

Successfully merging this pull request may close these issues.