Skip to content

transmute_ptr_to_ptr and transmute_bytes_to_str wrongly unmangled macros #16104

@profetia

Description

@profetia

Summary

The two lint, transmute_ptr_to_ptr and transmute_bytes_to_str wrongly unmangled macros when giving suggestions

Reproducer

Code:

#[warn(clippy::transmute_ptr_to_ptr)]
fn wronglg_unmangled_macros(make_ptr: fn() -> *const u32) {
    macro_rules! call {
        ($x:expr) => {
            $x()
        };
    }
    macro_rules! take_ref {
        ($x:expr) => {
            &$x
        };
    }

    unsafe {
        let _: *const f32 = std::mem::transmute(call!(make_ptr));
        let _: &f32 = std::mem::transmute(take_ref!(1u32));
    }

    let b = vec![1_u8, 2_u8];
    macro_rules! take_ref {
        ($x:expr) => {
            $x.as_slice()
        };
    }
    unsafe {
        let _: &str = std::mem::transmute(take_ref!(b));
    }
}

Current output:

warning: transmute from a pointer to a pointer
  --> src/main.rs:17:29
   |
17 |         let _: *const f32 = std::mem::transmute(call!(make_ptr));
   |                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#transmute_ptr_to_ptr
note: the lint level is defined here
  --> src/main.rs:3:8
   |
 3 | #[warn(clippy::transmute_ptr_to_ptr)]
   |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: use `pointer::cast` instead
   |
17 -         let _: *const f32 = std::mem::transmute(call!(make_ptr));
17 +         let _: *const f32 = $x().cast::<f32>();
   |

warning: transmute from a reference to a reference
  --> src/main.rs:18:23
   |
18 |         let _: &f32 = std::mem::transmute(take_ref!(1u32));
   |                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(&$x as *const u32 as *const f32)`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#transmute_ptr_to_ptr

warning: transmute from a `&[u8]` to a `&str`
  --> src/main.rs:28:23
   |
28 |         let _: &str = std::mem::transmute(take_ref!(b));
   |                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::str::from_utf8($x.as_slice()).unwrap()`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#transmute_bytes_to_str
   = note: `#[warn(clippy::transmute_bytes_to_str)]` on by default

Desired output:

warning: transmute from a pointer to a pointer
  --> src/main.rs:17:29
   |
17 |         let _: *const f32 = std::mem::transmute(call!(make_ptr));
   |                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#transmute_ptr_to_ptr
note: the lint level is defined here
  --> src/main.rs:3:8
   |
 3 | #[warn(clippy::transmute_ptr_to_ptr)]
   |        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: use `pointer::cast` instead
   |
17 -         let _: *const f32 = std::mem::transmute(call!(make_ptr));
17 +         let _: *const f32 = call!(make_ptr).cast::<f32>();
   |

warning: transmute from a reference to a reference
  --> src/main.rs:18:23
   |
18 |         let _: &f32 = std::mem::transmute(take_ref!(1u32));
   |                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&*(take_ref!(1u32) as *const u32 as *const f32)`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#transmute_ptr_to_ptr

warning: transmute from a `&[u8]` to a `&str`
  --> src/main.rs:28:23
   |
28 |         let _: &str = std::mem::transmute(take_ref!(b));
   |                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `std::str::from_utf8(take_ref!(b)).unwrap()`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#transmute_bytes_to_str
   = note: `#[warn(clippy::transmute_bytes_to_str)]` on by default

Version

rustc 1.93.0-nightly (2286e5d22 2025-11-13)
binary: rustc
commit-hash: 2286e5d224b3413484cf4f398a9f078487e7b49d
commit-date: 2025-11-13
host: x86_64-unknown-linux-gnu
release: 1.93.0-nightly
LLVM version: 21.1.5

Additional Labels

No response

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingT-macrosType: Issues with macros and macro expansion

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions