rust-lang / rust-lang/rust-clippy
Lint Suggestion: Public Type Alias
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
What it does
It detects the use of pub type that would expose that type across a crate boundary. By default, this could be set to allow but providing the option to disable public type aliases would be a nice feature.
Lint Name
public_type_alias
Category
style, complexity
Advantage
For large workspaces with a large number of contributors, it is nice to have as many dials as possible when using clippy to enforce code style. By providing this as an option, it would allow for banning the use of public type aliases across the project. For example, it is a common pattern to use
// lib.rs
pub struct Error;
type Result<T = ()> = std::result::Result<T, Error>;
This is an easy to follow pattern when applied across multiple crates in a workspace. A naive consumer may try and make this change.
- type Result<T = ()> = std::result::Result<T, Error>;
+ pub type Result<T = ()> = std::result::Result<T, Error>;
However, a more appropriate way to handle this result would be to define their own Error type that impl From<Error> for Error and define a similar type alias in the consuming crate.
Drawbacks
This is probably not something the ecosystem would be interested in as default enabled lint as it would make the existing pattern (most popularly used by std::io::Result) a pain to emulate.
Example
use other_crate::Result;
fn some_fn() -> Result<()> {
Ok(())
}
Could be written as:
use other_crate::Error;
type Result<T> = std::result::Result<T, Error>
fn some_fn() -> Result<()> {
Ok(())
}
Or more involved
use other_crate::Error as OtherError;
type Result<T> = std::result::Result<T, Error>
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error(transparent)]
Other(#[from] OtherError),
#[error("Some condition I control")]
MyError,
}
fn some_fn() -> Result<()> {
Ok(())
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The issue defines a proposed public_type_alias Clippy lint but names no implementation file or test. Start by reviewing existing Clippy lint implementations and their tests, then trace how public Rust type aliases are represented and checked. Done means the lint can optionally reject pub type aliases while remaining disabled by default and covering the stated examples.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100