Finding items with un-necessarily large visibility

I'm working on a large (~750 files, > 400k LoC) crate, which overuses pub. Pretty much every struct and method is pub. The crate is a library, so the compiler doesn't warn when an item isn't used. What process would you follow to reduce the visibility of items?

I was thiking of:

  • turning every pub fn, pub struct and pub enum into pub(crate) fn, pub(crate) struct, pub(crate) enum, then try to build the crates that uses the library, so see exactly what really needs to be pub.
  • then turn the pub(crate) into pub(super) and restore pub(crate) where there are errors
  • then remove the pub(super) and restore it where there are errors

But this is going to be pretty tedious process. Is there a better way to do that?

I'm a big believer in the "glutton for punishment" way of doing this:

  1. Make sure the existing crates compile without warnings, and that cargo clippy does not show anything (for all the crates in the set you care about, not just the one with excessive visibility).
  2. Remove pub completely, breaking everything, from the crate you're fixing.
  3. Make the crate compile without warnings on its own by adding pub in places where it's needed
  4. Fix up cargo clippy so that there's no clippy warnings from making things not pub.
  5. Repeat steps 3 and 4 for each dependent crate, until you have everything working again.
2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.