diff --git a/compiler/rustc_passes/src/stability.rs b/compiler/rustc_passes/src/stability.rs index ec6d48cbea2e4..18263506a03b2 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 @@ pub fn check_unused_or_stable_features(tcx: TyCtxt<'_>) { 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 0000000000000..d27be14b86d5a --- /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); +} diff --git a/tests/ui/lint/unused-features/unused-library-features.rs b/tests/ui/lint/unused-features/unused-library-features.rs index 75afd32e56cb3..2f53ab66b160c 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 e259058d6c33b..bf71e269e2f0f 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