]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/src/docs/ptr_offset_with_cast.txt
:arrow_up: rust-analyzer
[rust.git] / src / tools / clippy / src / docs / ptr_offset_with_cast.txt
1 ### What it does
2 Checks for usage of the `offset` pointer method with a `usize` casted to an
3 `isize`.
4
5 ### Why is this bad?
6 If we’re always increasing the pointer address, we can avoid the numeric
7 cast by using the `add` method instead.
8
9 ### Example
10 ```
11 let vec = vec![b'a', b'b', b'c'];
12 let ptr = vec.as_ptr();
13 let offset = 1_usize;
14
15 unsafe {
16     ptr.offset(offset as isize);
17 }
18 ```
19
20 Could be written:
21
22 ```
23 let vec = vec![b'a', b'b', b'c'];
24 let ptr = vec.as_ptr();
25 let offset = 1_usize;
26
27 unsafe {
28     ptr.add(offset);
29 }
30 ```