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

Add a lint for missing implementations of Error #9672

Closed
PatchMixolydic opened this issue Oct 17, 2022 · 2 comments
Closed

Add a lint for missing implementations of Error #9672

PatchMixolydic opened this issue Oct 17, 2022 · 2 comments
Labels
A-lint Area: New lints

Comments

@PatchMixolydic
Copy link
Contributor

PatchMixolydic commented Oct 17, 2022

What it does

This lint lints against local types which are used as a Result's E type, but don't implement the Error trait.

Lint Name

missing_error_impls

Category

style, pedantic, or restriction

Advantage

  • Allows the error to be passed to functions expecting dyn Error/impl Error
  • Enables the use of ? on the Result type in functions/try blocks that return Result<_, Box<dyn Error>> or anyhow::Result<_>

Drawbacks

  • Requires up to two trait implementations and a derive to resolve (or a dependency if the programmer opts to use thiserror)
  • On no_std, Error is only available with the error_in_core feature gate

Example

pub struct MyError;

pub fn foo() -> Result<(), MyError> {
    Ok(())
}

Could be written as:

#[derive(Debug)]
pub struct MyError;

impl std::error::Error for MyError {}

impl std::fmt::Display for MyError {
    fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
        todo!()
    }
}

pub fn foo() -> Result<(), MyError> {
    Ok(())
}

Or as:

#[derive(Debug, thiserror::Error)]
#[error("todo")]
pub struct MyError;

pub fn foo() -> Result<(), MyError> {
    Ok(())
}
@PatchMixolydic PatchMixolydic added the A-lint Area: New lints label Oct 17, 2022
@lukaslueg
Copy link
Contributor

Same as #6409?

@PatchMixolydic
Copy link
Contributor Author

Seems like it; my bad 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

No branches or pull requests

2 participants