Skip to content

WIP: Lint unused features#152164

Draft
mu001999 wants to merge 3 commits intorust-lang:mainfrom
mu001999-contrib:lint/unused_features
Draft

WIP: Lint unused features#152164
mu001999 wants to merge 3 commits intorust-lang:mainfrom
mu001999-contrib:lint/unused_features

Conversation

@mu001999
Copy link
Contributor

@mu001999 mu001999 commented Feb 5, 2026

Fixes #44232
Fixes #151752

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 5, 2026
@rust-log-analyzer

This comment has been minimized.

Copy link
Member

@jyn514 jyn514 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is neat! in the future, it would be easier to review if you split the rename into a separate commit from the change in behavior.

could you add some tests for this please?

View changes since this review

@mu001999 mu001999 force-pushed the lint/unused_features branch from a3c0247 to cb9b84c Compare February 5, 2026 15:25
@mu001999 mu001999 force-pushed the lint/unused_features branch from cb9b84c to cd83e12 Compare February 5, 2026 15:31
@mu001999
Copy link
Contributor Author

mu001999 commented Feb 5, 2026

Initial implementation completed. I plan to add more tests and check whether features in rustc are reported as unused correctly or not.

Comment on lines +44 to +100
pub fn unused_features(&self) -> Vec<(Symbol, Span)> {
let used_features = USED_FEATURES.lock().unwrap();
self.enabled_features_iter_stable_order()
.filter(|(f, _)| !used_features.contains(f))
.collect()
}

/// Is the given feature enabled (via `#[feature(...)]`)?
pub fn enabled(&self, feature: Symbol) -> bool {
USED_FEATURES.lock().unwrap().insert(feature);
self.features.enabled(feature)
}

/// Returns a list of [`EnabledLangFeature`] with info about:
///
/// - Feature gate name.
/// - The span of the `#[feature]` attribute.
/// - For stable language features, version info for when it was stabilized.
pub fn enabled_lang_features(&self) -> &Vec<EnabledLangFeature> {
self.features.enabled_lang_features()
}

pub fn enabled_lib_features(&self) -> &Vec<EnabledLibFeature> {
self.features.enabled_lib_features()
}

pub fn enabled_features(&self) -> &FxHashSet<Symbol> {
&self.features.enabled_features()
}

/// Returns a iterator of enabled features in stable order.
pub fn enabled_features_iter_stable_order(
&self,
) -> impl Iterator<Item = (Symbol, Span)> + Clone {
self.features.enabled_features_iter_stable_order()
}

pub fn dump_feature_usage_metrics(
&self,
metrics_path: PathBuf,
) -> Result<(), Box<dyn std::error::Error>> {
self.features.dump_feature_usage_metrics(metrics_path)
}

/// Some features are known to be incomplete and using them is likely to have
/// unanticipated results, such as compiler crashes. We warn the user about these
/// to alert them.
pub fn incomplete(&self, feature: Symbol) -> bool {
self.features.incomplete(feature)
}

/// Some features are internal to the compiler and standard library and should not
/// be used in normal projects. We warn the user about these to alert them.
pub fn internal(&self, feature: Symbol) -> bool {
self.features.internal(feature)
}
}
Copy link
Member

@jyn514 jyn514 Feb 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think for incremental soundness, none of these can be used outside the query system, right? maybe document that?

@rust-log-analyzer
Copy link
Collaborator

The job aarch64-gnu-llvm-20-1 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
##[group]Runner Image Provisioner
Hosted Compute Agent
Version: 20260123.484
Commit: 6bd6555ca37d84114959e1c76d2c01448ff61c5d
Build Date: 2026-01-23T19:41:17Z
Worker ID: {afd8edb8-0350-47b3-b837-eef7332e6984}
Azure Region: southcentralus
##[endgroup]
##[group]VM Image
- OS: Linux (arm64)
- Source: Partner
- Name: Ubuntu 24.04 by Arm Limited
---
    |
206 | #![feature(riscv_target_feature)]
    |            ^^^^^^^^^^^^^^^^^^^^

warning: feature `rtm_target_feature` is declared but not used
   --> library/core/src/lib.rs:207:12
    |
207 | #![feature(rtm_target_feature)]
    |            ^^^^^^^^^^^^^^^^^^

warning: feature `s390x_target_feature` is declared but not used
   --> library/core/src/lib.rs:208:12
    |
---
   Compiling unwind v0.0.0 (/checkout/library/unwind)
warning: feature `libc` is declared but not used
 --> library/unwind/src/lib.rs:7:47
  |
7 | #![cfg_attr(not(target_env = "msvc"), feature(libc))]
  |                                               ^^^^
  |
  = note: `#[warn(unused_features)]` (part of `#[warn(unused)]`) on by default

[RUSTC-TIMING] unwind test:false 0.070
---

warning: feature `string_replace_in_place` is declared but not used
  --> library/alloc/src/lib.rs:88:50
   |
88 | #![cfg_attr(not(no_global_oom_handling), feature(string_replace_in_place))]
   |                                                  ^^^^^^^^^^^^^^^^^^^^^^^

warning: feature `assert_matches` is declared but not used
  --> library/alloc/src/lib.rs:92:12
   |

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rustc could warn if a feature is unused Re-enable detection of unused library #![feature] directives

4 participants