rust-lang / rust-lang/rust-clippy

Restriction lint for unsealed traits

Open
#14,006 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint L-restriction
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by comparing the proposed lint with the existing clippy::exhaustive_structs and clippy::exhaustive_enums behavior. Work through the stated criteria for public, reachable traits and transitive dependence on an inaccessible Sealed trait, including the possible blanket-impl exception. Done means the lint design and its handling of these cases are settled and implementable.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.