rust-lang / rust-lang/rust-clippy
`*_exclusive_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
This is formed of 6 lints:
-
crate_exclusive_errors: Functions can only return error types from within their crate.In
src/lib.rs, don't do this:pub fn my_function() -> Result<(), std::io::Error> { /* ... */ }do this:
pub struct MyCrateError(std::io::Error); pub fn my_function() -> Result<(), crate::MyCrateError> { /* ... */ } -
module_exclusive_errors: Functions can only return error types from within their module.
This is a superset ofcrate_exclusive_errors.In
src/my_module.rs, don't do this:pub fn my_function() -> Result<(), crate::MyCrateError> { /* ... */ }do this:
pub struct MyModuleError(std::io::Error); pub fn my_function() -> Result<(), MyModuleError> { /* ... */ } -
function_exclusive_errors: Functions can only return error types from within their module and no 2 functions can return the same error type.
This is a superset ofmodule_exclusive_errors.In
src/my_module.rs, don't do this:pub enum MyModuleError { One(std::io::Error), Two(std::num::TryFromIntError) } pub fn my_first_function() -> Result<(), MyModuleError> { /* ... */ } pub fn my_second_function() -> Result<(), MyModuleError> { /* ... */ }do this:
pub struct MyFirstFunctionError(std::io::Error); pub struct MySecondFunctionError(std::num::TryFromIntError); pub fn my_first_function() -> Result<(), MyFirstFunctionError> { /* ... */ } pub fn my_second_function() -> Result<(), MySecondFunctionError> { /* ... */ } -
crate_name_errors: When an error type is defined at the root of a crate (lib.rsormain.rs) it should be named<crate name in pascal case>Error. -
module_name_errors: When an error type is defined within a module it should be named<module name in pascal case>Error. -
function_name_errors: When an error type is exclusively returned by 1 function it should be named<function name in pascal case>Error.
This supersedesmodule_name_errorsandcrate_name_errorswhere it applies
Advantage
Enforces a standard coding practice for error granularity throughout a project, offering an ascending level of strictness/descriptiveness.
Drawbacks
module_exclusive_errors: Requires a notable amount of code.function_exclusive_errors: Requires a significant amount of code.
Possible additional lints
Categories
I would suggest the categories:
crate_exclusive_errors: pedanticmodule_exclusive_errors: pedantic or restrictionfunction_exclusive_errors: restrictioncrate_name_errors: restrictionmodule_name_errors: restrictionfunction_name_errors: restriction
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
Start with the six proposed lint behaviors and the examples in src/lib.rs and src/my_module.rs. Review how the requested crate-, module-, and function-level exclusivity and naming rules should relate, then define the scope and tests needed for all six lints; done means the agreed rules are implemented and validated.
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
- 35/100