Skip to content

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

Closed
@Nemo157

Description

@Nemo157

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.A-visibilityArea: Visibility / privacyC-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions