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

Simplify if let statements that immediately return unwrapped value #8288

Closed
tvolk131 opened this issue Jan 15, 2022 · 6 comments
Closed

Simplify if let statements that immediately return unwrapped value #8288

tvolk131 opened this issue Jan 15, 2022 · 6 comments
Assignees
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages good-first-issue These issues are a good way to get started with Clippy

Comments

@tvolk131
Copy link

tvolk131 commented Jan 15, 2022

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()
}
@tvolk131 tvolk131 added the A-lint Area: New lints label Jan 15, 2022
@camsteffen camsteffen added C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages good-first-issue These issues are a good way to get started with Clippy and removed A-lint Area: New lints labels Jan 18, 2022
@camsteffen
Copy link
Contributor

This can be an enhancement of the question_mark lint.

@J-ZhengLi
Copy link
Member

J-ZhengLi commented Jan 19, 2022

Is it true that the clippy_utils::higher::IfLet::hir function does not pickup if let Err(err) = function_call() {} as a recognized pattern?

It detects

if let Err(err) = function_call() {
    return Err(err);
} else {}

but not without else, what am I missing?

@dswij
Copy link
Member

dswij commented Jan 19, 2022

Is it true that the clippy_utils::higher::IfLet::hir function does not pickup if let Err(err) = function_call() {} as a recognized pattern?

It detects

if let Err(err) = function_call() {
    return Err(err);
} else {}

but not without else, what am I missing?

If you're referring to

if let Some(higher::IfLet { let_pat, let_expr, if_then, if_else: Some(if_else) })
= higher::IfLet::hir(cx, expr);
not firing without else, it's because of the pattern match in L98 to only match on if_else: Some(..).

clippy_utils::higher::IfLet::hir does pick up if let without else. In fact, question_mark lint only checks for returns in the else arm right now.

@J-ZhengLi
Copy link
Member

... it's because of the pattern match in L98 to only match on if_else: Some(..).
clippy_utils::higher::IfLet::hir does pick up if let without else. In fact, question_mark lint only checks for returns in the else arm right now.

Thank you for pointing that out~

@dswij
Copy link
Member

dswij commented Jan 20, 2022

@J-ZhengLi are you working on this? If so, you can comment @rustbot claim to assign yourself to this issue :)

@J-ZhengLi
Copy link
Member

@J-ZhengLi are you working on this? If so, you can comment @rustbot claim to assign yourself to this issue :)

Well... fine, I guess I'll give it a try.

@rustbot claim

bors added a commit that referenced this issue Jul 8, 2022
Simplify if let statements

fixes: #8288

---

changelog: Allowing [`qustion_mark`] lint to check `if let` expressions that immediatly return unwrapped value
@bors bors closed this as completed in d0b8f75 Jul 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages good-first-issue These issues are a good way to get started with Clippy
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants