rust-lang / rust-lang/rust-clippy

Suggest array_chunks usage

Open
#8,039 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

This function is part of a basic (reference) implementation of a crypto algorithm:

fn words_from_little_endian_bytes(bytes: &[u8], words: &mut [u32]) {
    for (bytes_block, word) in bytes.chunks_exact(4).zip(words.iter_mut()) {
        *word = u32::from_le_bytes(bytes_block.try_into().unwrap());
    }
}

From:
https://github.com/BLAKE3-team/BLAKE3/blob/master/reference_impl/reference_impl.rs

Once array_chunks is stable I'd like Clippy to suggest better code like this:

#![feature(array_chunks)]

fn words_from_little_endian_bytes(bytes: &[u8], words: &mut [u32]) {
    for (bytes_block, word) in bytes.array_chunks::<4>().zip(words.iter_mut()) {
        *word = u32::from_le_bytes(*bytes_block);
    }
}

Eventually another Clippy link should help write code like this, without the iter_mut:

#![feature(array_chunks)]

fn words_from_little_endian_bytes(bytes: &[u8], words: &mut [u32]) {
    for (&bytes_block, word) in bytes.array_chunks::<4>().zip(words) {
        *word = u32::from_le_bytes(bytes_block);
    }
}

In my opinion a better version of this function should be:

fn words_from_little_endian_bytes(bytes: &[u8], words: &mut [u32]) {
    assert_eq!(words.len(), bytes.len() * 4);
    for (word, &bytes_block) in zip(words, bytes.array_chunks::<4>()) {
        *word = u32::from_le_bytes(bytes_block);
    }
}

But this is beyond the scope of Clippy.

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 with the linked BLAKE3 reference implementation and the array_chunks examples in the issue; no repository files or tests are named. Identify the relevant Clippy lint entry point and test location, then define completion as a lint that can recognize the shown pattern and suggest array_chunks usage once the API is stable.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.