Skip to content

Lint unused pub items in binary crates - #149509

Merged
rust-bors[bot] merged 3 commits into
rust-lang:mainfrom
mu001999-contrib:flag/pub-as-pub-crate
May 7, 2026
Merged

Lint unused pub items in binary crates#149509
rust-bors[bot] merged 3 commits into
rust-lang:mainfrom
mu001999-contrib:flag/pub-as-pub-crate

Conversation

@mu001999

@mu001999 mu001999 commented Dec 1, 2025

Copy link
Copy Markdown
Member

View all comments

This PR adds a new unstable flag -Ztreat-pub-as-pub-crate as @Kobzol suggested.
When compiling binary crates with this flag, the seed worklist will only contain the entry fn and won't contain other reachable items. Then we can do the dead code analysis for pub items just like they are pub(crate).

Related zulip thread #general > pub/pub(crate) within a binary is a footgun.


Updated:

Adds a new lint dead_code_pub_in_binary (crate-level, default allow for now) instead of the previous unstable flag to lint unused pub items for binary crates.

See more details of implementation in #149509 (comment).

This lint is allowed by default, but I believe this has been better than the unstable flag. Making it warn-by-default will lead to a lot of noise for this PR (like bless many tests). So I'd like to make it warn-by-default in a separate PR in the future.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 1, 2025
@rustbot

rustbot commented Dec 1, 2025

Copy link
Copy Markdown
Collaborator

r? @jdonszelmann

rustbot has assigned @jdonszelmann.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@mu001999 mu001999 changed the title Add new unstable flag -Ztreat-pub-as-pub-crate Add new unstable flag -Ztreat-pub-as-pub-crate Dec 1, 2025
@mu001999 mu001999 changed the title Add new unstable flag -Ztreat-pub-as-pub-crate Add a new unstable flag -Ztreat-pub-as-pub-crate Dec 1, 2025
@Noratrieb

Noratrieb commented Dec 1, 2025

Copy link
Copy Markdown
Member

What are the plans for this flag? Being made the default? As just a flag, it's essentially useless, because no one will know about it. So I'd be opposed to just adding it without any plan for making it useful for everyone.

@mu001999
mu001999 marked this pull request as draft December 2, 2025 01:13
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 2, 2025
@mu001999
mu001999 force-pushed the flag/pub-as-pub-crate branch 2 times, most recently from 6541f80 to 8ac52ba Compare December 2, 2025 02:12
@mu001999
mu001999 marked this pull request as ready for review December 2, 2025 02:13
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Dec 2, 2025
@mu001999

mu001999 commented Dec 2, 2025

Copy link
Copy Markdown
Member Author

Maybe this could be a separate lint, I haven't thought too clearly yet

@jdonszelmann

Copy link
Copy Markdown
Contributor

Yea I do agree with nora here, maybe open a (specific) zulip thread to make a proper plan for this?