rust/tests
Jacob Pratt 265503668d
Rollup merge of #144531 - Urgau:int_to_ptr_transmutes, r=jackh726
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 https://github.com/rust-lang/rust/issues/128409 and https://github.com/rust-lang/rust/issues/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 https://github.com/rust-lang/rust-clippy/issues/13140
Fixes https://github.com/rust-lang/rust/issues/141220
Fixes https://github.com/rust-lang/rust/issues/145523

`@rustbot` labels +I-lang-nominated +T-lang
cc `@traviscross`
r? compiler
2025-08-23 23:58:35 -04:00
..
assembly-llvm Rollup merge of #145761 - Gelbpunkt:hermit-aarch64_be, r=wesleywiser 2025-08-22 22:01:01 -04:00
auxiliary Set NumRegisterParameters LLVM module flag to N when -Zregparm=N is 2025-08-13 17:37:30 +02:00
codegen-llvm Rollup merge of #145380 - okaneco:add-codegen-tests, r=Mark-Simulacrum 2025-08-22 22:00:50 -04:00
codegen-units Auto merge of #142893 - Mark-Simulacrum:no-const-collect, r=oli-obk 2025-06-27 12:57:05 +00:00
coverage coverage: Remove all unstable support for MC/DC instrumentation 2025-08-06 22:38:52 +10:00
coverage-run-rustdoc
crashes fix: Reject async assoc fns of const traits/impls in ast_passes 2025-08-15 16:31:10 +09:00
debuginfo Rollup merge of #145745 - heiher:ignore-basic-stepping, r=lqd 2025-08-22 22:00:58 -04:00
incremental Bless incremental tests. 2025-07-13 13:50:01 +00:00
mir-opt Rollup merge of #142185 - saethlin:refprop-moves, r=cjgillot 2025-08-22 22:00:46 -04:00
pretty Update autodiff tests for the new intrinsics impl 2025-08-14 18:33:43 +00:00
run-make Rollup merge of #145405 - durin42:test-cleanup-tmpdir, r=lqd 2025-08-19 14:18:24 +10:00
rustdoc rustdoc: update attribute tests 2025-08-23 11:27:03 +02:00
rustdoc-gui rustdoc-search: GUI tests check for // in URL 2025-08-21 09:24:54 -07:00
rustdoc-js rustdoc: add tests for raw pointers in type-based search 2025-08-21 17:50:12 -05:00
rustdoc-js-std tests/rustdoc-js-std/parser-errors.js: remove syntax that is now valid 2025-08-21 20:50:33 -05:00
rustdoc-json rustdoc-json: Move #[macro_export] from Other to it's own variant 2025-07-30 19:57:32 +00:00
rustdoc-ui Bless rustdoc-ui. 2025-08-22 20:31:24 +00:00
ui Rollup merge of #144531 - Urgau:int_to_ptr_transmutes, r=jackh726 2025-08-23 23:58:35 -04:00
ui-fulldeps On E0277, point at type that doesn't implement bound 2025-08-22 17:55:15 +00:00
COMPILER_TESTS.md