rust-lang / rust-lang/rust

Consider separate lints for "hierarchical" unused lints (or some way to opt-out)

Open
#142,899 8 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-lints L-dead_code T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Description

Consider the example:

fn unused_foo() {
    used_bar_but_just_here();
}

fn used_bar_but_just_here() {}

In the past (I can't pinpoint when), cargo check would emit a "unused" warning for unused_foo, but not used_bar_but_just_here, like so:

warning: function `unused_foo` is never used
 --> src/main.rs:6:4
  |
6 | fn unused_foo() { used_bar_but_just_here(); }
  |    ^^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

Nowadays, cargo check emits unused warnings for both:

warning: function `unused_foo` is never used
 --> src/main.rs:6:4
  |
6 | fn unused_foo() { used_bar_but_just_here(); }
  |    ^^^^^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: function `used_bar_but_just_here` is never used
 --> src/main.rs:8:4
  |
8 | fn used_bar_but_just_here() {}
  |    ^^^^^^^^^^^^^^^^^^^^^^

The new behavior is really disruptive and annoying, as it marks your entire method "tree" with unused instead of just the root, which litters the codebase with warnings during prototype stages. It also fails to pinpoint the "real culprit".

Another point: As a user, I expect that removing unused items shouldn't cause compiler errors, in the example, cargo check complains that used_bar_but_just_here is never used, yet removing it would cause a compiler error since unused_foo does use that method.


If I may suggest, one of these options would really be appreciated (I'm willing to make the PR myself if any of these is accepted):

  • Revert to the old behavior (I personally can't think of a situation where I would miss the new behavior).
  • Move the new behavior to a different lint (something like unused_chained?) or add that new lint to represent the old behavior.
Version
rustc 1.86.0 (05f9846f8 2025-03-31)
binary: rustc
commit-hash: 05f9846f893b09a1be1fc8560e33fc3c815cfecb
commit-date: 2025-03-31
host: x86_64-unknown-linux-gnu
release: 1.86.0
LLVM version: 19.1.7

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the reported example with cargo check using the rustc 1.86.0 details in the issue, then read the existing dead_code and unused-lint behavior. Compare the proposed revert, separate lint, and opt-out approaches; done means the desired hierarchical warning behavior is agreed and covered by appropriate compiler tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.