rust-bindgen icon indicating copy to clipboard operation
rust-bindgen copied to clipboard

Bitfields cause `unnecessary_transmutes` compiler warning in Rust 1.88

Open FreezyLemon opened this issue 5 months ago • 2 comments

I have a generated struct

#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct AVIndexEntry {
    // ...fields
    pub _bitfield_align_1: [u32; 0],
    pub _bitfield_1: __BindgenBitfieldUnit<[u8; 4usize]>,
    // ...more fields
}

from the corresponding C header

typedef struct AVIndexEntry {
    // ...fields
    int flags:2;
    int size:30;
    // ...more fields
} AVIndexEntry;

The generated Rust implementation to read/write these bitfields uses transmute, but since 1.87 cast_unsigned/cast_signed exist and it looks like not using them creates a compiler warning in 1.88.

Generated code:

impl AVIndexEntry {
    #[inline]
    pub fn flags(&self) -> libc::c_int {
        unsafe { ::std::mem::transmute(self._bitfield_1.get(0usize, 2u8) as u32) }
    }
}

Resulting compiler error:

warning: unnecessary transmute
     --> /home/user/<...>/out/bindings.rs:15519:18
      |
15519 | ... { ::std::mem::transmute(self._bitfield_1.get(0usize, 2u8) as u32) }
      |       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace this with: `u32::cast_signed(self._bitfield_1.get(0usize, 2u8) as u32)`

FreezyLemon avatar Jun 27 '25 09:06 FreezyLemon

  • this is related to https://github.com/rust-lang/rust-bindgen/issues/2807

strohel avatar Sep 20 '25 21:09 strohel

Is there a way to silence this warning? Edit: #[allow(unnecessary_transmutes)] works, I was trying without the s :(.

jschwe avatar Nov 12 '25 20:11 jschwe