rust-lang / rust-lang/rust-clippy

Prevent wildcard Err match arms

Open
#14,742 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

What it does

Hi, I'm a beginner in Rust and have been playing around with error handling in my repo recently. One of the goals I'm trying to achieve is to ensure that all errors in my codebase are handled explicitly, either by propagating the error back to the caller or logging it.

We currently have the rule map_err_ignore that catches situations where we are throwing away the original error instead of propagating it.

I'm looking for something similar but that also covers when somebody directly matches on a Result, and similarly throws away the original error. So something like this should trigger a warning on the Err(_) match arm:

match get_result() {
    Ok(result) => {do something},
    Err(_) => {do something else}
}

I see that we have match_wild_err_arm, but I believe this only raises a warning when the body of the match arm includes a panic. However, let me know if there's already functionality through some other rule that accomplishes what I'm looking for.

Thanks!

Advantage

No response

Drawbacks

I could see drawbacks of the lint being that sometimes we really don't care about the error returned. I would probably keep it in the Pedantic category and allow it by default to avoid unnecessary warnings unless someone really wants to opt into it.

Example
match get_result() {
    Ok(result) => {do something},
    Err(_) => {do something else}
}

Could be written as:

match get_result() {
    Ok(result) => {do something},
    Err(e) => {
        println!("error happened: {}", e);
        // do something else
    } 
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the existing map_err_ignore and match_wild_err_arm lint implementations to understand how discarded errors and wildcard Err arms are handled. Define the intended warning behavior for non-panicking Err(_) arms, including whether it belongs in the Pedantic category, and verify that the resulting lint distinguishes intentional error ignoring.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.