Skip to content

Bitfields cause unnecessary_transmutes compiler warning in Rust 1.88 #3241

Open
@FreezyLemon

Description

@FreezyLemon

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)`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions