Skip to content

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

@PoignardAzur

Description

@PoignardAzur

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-libtestArea: `#[test]` / the `test` libraryA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-enhancementCategory: An issue proposing an enhancement or a PR with one.L-dead_codeLint: dead_codeT-cargoRelevant to the cargo team, which will review and decide on the PR/issue.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions