summaryrefslogtreecommitdiffstats
path: root/src/tools/clippy/src/docs/strlen_on_c_strings.txt
blob: 0454abf55a20481fded0fad25014db3f0f7b121f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
### What it does
Checks for usage of `libc::strlen` on a `CString` or `CStr` value,
and suggest calling `as_bytes().len()` or `to_bytes().len()` respectively instead.

### Why is this bad?
This avoids calling an unsafe `libc` function.
Currently, it also avoids calculating the length.

### Example
```
use std::ffi::CString;
let cstring = CString::new("foo").expect("CString::new failed");
let len = unsafe { libc::strlen(cstring.as_ptr()) };
```
Use instead:
```
use std::ffi::CString;
let cstring = CString::new("foo").expect("CString::new failed");
let len = cstring.as_bytes().len();
```