Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn about unused pub fields in non-pub structs #85324

Merged
merged 1 commit into from
May 15, 2021

Conversation

FabianWolff
Copy link
Contributor

This pull request fixes #85255. The current implementation of dead code analysis is too prudent because it marks all pub fields of structs as live, even though they cannot be accessed from outside of the current crate if the struct itself only has restricted or private visibility.

I have changed this behavior to take the containing struct's visibility into account when looking at field visibility and liveness. This also makes dead code warnings more consistent; consider the example given in #85255:

struct Foo {
    a: i32,
    pub b: i32,
}

struct Bar;

impl Bar {
    fn a(&self) -> i32 { 5 }
    pub fn b(&self) -> i32 { 6 }
}


fn main() {
    let _ = Foo { a: 1, b: 2 };
    let _ = Bar;
}

Current nightly already warns about Bar::b(), even though it is pub (but Bar is not). It should therefore also warn about Foo::b, which it does with the changes in this PR.

@rust-highfive
Copy link
Collaborator

r? @varkor

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 15, 2021
@varkor
Copy link
Member

varkor commented May 15, 2021

Thanks!

@bors r+

@bors
Copy link
Contributor

bors commented May 15, 2021

📌 Commit 46d55d6 has been approved by varkor

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 15, 2021
bors added a commit to rust-lang-ci/rust that referenced this pull request May 15, 2021
…laumeGomez

Rollup of 4 pull requests

Successful merges:

 - rust-lang#84751 (str::is_char_boundary - slight optimization)
 - rust-lang#85185 (Generate not more docs than necessary)
 - rust-lang#85324 (Warn about unused `pub` fields in non-`pub` structs)
 - rust-lang#85329 (fix version_str comment)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 7a6a25e into rust-lang:master May 15, 2021
@rustbot rustbot added this to the 1.54.0 milestone May 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

dead_code lint does not trigger on pub fields of restricted types
5 participants