Skip to content

fix: use ptr::add() instead of ptr::offset() in bitfield_unit#3368

Closed
mdz-axo wants to merge 1 commit intorust-lang:mainfrom
mdz-axo:fix/ptr-offset-with-cast
Closed

fix: use ptr::add() instead of ptr::offset() in bitfield_unit#3368
mdz-axo wants to merge 1 commit intorust-lang:mainfrom
mdz-axo:fix/ptr-offset-with-cast

Conversation

@mdz-axo
Copy link
Copy Markdown

@mdz-axo mdz-axo commented May 4, 2026

Summary

Replace .offset(byte_index as isize) with .add(byte_index) in the __BindgenBitfieldUnit template's raw_get_bit and raw_set_bit methods.

Motivation

The byte_index variable is always a non-negative usize (computed as index / 8). Using .offset(byte_index as isize) triggers the clippy::ptr_offset_with_cast lint in all downstream crates that compile the generated bindings.rs with clippy, because the cast from usize to isize is unnecessary and potentially lossy on platforms where usize::MAX > isize::MAX.

Using .add(byte_index) is semantically identical (both advance the pointer by byte_index bytes) but avoids the lint and makes the intent clearer: we are always advancing forward by a non-negative offset.

Changes

  • bindgen/codegen/bitfield_unit.rs: Two instances of .offset(byte_index as isize).add(byte_index) in raw_get_bit and raw_set_bit.

Note: The raw_get, raw_set, raw_get_const, and raw_set_const methods already use .add() correctly — this fix brings the two remaining raw_*_bit methods into alignment.

Replace `.offset(byte_index as isize)` with `.add(byte_index)` in
raw_get_bit and raw_set_bit to silence the `clippy::ptr_offset_with_cast`
lint.

The `byte_index` is always a non-negative `usize` derived from
`index / 8`, so the cast to `isize` is unnecessary and potentially
lossy on platforms where `usize::MAX > isize::MAX`. Using `.add()`
is both clearer and avoids the lint.

This affects code emitted into generated bindings.rs files via the
`__BindgenBitfieldUnit` template.
@emilio
Copy link
Copy Markdown
Contributor

emilio commented May 4, 2026

I think you need to run and fix the tests. Thanks!

@mdz-axo mdz-axo closed this May 4, 2026
@mdz-axo mdz-axo deleted the fix/ptr-offset-with-cast branch May 4, 2026 16:31
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.

2 participants