ITADN

chunks_exact_to_as_chunks suggests invalid use of associated const

#17597Openssanderson 创建于 6 天前
C-bugI-false-positive
S
ssandersoncommented
### Summary Following up on https://github.com/rust-lang/rust-clippy/issues/17359 and https://github.com/rust-lang/rust-clippy/issues/17314, it appears that there are still cases where chunks_exact_to_as_chunks suggests a refactoring that isn't valid on stable rust. It may be that this lint isn't ready to be enabled by default given the number of sharp edges remaining with const evaluation. ### Lint Name chunks_exact_to_as_chunks ### Reproducer I tried this code: ```rust pub trait HasAssociatedConst { const SIZE: usize; } pub fn use_const<T: HasAssociatedConst>(buf: &mut [u8]) { let _ = buf.chunks_exact_mut(T::SIZE); } ``` I saw this happen: ``` warning: using `chunks_exact_mut` with a constant chunk size --> src/lib.rs:6:17 | 6 | let _ = buf.chunks_exact_mut(T::SIZE); | ^^^^^^^^^^^^^^^^^^^^^^^^^ | help: consider using `as_chunks_mut::<T::SIZE>()` instead --> src/lib.rs:6:17 | 6 | let _ = buf.chunks_exact_mut(T::SIZE); | ^^^^^^^^^^^^^^^^^^^^^^^^^ = help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.98.0/index.html#chunks_exact_to_as_chunks = note: `#[warn(clippy::chunks_exact_to_as_chunks)]` on by default warning: `const-generic-repro` (lib) generated 1 warning Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.05s ``` I expected to see this happen: No lint should fire here. The suggestion of `as_chunks_mut<T::SIZE>` isn't valid. It errors with: ``` error[E0747]: type provided when a constant was expected --> src/lib.rs:6:33 | 6 | let _ = buf.as_chunks_mut::<T::SIZE>(); | ^^^^^^^ | help: if this generic argument was intended as a const parameter, surround it with braces | 6 | let _ = buf.as_chunks_mut::<{ T::SIZE }>(); | + + ``` and that suggestion in turn errors with: ``` error: generic parameters may not be used in const operations --> src/lib.rs:6:35 | 6 | let _ = buf.as_chunks_mut::<{ T::SIZE }>(); | ^ cannot perform const operation using `T` | = note: type parameters may not be used in const expressions error: could not compile `const-generic-repro` (lib) due to 1 previous error ``` In general, I believe there's no supported way on stable rust today to invoke `as_chunks_mut` using `T::SIZE`, so the lint shouldn't fire here. ### Version ```text $ rustc -Vvrustc 1.98.0 (88d9e12ae 2026-08-18) binary: rustc commit-hash: 88d9e12ae178fab0fb5cc050a94da85685d449ea commit-date: 2026-08-18 host: x86_64-unknown-linux-gnu release: 1.98.0 LLVM version: 22.1.8 ``` ### Additional Labels _No response_
0 条评论