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

dead_code lint does not trigger on pub fields of restricted types #85255

Closed
Nemo157 opened this issue May 13, 2021 · 3 comments · Fixed by #85324
Closed

dead_code lint does not trigger on pub fields of restricted types #85255

Nemo157 opened this issue May 13, 2021 · 3 comments · Fixed by #85324
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. A-visibility Area: Visibility / privacy. C-bug Category: This is a bug.

Comments

@Nemo157
Copy link
Member

Nemo157 commented May 13, 2021

I tried this code:

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;
}

I expected to see this happen: All of {Foo, Bar}::{a, b} are caught by the dead_code lint. Even though {Foo, Bar}::b are declared pub, they should both inherit the visibility of their parent items since there is no way for external code to access them.

Instead, this happened: No warning is emitted for Foo::b:

warning: field is never read: `a`
 --> src/main.rs:2:5
  |
2 |     a: i32,
  |     ^^^^^^
  |
  = note: `#[warn(dead_code)]` on by default

warning: associated function is never used: `a`
 --> src/main.rs:9:8
  |
9 |     fn a(&self) -> i32 { 5 }
  |        ^

warning: associated function is never used: `b`
  --> src/main.rs:10:12
   |
10 |     pub fn b(&self) -> i32 { 6 }
   |            ^

warning: 3 warnings emitted

Meta

rustc --version --verbose:

1.54.0-nightly
(2021-05-12 21e92b97309e15b16bc6)
@Nemo157 Nemo157 added C-bug Category: This is a bug. A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. A-visibility Area: Visibility / privacy. labels May 13, 2021
@varkor
Copy link
Member

varkor commented May 15, 2021

Why do we not emit a warning for pub b in the following?

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

Surely the visibility modifier is useless here because Foo is not public? Is this a separate issue?

@Nemo157
Copy link
Member Author

Nemo157 commented May 15, 2021

There is an allow-by-default lint unreachable-pub. That correctly triggers on both {Foo, Bar}::b when enabled.

@bors bors closed this as completed in 7a6a25e May 15, 2021
@varkor
Copy link
Member

varkor commented May 16, 2021

@Nemo157: ah, thanks. For some reason I thought it was warn-by-default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. A-visibility Area: Visibility / privacy. C-bug Category: This is a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants