rust-lang / rust-lang/rust-clippy

New lint: `windows_const` and `chunks_const`

Open
#6,580 11 comments 0 reactions 1 assignee View on GitHub

Nobody has claimed this yet.

A-lint good first issue L-perf T-middle
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Now that min_const_generics is stabilized, I suspect it won't be long for array_windows and array_chunks to be stabilized as well. It would be nice to have a lint ready when that comes. But for now we can check the feature gates.

What it does

Checks for usage of windows or chunks with a constant size. These may be replaced with array_windows and array_chunks respectively.

Categories (optional)
  • Kind: perf

What is the advantage of the recommended code over the original code

It avoids heap allocation and should be more performant. Also it often simplifies the code since you can destructure the array.

Drawbacks

None.

Example
for window in slice.windows(2) {
    println!("{} {}", w[0], w[1]);
}

Could be written as:

for [a, b] in slice.array_windows() {
    println!("{} {}", a, b);
}

Maybe we should avoid linting if the array would be too big?

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.