rust-lang/rust-clippy

New lint: `windows_const` and `chunks_const`

Open

#6 580 ouverte le 12 janv. 2021

Voir sur GitHub
 (7 commentaires) (0 réactions) (1 assigné)Rust (1 391 forks)batch import
A-lintL-perfT-middlegood first issue

Métriques du dépôt

Stars
 (10 406 stars)
Métriques de merge PR
 (Merge moyen 19j 22h) (113 PRs mergées en 30 j)

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?

Guide contributeur