New lint: const_size_windows #17440
Open
DanBondarenko wants to merge 2 commits into
Open
Conversation
DanBondarenko
force-pushed
the
const_size_windows
branch
from
July 21, 2026 17:01
62e3e4a to
52555d6
Compare
DanBondarenko
marked this pull request as ready for review
July 22, 2026 13:09
This comment has been minimized.
This comment has been minimized.
DanBondarenko
force-pushed
the
const_size_windows
branch
from
July 22, 2026 13:12
52555d6 to
33160a2
Compare
|
No changes for ba79030 |
Gri-ffin
reviewed
Jul 22, 2026
Comment on lines
+215
to
+220
| fn is_array_windows_shadowed<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool { | ||
| cx.tcx | ||
| .all_traits_including_private() | ||
| .filter(|&trait_def_id| trait_declares_array_windows(cx, trait_def_id)) | ||
| .any(|trait_def_id| implements_trait(cx, ty, trait_def_id, &[])) | ||
| } |
Contributor
There was a problem hiding this comment.
Why do we need to check all traits, even private ones here?
Author
There was a problem hiding this comment.
@Gri-ffin Well spotted! Checking for private traits here was overkill – I changed this to TyCtxt::visible_traits in the a newer commit.
I'll also shortly describe the reason for checking traits in general:
- This lint has the ambition of providing a
MachineApplicablesuggestion when possible. - Potential source of invalid suggestions is the receiver implementing another trait with a
array_windowsmethod; in which case a naivewindows->array_windowsreplacement may cause the method to resolve to the trait implementation instead of theslice::array_windowsmethod. - To this end, I added checks to detect such ambiguities; when detected, UFCS is used in the replacement snippet:
<[_]>::array_windows::<N>(&recv)instead ofrecv.array_windows::<N>().
DanBondarenko
force-pushed
the
const_size_windows
branch
from
July 23, 2026 03:30
33160a2 to
7b5b2f4
Compare
This comment has been minimized.
This comment has been minimized.
DanBondarenko
force-pushed
the
const_size_windows
branch
from
July 23, 2026 03:57
7b5b2f4 to
607b921
Compare
This comment has been minimized.
This comment has been minimized.
I replaced const-size usages of `slice::windows` with `slice::array_windows` in the codebase. However, I refrained from replacing usages in the input files for UI tests, instead allowing violations via #![allow(clippy::const_size_windows)].
DanBondarenko
force-pushed
the
const_size_windows
branch
from
July 24, 2026 13:01
607b921 to
ba79030
Compare
Collaborator
|
This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
See #6580
This adds a new lint
const_size_windows(currently registered understyle). This lint checks for usage ofslice::windowswith a constant window size that can be replaced withslice::array_windows, enabling array destructuring.This lint verifies that:
windowsmethod, specifically the implementation on theslicetype, has been called.sizeargument, passed to the method, is const-evaluatable.array_windowswas stabilized [2].This lint has code suggestion behaviour:
MachineApplicablecode with the source-equivalentslice::array_windowscall.array_windowshas been implemented for another trait on the receiver, suggesting UFCS in those cases.sizeis a complex const requiring surrounding braces as a generic const parameter in the suggestion.This lint offers an additional note alongside the suggestion:
slice::array_windowscall.size = 2destructures to[left, right],size = 3to[x, y, z]etc. This liberates us from specifying the generic const parameter specification in the majority of cases.Limitations of the lint:
slice::chunkswith const size. Thechunksmethod does not have a clean equivalent transformation sinceas_chunkshas a different return type [3].Open question: Should this be a
perflint or astylelint?windows_constandchunks_const#6580 suggests this as aperflint.slice::array_windowstoslice::windows.slice::array_windowsmention optimization [1]:changelog: new lint: [
const_size_windows].stderrfile): 29 positive test cases, 19 negative test casescargo testpasses locallycargo dev update_lintscargo dev fmt