Fix missing_const_for_thread_local false positive on targets without native #[thread_local] - #17567
Conversation
On targets without native #[thread_local], std's thread_local! now expands a const initializer into a plain non-const init fn, so the is_const_fn guard from rust-lang#12276 no longer filters it and the lint fires on initializers that are already const. Skip ExprKind::ConstBlock initializers: for those there is never anything to suggest. Fixes rust-lang#17566
|
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. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
|
|
So do we have an testing gap? |
|
Added as I could not run the UI suite locally to confirm it: rustup-managed Rust binaries are blocked by an Application Control policy on this machine, so |
On targets without native
#[thread_local](notarget_thread_localcfg — e.g.x86_64-pc-windows-gnu), current nightlies expand aconstthread-local initializer into a plain non-const#[inline] fn __rust_std_internal_init_fn(). The lint's backend guard (!cx.tcx.is_const_fn(defid), from #12276) assumed the generated init fn isconstwhenever the initializer is, so these fns are no longer filtered out:is_min_const_fnapproves the body and the lint fires on initializers that are alreadyconst, spanning the wholethread_local!invocation. Reproducer and details: #17566.The fix adds one guard: skip when the initializer expression is
ExprKind::ConstBlock— for an already-constinitializer there is never anything to suggest, on any backend. The existingis_const_fncheck is deliberately left in place for backends whose generated init fn is stillconst.Verified on
x86_64-pc-windows-gnu, the affected host (no Clippy CI host lackstarget_thread_local, so CI cannot observe the bug or the fix):cargo test --test compile-test -- missing_const_for_thread_localfails with 8 errors against the blessed 6 (extra whole-block spans on the already-conststatics), andcargo test --test dogfoodfails onclippy_utils/src/macros.rs:205(could not compile clippy_utils)tests/ui/missing_const_for_thread_local.rs ... ok,tests/ui/missing_const_for_thread_local.fixed ... ok, dogfood lints all crates cleanx86_64-pc-windows-msvc/Linux output is unchanged by construction: the new arm only rejectsConstBlockinitializers, which never linted thereNo ui-test change is included: the blessed
.stderralready encodes the correct behavior, and no CI target can exercise the affected expansion path.fixes #17566
changelog: [
missing_const_for_thread_local]: fix false positive on targets without native#[thread_local]where the initializer is alreadyconst