rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

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

Open matthiaskrgr opened this issue 3 months ago • 3 comments

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

matthiaskrgr avatar Aug 24 '26 18:08 matthiaskrgr

might be related to https://github.com/rust-lang/rust-clippy/pull/16770

matthiaskrgr avatar Aug 24 '26 18:08 matthiaskrgr

@rustbot label +C-bug

CommanderStorm avatar Aug 25 '26 12:08 CommanderStorm

@rustbot claim

kennywillbe avatar Aug 28 '26 23:08 kennywillbe