rust-lang / rust-lang/rust

Compiler fails to mark main function as dead code in integration test

Open
#149,176 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Code
fn foobar() {
    panic!();
}

fn main() {
    foobar();
}
Current output
Finished `test` profile [unoptimized + debuginfo] target(s) in 0.15s
Desired output
warning: function `foobar` is never used
 --> tests/foo.rs:1:4
  |
1 | fn foobar() {
  |    ^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: function `main` is never used
 --> tests/foo.rs:5:4
  |
5 | fn main() {
  |    ^^^^^

warning: `test-rust-test` (test "foo") generated 2 warnings
Rationale and extra context

In an integration tests (such as, by default, the files in a workspace's tests/ folder), the main function is usually replaced by the standard test harness.

When that happens, the main function should be marked as dead code, or more specifically, it shouldn't be included in the roots during liveness analysis.

(This is obviously different in cases where e.g. harness = false)

Rust Version
rustc 1.90.0 (1159e78c4 2025-09-14)
binary: rustc
commit-hash: 1159e78c4747b02ef996e55082b704c09b970588
commit-date: 2025-09-14
host: x86_64-unknown-linux-gnu
release: 1.90.0
LLVM version: 20.1.8

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 warning difference using the integration test example in tests/foo.rs and the reported rustc version. Trace how the standard test harness treats main during liveness analysis, then verify that warnings appear for both foobar and main while harness = false retains its normal behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers, testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.