Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restriction lint for unsealed traits #14006

Open
Manishearth opened this issue Jan 16, 2025 · 0 comments
Open

Restriction lint for unsealed traits #14006

Manishearth opened this issue Jan 16, 2025 · 0 comments
Labels
A-lint Area: New lints L-restriction Lint: Belongs in the restriction lint group

Comments

@Manishearth
Copy link
Member

Manishearth commented Jan 16, 2025

A pattern in Rust is "sealing" traits: making them unimplementable to other users. It's typically done by doing something like:

pub trait PublicTrait: private::Sealed {
  // items
}

mod private {
   pub trait Sealed {} // empty
}

This allows one to declare that a trait is only used with internal types and should not be implemented by external users.

This has some benefits for keeping semver compatability. Codebases (like ICU4X) may wish to make this the default, just like you can already opt in to clippy::exhaustive_structs and clippy::exhaustive_enums to avoid committing to too much semver wise with public structs/enums. In a codebase with this lint enabled, every public non-sealed trait would have an allow ideally with justification.

The idea would be a lint clippy::unsealed_trait, which lints on traits which are:

  • Public
  • Reachable
  • Do not depend (transitively) on any trait named Sealed (we could also ignore the naming) that are not reachable from other crates
  • (perhaps) do not have a blanket impl for T

Transitivity might be tricky to do in a performant way.

@Manishearth Manishearth added A-lint Area: New lints L-restriction Lint: Belongs in the restriction lint group labels Jan 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints L-restriction Lint: Belongs in the restriction lint group
Projects
None yet
Development

No branches or pull requests

1 participant