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
pub(crate) rather than pub. If I use e.g. pub(crate) fn x() -> X I cannot compile due to "x is only public within the crate, and cannot be re-exported outside". Alternatively, I can use pub(crate) struct X(); which gives me the correct warning "type Xis more private than the itemx". However, this requires manual checking which is not scalable for larger projects.Result<PublicStruct, PrivateEnumError>.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.