summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/src/docs/ptr_offset_with_cast.txt
blob: f204e769bf4b8ca3a228a8396e45d705cef71ade (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
### 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);
}
```