Rollup merge of #153610 - mu001999-contrib:fix/unused-stable-features, r=JonathanBrouwer

Only lint unused features if they are unstable

Fixes rust-lang/rust#153523
This commit is contained in:
Matthias Krüger
2026-03-09 23:47:03 +01:00
committed by GitHub
4 changed files with 26 additions and 10 deletions
+6
View File
@@ -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);
@@ -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);
}
@@ -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))]
@@ -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