rust-lang / rust-lang/rust-clippy

FP `clippy::byte_char_slices` : cannot borrow data in a `&` reference as mutable

Open
#17,627 3 comments 0 reactions 1 assignee View on GitHub

@kennywillbe is already working on this.

Since Aug 28, 2026.

C-bug I-suggestion-causes-error
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Using the following flags

--force-warn clippy::byte_char_slices

this code:

pub fn main() {
    unsafe {
        std::str::from_utf8_unchecked_mut(&mut [b'c', b'l', b'i', b'p', b'p', b'y']);
        str::from_utf8_unchecked_mut(&mut [b'c', b'l', b'i', b'p', b'p', b'y']);
    }
}

caused the following diagnostics:

    Checking _a v0.1.0 (/tmp/icemaker_global_tempdir.hdrV5syrK78Q/icemaker_clippyfix_tempdir.tWsRAmMi7qQH/_a)
warning: can be more succinctly written as a byte str
 --> src/main.rs:3:48
  |
3 |         std::str::from_utf8_unchecked_mut(&mut [b'c', b'l', b'i', b'p', b'p', b'y']);
  |                                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `*b"clippy"`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/main/index.html#byte_char_slices
  = note: requested on the command line with `--force-warn clippy::byte-char-slices`

warning: can be more succinctly written as a byte str
 --> src/main.rs:4:43
  |
4 |         str::from_utf8_unchecked_mut(&mut [b'c', b'l', b'i', b'p', b'p', b'y']);
  |                                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `*b"clippy"`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/main/index.html#byte_char_slices

warning: `_a` (bin "_a") generated 2 warnings (run `cargo clippy --fix --bin "_a" -p _a -- -Aclippy::all -Awarnings --force-warn clippy::byte_char_slices --cap-lints warn` to apply 2 suggestions)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.27s

However after applying these diagnostics, the resulting code:

pub fn main() {
    unsafe {
        std::str::from_utf8_unchecked_mut(&mut *b"clippy");
        str::from_utf8_unchecked_mut(&mut *b"clippy");
    }
}

no longer compiled:

    Checking _a v0.1.0 (/tmp/icemaker_global_tempdir.hdrV5syrK78Q/icemaker_clippyfix_tempdir.tWsRAmMi7qQH/_a)
error[E0596]: cannot borrow data in a `&` reference as mutable
 --> src/main.rs:3:43
  |
3 |         std::str::from_utf8_unchecked_mut(&mut *b"clippy");
  |                                           ^^^^^^^^^^^^^^^ cannot borrow as mutable

error[E0596]: cannot borrow data in a `&` reference as mutable
 --> src/main.rs:4:38
  |
4 |         str::from_utf8_unchecked_mut(&mut *b"clippy");
  |                                      ^^^^^^^^^^^^^^^ cannot borrow as mutable

For more information about this error, try `rustc --explain E0596`.
error: could not compile `_a` (bin "_a") due to 2 previous errors
warning: build failed, waiting for other jobs to finish...
error: could not compile `_a` (bin "_a" test) due to 2 previous errors

Version:

rustc 1.100.0-nightly (0a3fa2af3 2026-08-24)
binary: rustc
commit-hash: 0a3fa2af35783dcd1b4206a85fe7811297eea0bf
commit-date: 2026-08-24
host: x86_64-unknown-linux-gnu
release: 1.100.0-nightly
LLVM version: 23.1.0

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.