rust-lang / rust-lang/rust-clippy
Suggest array_chunks usage
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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