rustmoduleprivatepublic

Detect leaking of private types which are only public in their module


Suppose I have a Rust crate mycrate with lib.rs:

mod internal {
    pub struct X();

    pub fn x() -> X {
        X()
    }
}

pub use internal::x;

This works fine on Rust 2024 and any consumer can use let val = mycrate::x();. However, X is not publically available making it very tricky to work with.

Question

How can I automatically detect if there are private types that my API "leaks".

Notes and clarifications


Solution

  • The compiler has a lint for that: unnameable_types. It is just allowed by default. You can #[warn] or #[deny] it in source code or from the command line.

    Also see the close sibling unreachable_pub.