RS.CLIPPY.NEEDLESS_AS_BYTES

Detect useless calls to `as_bytes()`

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

What it does

It detects useless calls to str::as_bytes() before calling len() or is_empty().

Why is this bad?

The len() and is_empty() methods are also directly available on strings, and they return identical results. In particular, len() on a string returns the number of bytes.

Example

let len = "some string".as_bytes().len();
let b = "some string".as_bytes().is_empty();

Use instead:

let len = "some string".len();
let b = "some string".is_empty();