Skip to content

Add a dead code elimination pass #1749

Description

@catamorphism

Add a pass that warns about non-exported functions that are never called.

Activity

  1. catamorphism commented on Feb 3, 2012

    @catamorphism
    ContributorAuthor

    as per IRC conversation w/ nmatsakis: The only trick is that many functions are "accidentally" exported due to the "export all by default" rule. One solution is to warn anyway if a function in a module with no export decls is unused (within the crate). Adding explicit export decls suppresses the warning.

  2. brson commented on Feb 3, 2012

    @brson
    Contributor

    See also #1702

  3. graydon commented on Feb 3, 2012

    @graydon
    Contributor

    We've discussed in the past inverting the "everything exported by default" rule at crate top-level. That is: only export explicit stuff for linking. The intra-crate is intended to make code less chatty and bureaucratic, but linking is often the appropriate time for bureaucracy.

    Also note: if you have an explicit export list from a crate, you have a lot fewer roots to run your dead-code analysis from.

  4. metajack commented on Apr 25, 2013

    @metajack
    Contributor

    Somewhat related is #2953

  5. Aatch commented on Aug 13, 2013

    @Aatch
    Contributor

    Visiting for triage. There have been lots of changes to privacy and similar, but I think this is still true.

  6. ktt3ja commented on Nov 7, 2013

    @ktt3ja
    Contributor

    I'm thinking of working on this for a class project but I'm unfamiliar with the internal of rustc. tjc has told me to focus on adding warning for now, and then work on eliminating dead code if I still have time, so I have been looking inside librustc/middle to figure out what to do. Since this issue is about adding a new pass and (I presume) the logic is similar to the liveness analysis in librustc/middle/liveness.rs, does that mean I'm supposed to create a new file in librustc/middle/ (say foo.rs) with a top-facing pub fn check_crate that does something similar to liveness.rs, and then call middle::foo::check_crate inside librustc/driver/driver.rs? Also, is there difficulty in checking for unused non-exported functions inside the existing liveness.rs rather than doing a separate pass?

    (sorry if the above are stupid questions)

  7. kud1ing commented on Dec 9, 2013

    @kud1ing

    Can be closed?

  8. alexcrichton commented on Dec 9, 2013

    @alexcrichton
    Member

    It can indeed, thanks @ktt3ja!

  9. added 2 commits that reference this issue on Aug 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions