rust-lang / rust-lang/rust-clippy

`chunks_exact_to_as_chunks` MachineApplicable suggestion doesn't compile

Open
#17,515 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Summary

chunks_exact_to_as_chunks suggests as_chunks::<N>().0.iter() as MachineApplicable in certain scenarios. The suggested iterator yields &[T; N], while the original yielded &[T], so if something downstream requires &[T], then the suggestion doesn't compile. As a result, cargo clippy --fix would break the build.

Occurs on master (6b4d9fd7b). As of writing, the lint is not yet in stable clippy.

Lint Name

chunks_exact_to_as_chunks

Reproducer

Note: I reproduced this on master (6b4d9fd7b) via cargo uibless and cargo dev lint.

I tried this code:

fn main() {
    let slice: &[u8] = &[1, 2, 3, 4, 5, 6, 7, 8];
    let chunks = slice.chunks_exact(4).collect::<Vec<&[u8]>>();
    println!("{chunks:?}");
}

I saw this happen:

warning: using `chunks_exact` with a constant chunk size
 --> reproducer.rs:3:24
  |
3 |     let chunks = slice.chunks_exact(4).collect::<Vec<&[u8]>>();
  |                        ^^^^^^^^^^^^^^^ help: consider using `as_chunks` instead: `as_chunks::<4>().0.iter()`

Applying the machine-applicable fix (reproduced via cargo uibless) causes compilation to fail:

error[E0277]: a value of type `Vec<&[u8]>` cannot be built from an iterator over elements of type `&[u8; 4]`
    --> reproducer-fixed.rs:3:60
     |
   3 |     let chunks = slice.as_chunks::<4>().0.iter().collect::<Vec<&[u8]>>();
     |                                                  -------   ^^^^^^^^^^ value of type `Vec<&[u8]>` cannot be built from `std::iter::Iterator<Item=&[u8; 4]>`
     |                                                  |
     |                                                  required by a bound introduced by this call
     |
help: the trait `FromIterator<&[u8; 4]>` is not implemented for `Vec<&[u8]>`
      but trait `FromIterator<&[u8]>` is implemented for it

I expected either of these to happen:

  • (a) no machine-applicable suggestion (since it doesn't compile)
  • (b) suggesting as_chunks::<4>().0.iter().map(AsRef::as_ref) (since it compiles)

❗ Other examples that trigger a machine-applicable suggestions which also don't compile:

fn main() {
    fn function_and_closure_param_type(slice: &[u8]) {
        let _ = slice.chunks_exact(8).map(std::str::from_utf8); // <- `from_utf` expects `&[u8]`, suggestion yields `&[u8; 8]`
        slice.chunks_exact(2).for_each(|x: &[u8]| println!("{x:?}")); // <- closure expects `&[u8], suggestion yields `&[u8; 2]`
    }

    fn collect_to_vec_and_return_type(slice: &[u8]) -> Vec<&[u8]> {
        let mut out = Vec::new();
        for chunk in slice.chunks_exact(2) {
            out.push(chunk); // <- this causes `out` to become `Vec<&[u8; 2]>`
        }
        out //  `Vec<&[u8; 2]>` isn't coerced to `Vec<&[u8]>`
    }
}
Version
rustc 1.99.0-nightly (ad3d0bc14 2026-07-31)
binary: rustc
commit-hash: ad3d0bc141a02cf446e384136d250a1f6950fed5
commit-date: 2026-07-31
host: aarch64-apple-darwin
release: 1.99.0-nightly
LLVM version: 22.1.8
Additional Labels

@rustbot label +I-suggestion-causes-error

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.

Research direction

Start by locating the chunks_exact_to_as_chunks lint implementation and its existing checks, then run the reproducer with cargo uibless and cargo dev lint. The fix is done when the lint no longer emits a machine-applicable suggestion that changes &[T] into &[T; N] incompatibly, while valid suggestions still work.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.