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

New lint: `windows_const` and `chunks_const`

Open camsteffen opened this issue 4 years ago • 6 comments

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?

camsteffen avatar Jan 12 '21 18:01 camsteffen

How can the array be "too big"? Don't array_windows and array_chunks always return a reference into already-allocated memory?

pitaj avatar Apr 02 '22 18:04 pitaj

"too big" because it's not practical to write out 100 array elements.

camsteffen avatar Apr 02 '22 21:04 camsteffen

You could still use the array like you would a slice though, right?

pitaj avatar Apr 02 '22 22:04 pitaj

Yeah that's true.

camsteffen avatar Apr 02 '22 23:04 camsteffen

@rustbot claim

mchodzikiewicz avatar Mar 25 '25 14:03 mchodzikiewicz

@rustbot claim

ghost avatar Jun 20 '25 05:06 ghost