RS.CLIPPY.STRLEN_ON_C_STRINGS

Using `libc::strlen` on a `CString` or `CStr` value, while `as_bytes().len()` or `to_bytes().len()` respectively can be used instead

This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: strlen_on_c_strings. Copyright ©2025 The Rust Team. All rights reserved.

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();