RS.CLIPPY.PTR_OFFSET_WITH_CAST
Unneeded pointer offset cast
This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: ptr_offset_with_cast. Copyright ©2025 The Rust Team. All rights reserved.
What it does
Checks for usage of the offset pointer method with a usize casted to an
isize.
Why is this bad?
If we're always increasing the pointer address, we can avoid the numeric
cast by using the add method instead.
Example
let vec = vec![b\'a\', b\'b\', b\'c\'];
let ptr = vec.as_ptr();
let offset = 1_usize;
unsafe {
ptr.offset(offset as isize);
}
Could be written:
let vec = vec![b\'a\', b\'b\', b\'c\'];
let ptr = vec.as_ptr();
let offset = 1_usize;
unsafe {
ptr.add(offset);
}