fix: do not machine-apply chunks_exact_to_as_chunks rewrites - #17642
fix: do not machine-apply chunks_exact_to_as_chunks rewrites#17642cestercian wants to merge 3 commits into
Conversation
The suggestion rewrites `slice.chunks_exact(N)` to `as_chunks::<N>()`, which yields `&[T; N]` instead of `&[T]`. Machine-applying that via `cargo clippy --fix` breaks builds when the item type is pinned, e.g. `collect::<Vec<&[T]>>()`, `map(from_utf8)`, or typed closures. Downgrade both span_suggestion arms to MaybeIncorrect (keeping HasPlaceholders when the snippet already required it). Co-authored-by: Cestercian <yashafaid@gmail.com>
|
Thanks for the pull request, and welcome! You should hear from one of our reviewers after this PR gets at least 2 reviews from the community. Please see the contribution instructions for more information. |
Clippy's lint-attributes-organization test requires allow-list lint names in alphabetical order. Co-authored-by: Cestercian <yashafaid@gmail.com>
| //@no-rustfix: rewriting `chunks_exact` to `as_chunks` changes `Item` from `&[T]` to `&[T; N]` | ||
| #![warn(clippy::chunks_exact_to_as_chunks)] | ||
| #![allow(unused, clippy::redundant_closure_call)] | ||
| #![allow(unused, clippy::deref_addrof, clippy::redundant_closure_call)] |
There was a problem hiding this comment.
please use expect(..) here and since it looks like deref_addrof is just one or maybe two items expect that on the item level instead.
The added expect gives us more signal for free if something breaks 😉
There was a problem hiding this comment.
Done in 8302184: crate-level #![allow] is only unused now. redundant_closure_call is #[expect] on that site. Dropped deref_addrof from the allow list (wasn't needed on this file).
There was a problem hiding this comment.
Nopey also change the crate level allow to an expect. Let's leave the campsite cleaner than we found it
| let _ = slice.chunks_exact(size_of::<T>()); | ||
| } | ||
|
|
||
| // Suggestion text for for-loop / iterator-method use sites (not rustfix-applied: Item type |
There was a problem hiding this comment.
please move the new tests to an mod issueXYZ to have these properly orderd.
Ideally, with one function inside per higher level invariant to not have a long long list that is hard to scan 😉
There was a problem hiding this comment.
Done: issue 17515 repros are in mod issue17515 with one function per invariant (collect_vec_slice, map_from_utf8, typed_for_each, for_loop_into_vec).
| // `as_chunks` yields `&[T; N]`, not `&[T]`. Downstream uses that pin the item | ||
| // type (e.g. `collect::<Vec<&[T]>>()`, `map(from_utf8)`) would fail after `--fix`. | ||
| if applicability != Applicability::HasPlaceholders { | ||
| applicability = Applicability::MaybeIncorrect; | ||
| } |
There was a problem hiding this comment.
there is no way to typecheck our way to just MaybeIncorrect in the actual case? 🤔
There was a problem hiding this comment.
Looked at this — keeping blanket MaybeIncorrect (option B).
is_ty_unified only sees when the iterator type must unify (e.g. if/else arms), not Item pinning. A local check at the immediate use_expr could catch some collect / map / typed-closure pins, but:
- a
forbinding is alwaysChunksExact::Item(&[T]) today, so that does not tell us whether later uses accept&[T; N] - the pin can sit arbitrarily far downstream
Restoring MachineApplicable for “simple” for _ in / .take(2) would still let --fix break the issue 17515 for-loop repro. Proving acceptance at arbitrary downstream sites isn’t realistic with the current helpers, so the suggestions stay visible but not auto-applied.
There was a problem hiding this comment.
Not really an answer. Why is extending the infra not a better call?
Keep blanket MaybeIncorrect (option B). `is_ty_unified` only sees iterator-type unification, not Item pinning. A local check at the immediate use_expr could spot some collect/map/typed-closure pins, but the for-loop binding is always ChunksExact::Item (`&[T]`) and the pin can be arbitrarily far downstream, so restoring MachineApplicable for `for _ in` / `.take(2)` would still let `--fix` break the issue 17515 for-loop repro. Crate-level allow is only `unused`; `redundant_closure_call` is expected on that site. Issue 17515 repros live in `mod issue17515` with one function per invariant. Co-authored-by: Cestercian <yashafaid@gmail.com>
|
Also please review https://blog.rust-lang.org/inside-rust/2026/08/05/rust-langrust-is-adopting-an-llm-policy/ given that 8302184 and the previous commit was done using cursor |
thanks for the heads up 👀 I disclosed the use of AI in the PR description itself if there's anything else I need todo please let me know |
You... did not explain how it was used- Could you give us some details? (ㅇㅅㅇ❀) |
Cursor wrote this patch and the follow-up commits. I reviewed the diff, including the MaybeIncorrect choice (
is_ty_unifieddoes not see Item pinning), the test split intomod issue17515, and the UI tests.chunks_exact/chunks_exact_mutyield&[T], but the suggestedas_chunks::<N>()/as_chunks_mut::<N>()rewrite yields&[T; N]. Bothspan_suggestionarms usedApplicability::MachineApplicable, socargo clippy --fixrewrote call sites whose downstream uses pinItemto&[T]and left the crate unable to compile.Fully proving that a use site can accept
&[T; N]is not realistic (get_expr_use_site/is_ty_unifiedalready skip some if/else cases, but notcollect,map, typed closures, or return types). The suggestions are thereforeMaybeIncorrectso they still appear, but are not auto-applied.Changes
span_suggestionarms inchunks_exact_to_as_chunkstoApplicability::MaybeIncorrect, unless the snippet already forcedHasPlaceholders.map(AsRef::as_ref)wrapper//@no-rustfix) and add the issue 17515 repros (collect::<Vec<&[u8]>>(),map(from_utf8), typedfor_each, for-loop intoVec<&[u8]>)Related Issue
Fixes #17515
Testing
changelog: [
chunks_exact_to_as_chunks]: do not machine-apply suggestions, since the rewrite changesItemfrom&[T]to&[T; N]