Skip to content

Remove unnecessary match #7040

Closed
Closed
@SaadiSave

Description

@SaadiSave

What it does

What does this lint do?

Remove unnecessary match for error handling when function signatures are the same.

Categories (optional)

  • Kind: style, correctness ?

What is the advantage of the recommended code over the original code

Not sure if there are any performance improvements. The code is more readable and shorter though. This lint also applies for any function pair that returns the same enum, like Option<T> for example.

Drawbacks

None.

Example

// For example, a function has the signature:
fn fname(...) -> Result<T, E> { ... }

// and it is called in another function with the same signature:
fn fname2(...) -> Result<T, E> {
    // This is obviously unnecessary
    match fname(...) {
        Ok(s) => Ok(s),
        Err(e) => Err(e),
    }
}

Could be written as:

// For example, a function has the signature:
fn fname(...) -> Result<T, E> { ... }

// and it is called in another function with the same signature:
fn fname2(...) -> Result<T, E> {
    // Much better
    fname(...)
}

Real world example

async fn destructure_reqwest_response(res: Response) -> Result<Generic, InternalError> {
    ...
}

async fn get_count(...) -> Result<Generic, InternalError> {
    ...
    // Changed this later. Even clippy::pedantic did not detect this, so I assume that a lint is missing.
    match res {
        Ok(r) => match destructure_reqwest_response(r).await {
            Ok(r) => Ok(r),
            Err(e) => Err(e),
        },
        Err(_) => Err(INTERNALERROR),
    }
    ...
}

Metadata

Metadata

Assignees

Labels

A-lintArea: New lintsgood first issueThese issues are a good way to get started with Clippy

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions