Skip to content

Fix unnecessary transmute and unnecessary unsafe block warnings on bitfield codegen#3388

Open
armoha wants to merge 3 commits into
rust-lang:mainfrom
armoha:fix_warnings_bitfield_getters_setters
Open

Fix unnecessary transmute and unnecessary unsafe block warnings on bitfield codegen#3388
armoha wants to merge 3 commits into
rust-lang:mainfrom
armoha:fix_warnings_bitfield_getters_setters

Conversation

@armoha

@armoha armoha commented Jul 4, 2026

Copy link
Copy Markdown

Fix #2807 and #3241 (comment)

PR #3335 replaced transmute with as _ in the union branch getter/setter and the non-union setter, and removed the unsafe block from the union branch. It left non-union getter and raw getter, emitting unsafe { transmute(...) } and triggering unnecessary_transmutes warning for integer (#2807). It replaced transmute with val as _ for non-union setter and kept unsafe block which is required for Rust unions (its field access is unsafe) but redundant for the rest of cases (#3241 (comment)).

This PR type-dispatches the non-union getter codegen, and removes unnecessary unsafe block from the non-union setter. It dispatches on bitfield_ty.canonical_type(ctx).kind() instead of bitfield_ty.kind() to resolve Alias/Elaborated wrappers clang applies.

  • integer getter: unsafe { transmute(get_const() as u32) } (unnecessary transmutes) -> get_const() as u32 as _
  • bool getter: unsafe { transmute(get_const() as u8) } -> get_const() as u8 != 0
  • enum getter: unchanged. unsafe { transmute(get_const() as u32) }
  • non-union setter on Rust unions: unchanged. unsafe { let val: u32 = val as _; self._bitfield_1.set_const(val as u64) }
  • non-union setter for the rest of cases: removes unnecessary unsafe block for regular structs. let val: u32 = val as _; self._bitfield_1.set_const(val as u64)

@armoha

armoha commented Jul 5, 2026

Copy link
Copy Markdown
Author

CI fails because libtinfo5_6.3-2ubuntu0.1_arm64.deb no longer exists in https://ports.ubuntu.com/ubuntu-ports/pool/universe/n/ncurses/ and instead it was replaced with 6.3-2ubuntu0.2 on July 2.

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.

Clippy warns of useless_transmute in bitfields' setters and getters

1 participant