Skip to content

Suggestion for pub(crate) and reexports are tricked #57411

@alexcrichton

Description

@alexcrichton

First reported upstream, this code:

#![warn(unreachable_pub)]
#![deny(unused_imports)]

pub mod core {
    pub mod compiler {
        mod context {
            mod compilation_files {
                pub struct Metadata;
                pub struct CompilationFiles;
                impl CompilationFiles {
                    pub fn metadata() -> Metadata { Metadata }
                }
            }
            use self::compilation_files::CompilationFiles;
            pub use self::compilation_files::Metadata;  // Suggests pub(crate)
                                // but that causes unused_imports to complain.

            pub struct Context;
            impl Context {
                pub fn files() -> CompilationFiles { CompilationFiles }
            }
        }
        pub use self::context::Context;
    }
}

generates the warning

warning: unreachable `pub` item
  --> src/lib.rs:15:13
   |
15 |             pub use self::compilation_files::Metadata;  // Suggests pub(crate)
   |             ---^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |             |
   |             help: consider restricting its visibility: `pub(crate)`
   |
note: lint level defined here
  --> src/lib.rs:1:9
   |
1  | #![warn(unreachable_pub)]
   |         ^^^^^^^^^^^^^^^
   = help: or consider exporting it for use by other crates

warning: unreachable `pub` item
  --> src/lib.rs:15:21
   |
15 |             pub use self::compilation_files::Metadata;  // Suggests pub(crate)
   |             ---     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |             |
   |             help: consider restricting its visibility: `pub(crate)`
   |
   = help: or consider exporting it for use by other crates

but the suggested fix doesn't compile!

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.L-unreachable_pubLint: unreachable_pubT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions