Skip to content

Lint unused pub items in binary crates#149509

Open
mu001999 wants to merge 4 commits intorust-lang:mainfrom
mu001999-contrib:flag/pub-as-pub-crate
Open

Lint unused pub items in binary crates#149509
mu001999 wants to merge 4 commits intorust-lang:mainfrom
mu001999-contrib:flag/pub-as-pub-crate

Conversation

@mu001999
Copy link
Copy Markdown
Contributor

@mu001999 mu001999 commented Dec 1, 2025

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 unused_pub_items_in_binary (crate-level, default allow for now) instead of the previous unstable flag to lint unused pub items for binary crates.

@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
Copy link
Copy Markdown
Collaborator

rustbot commented Dec 1, 2025

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
Copy link
Copy Markdown
Member

Noratrieb commented Dec 1, 2025

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
Copy link
Copy Markdown
Contributor Author

mu001999 commented Dec 2, 2025

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?

@jdonszelmann
Copy link
Copy Markdown
Contributor

I guess I'd prefer this as a lint (attribute at the crate root) myself

@jdonszelmann
Copy link
Copy Markdown
Contributor

@rustbot author

@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 3, 2025
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Dec 3, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@mu001999 mu001999 marked this pull request as draft December 3, 2025 14:34
@Kobzol
Copy link
Copy Markdown
Member

Kobzol commented Dec 3, 2025

I guess that the use-case that I'm envisioning is to have an opt-in way to treat pub stuff as pub(crate) in binary crates. If it was an allow-by-default lint, which could be manually denied, that would serve that use-case, I think.

@GrantGryczan
Copy link
Copy Markdown

Why would the end goal be for this to be opt-in? I and others want to see #74970 become a default. I wouldn't opt into this, because that would make my codebase less idiomatic, diverging from how everyone else who hasn't opted in uses pub.

The reason this is useful as a default is because it doesn't semantically make sense to distinguish between pub and pub(crate) in binary crates. This creates a footgun because, for example, pub items can't receive dead_code warnings. If it's just opt-in, then that doesn't remove the footgun for anyone who hasn't opted in, and I don't see a benefit to pub and pub(crate) being distinct in binary crates.

The only reason I can see that someone might not want to opt into this is that one might use pub to prevent dead_code warnings as opposed to pub(crate). But I think this intent might be better communicated using #[expect(dead_code)] rather than the implicit behavior of pub. Dead code detection is useful as a default. (The only downside to that is that you then have to remove the #[expect(dead_code)] if you later use it, but I'll leave it to others to decide if that coupling is an important consideration.)

@mu001999 mu001999 force-pushed the flag/pub-as-pub-crate branch from 8ac52ba to 923596e Compare December 28, 2025 15:22
@mu001999 mu001999 force-pushed the flag/pub-as-pub-crate branch from 923596e to 1d89354 Compare March 19, 2026 12:06
@rust-log-analyzer

This comment has been minimized.

@mu001999 mu001999 force-pushed the flag/pub-as-pub-crate branch from 1d89354 to e116866 Compare March 19, 2026 13:41
@mu001999 mu001999 force-pushed the flag/pub-as-pub-crate branch from 4ff6386 to 05d9723 Compare March 20, 2026 01:53
@rust-log-analyzer

This comment has been minimized.

@mu001999 mu001999 force-pushed the flag/pub-as-pub-crate branch from 05d9723 to 525dee9 Compare March 20, 2026 13:17
@rust-log-analyzer

This comment has been minimized.

@mu001999 mu001999 force-pushed the flag/pub-as-pub-crate branch 2 times, most recently from 1770f41 to b2c59a7 Compare March 20, 2026 23:58
@mu001999
Copy link
Copy Markdown
Contributor Author

New lint unused_pub_items_in_binary (crate-level, default allow for now) is added and used to lint such items, instead of the unstable flag.

The main implementation and change in this PR is to try to split the dead-code analysis into two-phase analysis:

  1. In the first phase, the worklist seeds won't contain items that are reachable or allowed by #[allow(dead_code)]. After the first phase, the snapshot of the result will be kept and used for the new unused_pub_items_in_binary lint.
  2. Before the second phase, the worklist will be extended with these items. And finally, we could get the original analysis result for dead_code lint.

This two-phase process is to keep the behavior of the two lints independent of each other.

@mu001999 mu001999 changed the title Add a new unstable flag -Ztreat-pub-as-pub-crate Lint unused pub items in binary crates Mar 21, 2026
@mu001999 mu001999 marked this pull request as ready for review March 21, 2026 05:31
@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 Mar 21, 2026
@jdonszelmann
Copy link
Copy Markdown
Contributor

@rustbot author

@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 Apr 5, 2026
@mu001999 mu001999 force-pushed the flag/pub-as-pub-crate branch from b2c59a7 to 5085ed1 Compare April 6, 2026 14:07
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 6, 2026

Some changes occurred in compiler/rustc_passes/src/check_attr.rs

cc @jdonszelmann, @JonathanBrouwer

@rustbot rustbot added the A-attributes Area: Attributes (`#[…]`, `#![…]`) label Apr 6, 2026
@mu001999 mu001999 force-pushed the flag/pub-as-pub-crate branch from 5085ed1 to c6daae0 Compare April 6, 2026 15:06
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 6, 2026

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@mu001999
Copy link
Copy Markdown
Contributor Author

mu001999 commented Apr 6, 2026

@rustbot review

@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 Apr 6, 2026
@mu001999 mu001999 force-pushed the flag/pub-as-pub-crate branch from c6daae0 to a28e168 Compare April 9, 2026 14:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants