Skip to content

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

Merged
emilio merged 2 commits into
rust-lang:mainfrom
armoha:fix_warnings_bitfield_getters_setters
Jul 6, 2026
Merged

Fix unnecessary transmute and unnecessary unsafe block warnings on bitfield codegen#3388
emilio merged 2 commits into
rust-lang:mainfrom
armoha:fix_warnings_bitfield_getters_setters

Conversation

@armoha

@armoha armoha commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

Fixes #2807 , Fixes #3355 (=#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 (#3355).

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

This comment was marked as resolved.

@armoha armoha force-pushed the fix_warnings_bitfield_getters_setters branch from 41d933d to 872606a Compare July 6, 2026 15:17
@rustbot

rustbot commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@armoha armoha force-pushed the fix_warnings_bitfield_getters_setters branch from 872606a to f12117a Compare July 6, 2026 15:50
@armoha

armoha commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

header_issue_544_stylo_creduce_2_hpp and nsBaseHashtable tests were overwritten accidentally, probably due to LLVM versions and AST output mismatch on template parameter. (I tested with clang version 21.1.8)

r? @emilio

@emilio emilio added this pull request to the merge queue Jul 6, 2026
Merged via the queue into rust-lang:main with commit 7acba9a Jul 6, 2026
51 checks passed
@armoha armoha deleted the fix_warnings_bitfield_getters_setters branch July 6, 2026 16:42
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.

Bindgen generates unnecessary unsafe blocks Clippy warns of useless_transmute in bitfields' setters and getters

3 participants