rust-lang / rust-lang/rust-clippy
Lint idea: suggest std Error trait for custom errors
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
Libraries regularly implement their own error types, frequently as enum wrappers around dependency-specific errors.
Other libraries like anyhow exist to make error handling in application code simpler, at least for error types that implement std::error::Error.
However, when wrapping/writing custom error types for a library, it's easy to forget to implement std::error::Error. It would be nice to have a clippy lint that detects custom error types and suggests that the user add an impl std::error::Error for MyError {} implementation.
Categories (optional)
Potentially clippy::style.
What is the advantage of the recommended code over the original code
The recommended code transitively simplifies error handling in other codebases by ensuring that the standard Error trait is implemented. This enables more direct use of libraries like anyhow.
Drawbacks
This lint should only be run on crates that are specified as libraries, not as application code (i.e., producing binaries).
It might also be somewhat difficult to determine which types are custom error types. Some ideas:
- Look for
Result<T, X>, whereXis a crate-local type - Look for types within
crate::*::errorthat match a naming pattern (like*Error*) - Look for
enumtypes whose variants' associated datas contain one or more implementations ofstd::error::Error
Example
pub enum MyError {
Io(std::io::Error),
Other(SomeOtherError),
}
Could be written as:
pub enum MyError {
Io(std::io::Error),
Other(SomeOtherError),
}
impl std::error::Error for MyError {
// trait impl...
}
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 names no repository files or tests. Start by reviewing Clippy's existing lint structure and the proposed signals for identifying custom error types, including crate-local Result errors and error enums. Done means the detection scope, library-only behavior, suggested std::error::Error implementation, and test coverage are defined and implemented.
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
- Needs clarification
- Newbie friendliness
- 20/100