Skip to content

Simplify if let statements that immediately return unwrapped value #8288

@tvolk131

Description

@tvolk131

What it does

Reduce simple if let statements that immediately return the unwrapped value by replacing the if let with a ?.

Lint Name

collapsible_if_let_return

Category

style

Advantage

Encourages concise code and proper use of the ? operator.

Drawbacks

None that I can think of.

Example

fn example_fn() -> Result<(), CustomError> {
    if let Err(err) = run_some_process() {
        return Err(err);
    }
    // Other function logic...
    Ok(())
}

Could be written as:

fn example_fn() -> Result<(), CustomError> {
    run_some_process()?;
    // Other function logic...
    Ok(())
}

And, if there were no other function logic, could further be reduced to:

fn example_fn() -> Result<(), CustomError> {
    run_some_process()
}

Metadata

Metadata

Assignees

Labels

C-enhancementCategory: Enhancement of lints, like adding more cases or adding help messagesgood 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