Skip to content

New lint: windows_const and chunks_const #6580

Open
@camsteffen

Description

@camsteffen
Contributor

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?

Activity

added
good first issueThese issues are a good way to get started with Clippy
L-perfLint: Belongs in the perf lint group
T-middleType: Probably requires verifiying types
on Feb 8, 2021
pitaj

pitaj commented on Apr 2, 2022

@pitaj
Contributor

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

camsteffen

camsteffen commented on Apr 2, 2022

@camsteffen
ContributorAuthor

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

pitaj

pitaj commented on Apr 2, 2022

@pitaj
Contributor

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

camsteffen

camsteffen commented on Apr 2, 2022

@camsteffen
ContributorAuthor

Yeah that's true.

mchodzikiewicz

mchodzikiewicz commented on Mar 25, 2025

@mchodzikiewicz

@rustbot claim

6 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-lintArea: New lintsL-perfLint: Belongs in the perf lint groupT-middleType: Probably requires verifiying typesgood first issueThese issues are a good way to get started with Clippy

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Participants

    @pitaj@camsteffen@mchodzikiewicz@immersum

    Issue actions

      New lint: `windows_const` and `chunks_const` · Issue #6580 · rust-lang/rust-clippy