fix: use ptr::add() instead of ptr::offset() in bitfield_unit#3368
Closed
mdz-axo wants to merge 1 commit intorust-lang:mainfrom
Closed
fix: use ptr::add() instead of ptr::offset() in bitfield_unit#3368mdz-axo wants to merge 1 commit intorust-lang:mainfrom
mdz-axo wants to merge 1 commit intorust-lang:mainfrom
Conversation
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
approved these changes
May 4, 2026
Contributor
|
I think you need to run and fix the tests. Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Replace
.offset(byte_index as isize)with.add(byte_index)in the__BindgenBitfieldUnittemplate'sraw_get_bitandraw_set_bitmethods.Motivation
The
byte_indexvariable is always a non-negativeusize(computed asindex / 8). Using.offset(byte_index as isize)triggers theclippy::ptr_offset_with_castlint in all downstream crates that compile the generatedbindings.rswith clippy, because the cast fromusizetoisizeis unnecessary and potentially lossy on platforms whereusize::MAX > isize::MAX.Using
.add(byte_index)is semantically identical (both advance the pointer bybyte_indexbytes) 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)inraw_get_bitandraw_set_bit.Note: The
raw_get,raw_set,raw_get_const, andraw_set_constmethods already use.add()correctly — this fix brings the two remainingraw_*_bitmethods into alignment.