Skip to content

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

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)

Activity

  1. added
    C-bugCategory: This is a bug.
    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.
    A-visibilityArea: Visibility / privacy
    on May 13, 2021
  2. varkor commented on May 15, 2021

    @varkor
    Contributor

    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?

  3. Nemo157 commented on May 15, 2021

    @Nemo157
    ContributorAuthor

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

  4. added a commit that references this issue on May 15, 2021
    7a6a25e
  5. varkor commented on May 16, 2021

    @varkor
    Contributor

    @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

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