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
@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

This comment has been minimized.

@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
@rust-bors

This comment has been minimized.

@mu001999 mu001999 force-pushed the flag/pub-as-pub-crate branch from a28e168 to ab9fe41 Compare April 10, 2026 04:48
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 10, 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.

@jdonszelmann jdonszelmann added T-lang Relevant to the language team I-lang-nominated Nominated for discussion during a lang team meeting. I-lang-easy-decision Issue: The decision needed by the team is conjectured to be easy; this does not imply nomination labels Apr 10, 2026
@jdonszelmann
Copy link
Copy Markdown
Contributor

r=me on the implementation, but talking to some people I think it's best to ping lang on this.

cc: @rust-lang/lang

@jdonszelmann
Copy link
Copy Markdown
Contributor

@rustbot blocked

@rustbot rustbot added S-blocked Status: Blocked on something else such as an RFC or other implementation work. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 10, 2026
@traviscross traviscross added the P-lang-drag-1 Lang team prioritization drag level 1. https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang label Apr 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) I-lang-easy-decision Issue: The decision needed by the team is conjectured to be easy; this does not imply nomination I-lang-nominated Nominated for discussion during a lang team meeting. P-lang-drag-1 Lang team prioritization drag level 1. https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang S-blocked Status: Blocked on something else such as an RFC or other implementation work. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants