Skip to content

Suggestion for pub(crate) not always valid on multiple imports #50455

Closed

Description

@alexcrichton

Given code that looks like:

mod foo {
    pub struct Bar;
    pub struct Baz;
}

mod bar {
    pub use foo::{Bar, Baz};
}

you get:

Details
$ rustc +nightly src/lib.rs --crate-type lib -W rust_2018_idioms
warning: struct is never used: `Bar`
 --> src/lib.rs:2:5
  |
2 |     pub struct Bar;
  |     ^^^^^^^^^^^^^^^
  |
  = note: #[warn(dead_code)] on by default

warning: struct is never used: `Baz`
 --> src/lib.rs:3:5
  |
3 |     pub struct Baz;
  |     ^^^^^^^^^^^^^^^

warning: unreachable `pub` item
 --> src/lib.rs:2:5
  |
2 |     pub struct Bar;
  |     ---^^^^^^^^^^^^
  |     |
  |     help: consider restricting its visibility: `pub(crate)`
  |
  = note: `-W unreachable-pub` implied by `-W rust-2018-idioms`
  = help: or consider exporting it for use by other crates

warning: unreachable `pub` item
 --> src/lib.rs:3:5
  |
3 |     pub struct Baz;
  |     ---^^^^^^^^^^^^
  |     |
  |     help: consider restricting its visibility: `pub(crate)`
  |
  = help: or consider exporting it for use by other crates

warning: unreachable `pub` item
 --> src/lib.rs:7:19
  |
7 |     pub use foo::{Bar, Baz};
  |                   ^^^ help: consider restricting its visibility: `pub(crate)`
  |
  = help: or consider exporting it for use by other crates

warning: unreachable `pub` item
 --> src/lib.rs:7:24
  |
7 |     pub use foo::{Bar, Baz};
  |                        ^^^ help: consider restricting its visibility: `pub(crate)`
  |
= help: or consider exporting it for use by other crates

which when auto-fixed by cargo fix will produce:

mod foo {
    pub(crate) struct Bar;
    pub(crate) struct Baz;
}

mod bar {
    pub use foo::{pub(crate), pub(crate)};
}

which fails to compile!

cc @Manishearth

Activity

  1. Manishearth commented on May 4, 2018

    @Manishearth
    Member

    cc @zackmdavis who implemented this

    I also wish we had a better answer for this lint than chopping up spans, but the current architecture makes this hard (this has been a problem for clippy forever).

    The problem here is that when we do HIR lowering we split apart uses, and this totally breaks down.

    But we throw away the AST by the time we have access level info.

    We can skip this lint for pub use for now, but that's not too great.

    I don't really have ideas for fixing this.

  2. zackmdavis commented on May 6, 2018

    @zackmdavis
    Contributor

    #50476 proposes not making suggestions for use items. (The lint itself still fires, of course.)

    I also wish we had a better answer for this lint than chopping up spans, but the current architecture makes this hard (this has been a problem for clippy forever).

    We could add spans to hir::Visibility?

  3. added a commit that references this issue on May 11, 2018
    268b038
  4. added a commit that references this issue on May 12, 2018
    5f98fe7
  5. added a commit that references this issue on Jul 2, 2018
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-diagnosticsArea: Messages for errors, warnings, and lintsA-edition-2018Area: The 2018 editionF-rust_2018_preview`#![feature(rust_2018_preview)]`

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions