Skip to content

A lint to check the ignoring of Err #16107

Description

@scuzzycheese

What it does

I want to demonstrate below, what I'd like a potential linter to prevent. This should be an optional linter:

if let Ok(res) = some_call() {
   //Do something with res
} else {
  error!("Something went wrong, but I don't know what");
}

vs

match some_call() {
  Ok(res) => // Do something with res,
  Err(e) => {
    error!("Something went wrong: {}", e);
  }
}

In the former example, the Err variant is ignored, and you're losing context. And for more complex error types, you might be able to recover depending on the type of error.

In the latter, it's enforing that something is done with the Err variant and isn't ignored.

I would love to have a linter that warns (or fails) if the Err variant is ignored.

Advantage

  • For people who want to use it, it can ensure accurate and detailed logging
  • Allows for the ability to recover from an error, if you know what's happened
  • Context is not lost

Drawbacks

  • Not everyone may want such a lint check, so it should be opt in

Example

if let Ok(res) = some_call() {
   //Do something with res
} else {
  error!("Something went wrong, but I don't know what");
}

Could be written as:

match some_call() {
  Ok(res) => // Do something with res,
  Err(e) => {
    error!("Something went wrong: {}", e);
  }
}

Comparison with existing lints

I don't know if an existing linter like this or similar to this exists. The closest one I can think of is this:

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions