Skip to content

Conversation

flip1995
Copy link
Member

@flip1995 flip1995 commented Sep 4, 2025

r? @ghost

changelog: integer-to-pointer-transmutes are now handled by the upstream integer_to_ptr_transmutes Rust lint and no longer by the [useless_transmute] Clippy lint

flip1995 and others added 13 commits August 22, 2025 14:57
To avoid workspace warnings.
Add lint against integer to pointer transmutes

# `integer_to_ptr_transmutes`

*warn-by-default*

The `integer_to_ptr_transmutes` lint detects integer to pointer transmutes where the resulting pointers are undefined behavior to dereference.

### Example

```rust
fn foo(a: usize) -> *const u8 {
    unsafe {
        std::mem::transmute::<usize, *const u8>(a)
    }
}
```

```
warning: transmuting an integer to a pointer creates a pointer without provenance
   --> a.rs:1:9
    |
158 |         std::mem::transmute::<usize, *const u8>(a)
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: this is dangerous because dereferencing the resulting pointer is undefined behavior
    = note: exposed provenance semantics can be used to create a pointer based on some previously exposed provenance
    = help: if you truly mean to create a pointer without provenance, use `std::ptr::without_provenance_mut`
    = help: for more information about transmute, see <https://doc.rust-lang.org/std/mem/fn.transmute.html#transmutation-between-pointers-and-integers>
    = help: for more information about exposed provenance, see <https://doc.rust-lang.org/std/ptr/index.html#exposed-provenance>
    = note: `#[warn(integer_to_ptr_transmutes)]` on by default
help: use `std::ptr::with_exposed_provenance` instead to use a previously exposed provenance
    |
158 -     std::mem::transmute::<usize, *const u8>(a)
158 +     std::ptr::with_exposed_provenance::<u8>(a)
    |
```

### Explanation

Any attempt to use the resulting pointers are undefined behavior as the resulting pointers won't have any provenance.

Alternatively, `std::ptr::with_exposed_provenance` should be used, as they do not carry the provenance requirement or if the wanting to create pointers without provenance `std::ptr::without_provenance_mut` should be used.

See [std::mem::transmute] in the reference for more details.

[std::mem::transmute]: https://doc.rust-lang.org/std/mem/fn.transmute.html

--------

People are getting tripped up on this, see rust-lang/rust#128409 and rust-lang/rust#141220. There are >90 cases like these on [GitHub search](https://github.com/search?q=lang%3Arust+%2Ftransmute%3A%3A%3Cu%5B0-9%5D*.*%2C+%5C*const%2F&type=code).

Fixes rust-lang#13140
Fixes rust-lang/rust#141220
Fixes rust-lang/rust#145523

`@rustbot` labels +I-lang-nominated +T-lang
cc `@traviscross`
r? compiler
Use unnamed lifetime spans as primary spans for `MISMATCHED_LIFETIME_SYNTAXES`

Fixes rust-lang/rust#145772

This PR changes the primary span(s) of the `MISMATCHED_LIFETIME_SYNTAXES` to point to the *unnamed* lifetime spans in both the inputs and *outputs* of the function signature. As reported in rust-lang/rust#145772, this should make it so that IDEs highlight the spans of the actionable part of this lint, rather than just the (possibly named) input spans like they do today.

This could be tweaked further perhaps, for example for `fn foo(_: T<'_>) -> T`, we don't need to highlight the elided lifetime if the actionable part is to change only the return type to `T<'_>`, but I think it's improvement on what's here today, so I think that should be follow-up since I think the logic might get a bit hairy.

cc ```@shepmaster```
add span to struct pattern rest (..)

Struct pattern rest (`..`) did not retain span information compared to normal fields. This patch adds span information for it.

The motivation of this patch comes from when I implemented this PR for Clippy: rust-lang#15000 (comment)

It is possible to get the span of the Et cetera in a bit roundabout way, but I thought this would be nicer.
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Sep 4, 2025
@flip1995
Copy link
Member Author

flip1995 commented Sep 4, 2025

What a nice small sync!

@flip1995 flip1995 enabled auto-merge September 4, 2025 22:22
@flip1995 flip1995 added this pull request to the merge queue Sep 4, 2025
Copy link

github-actions bot commented Sep 4, 2025

Lintcheck changes for e9810e5

Lint Added Removed Changed
clippy::incompatible_msrv 6 6 0
clippy::missing_trait_methods 0 0 41210
clippy::useless_conversion 0 0 3
clippy::useless_transmute 0 1 0

This comment will be updated if you push new changes

Merged via the queue into rust-lang:master with commit e9b7045 Sep 4, 2025
11 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Sep 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.