From 2c7690e1e6b18ebf51e84e4b08a35903cee64f6b Mon Sep 17 00:00:00 2001 From: mu001999 Date: Mon, 9 Mar 2026 23:36:26 +0800 Subject: [PATCH 1/2] Mark stable features as used to avoid trigger unused_features --- compiler/rustc_passes/src/stability.rs | 6 ++++++ .../ui/lint/unused-features/stable-features.rs | 17 +++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 tests/ui/lint/unused-features/stable-features.rs diff --git a/compiler/rustc_passes/src/stability.rs b/compiler/rustc_passes/src/stability.rs index ec6d48cbea2e..18263506a03b 100644 --- a/compiler/rustc_passes/src/stability.rs +++ b/compiler/rustc_passes/src/stability.rs @@ -964,6 +964,9 @@ pub fn check_unused_or_stable_features(tcx: TyCtxt<'_>) { let mut lang_features = UnordSet::default(); for EnabledLangFeature { gate_name, attr_sp, stable_since } in enabled_lang_features { if let Some(version) = stable_since { + // Mark the feature as enabled, to ensure that it is not marked as unused. + let _ = tcx.features().enabled(*gate_name); + // Warn if the user has enabled an already-stable lang feature. unnecessary_stable_feature_lint(tcx, *attr_sp, *gate_name, *version); } @@ -1021,6 +1024,9 @@ fn check_features<'tcx>( if let FeatureStability::AcceptedSince(since) = stability && let Some(span) = remaining_lib_features.get(&feature) { + // Mark the feature as enabled, to ensure that it is not marked as unused. + let _ = tcx.features().enabled(feature); + // Warn if the user has enabled an already-stable lib feature. if let Some(implies) = all_implications.get(&feature) { unnecessary_partially_stable_feature_lint(tcx, *span, feature, *implies, since); diff --git a/tests/ui/lint/unused-features/stable-features.rs b/tests/ui/lint/unused-features/stable-features.rs new file mode 100644 index 000000000000..d27be14b86d5 --- /dev/null +++ b/tests/ui/lint/unused-features/stable-features.rs @@ -0,0 +1,17 @@ +//@ check-pass + +#![deny(warnings)] +#![allow(stable_features)] + +// Lang feature +#![feature(lint_reasons)] + +// Lib feature +#![feature(euclidean_division)] + +#[allow(unused_variables, reason = "my reason")] +fn main() { + let x = (); + + let _ = 42.0_f32.div_euclid(3.0); +} From dfd5e905c5f26a2ae9682468b1c30a6ccd60cc20 Mon Sep 17 00:00:00 2001 From: mu001999 Date: Mon, 9 Mar 2026 23:38:30 +0800 Subject: [PATCH 2/2] Bless other tests --- .../ui/lint/unused-features/unused-library-features.rs | 3 +-- .../unused-features/unused-library-features.stderr | 10 ++-------- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/tests/ui/lint/unused-features/unused-library-features.rs b/tests/ui/lint/unused-features/unused-library-features.rs index 75afd32e56cb..2f53ab66b160 100644 --- a/tests/ui/lint/unused-features/unused-library-features.rs +++ b/tests/ui/lint/unused-features/unused-library-features.rs @@ -5,8 +5,7 @@ #![feature(step_trait)] //~^ ERROR feature `step_trait` is declared but not used #![feature(is_sorted)] -//~^ ERROR feature `is_sorted` is declared but not used -//~^^ WARN the feature `is_sorted` has been stable since 1.82.0 and no longer requires an attribute to enable +//~^ WARN the feature `is_sorted` has been stable since 1.82.0 and no longer requires an attribute to enable // Enabled via cfg_attr, unused #![cfg_attr(all(), feature(slice_ptr_get))] diff --git a/tests/ui/lint/unused-features/unused-library-features.stderr b/tests/ui/lint/unused-features/unused-library-features.stderr index e259058d6c33..bf71e269e2f0 100644 --- a/tests/ui/lint/unused-features/unused-library-features.stderr +++ b/tests/ui/lint/unused-features/unused-library-features.stderr @@ -18,17 +18,11 @@ note: the lint level is defined here LL | #![deny(unused_features)] | ^^^^^^^^^^^^^^^ -error: feature `is_sorted` is declared but not used - --> $DIR/unused-library-features.rs:7:12 - | -LL | #![feature(is_sorted)] - | ^^^^^^^^^ - error: feature `slice_ptr_get` is declared but not used - --> $DIR/unused-library-features.rs:12:28 + --> $DIR/unused-library-features.rs:11:28 | LL | #![cfg_attr(all(), feature(slice_ptr_get))] | ^^^^^^^^^^^^^ -error: aborting due to 3 previous errors; 1 warning emitted +error: aborting due to 2 previous errors; 1 warning emitted