Skip to content

Suggest using let-chains #14678

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

Closed
nickdrozd opened this issue Apr 23, 2025 · 3 comments · Fixed by #14723
Closed

Suggest using let-chains #14678

nickdrozd opened this issue Apr 23, 2025 · 3 comments · Fixed by #14723
Labels
A-lint Area: New lints

Comments

@nickdrozd
Copy link

What it does

2024 edition introduces let-chains (rust-lang/rust#53667). It would be cool if Clippy could identify places where this feature could be used. Maybe autofix too?

Advantage

  • Less nesting
  • Less logical complexity

Drawbacks

No response

Example

if let Some(x) = f() {
    if g(x) {
        dbg!(x)
    }
}

Could be written as:

if let Some(x) = f() && g(x) {
    dbg!(x)
}
@nickdrozd nickdrozd added the A-lint Area: New lints label Apr 23, 2025
@y21
Copy link
Member

y21 commented Apr 23, 2025

#14481 implemented this as an extension to the collapsible_if lint.

(It's not on nightly yet though, there's been some issues with the clippy->rust sync so the component has been behind this repo for a few weeks now, but it should be soon)

Clippy output for the example snippet
warning: this `if` statement can be collapsed
 --> x.rs:5:5
  |
5 | /     if let Some(x) = f() {
6 | |         if g(x) {
7 | |             dbg!(x);
8 | |         }
9 | |     }
  | |_____^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#collapsible_if
  = note: `#[warn(clippy::collapsible_if)]` on by default
help: collapse nested if block
  |
5 ~     if let Some(x) = f()
6 ~         && g(x) {
7 |             dbg!(x);
8 ~         }
  |

@mi2ebi
Copy link

mi2ebi commented Apr 24, 2025

@y21 shouldn't the final brace be unindented one level? or is that not really something clippy can do? /genq

@nickdrozd
Copy link
Author

@y21 Cool, feel free to close this issue, or leave it open for tracking.

github-merge-queue bot pushed a commit that referenced this issue May 6, 2025
The MSRV being for crates targeting 1.85-1.87 on edition 2024

This enables the lint for regular nightly users without the feature gate
enabled

r? @samueltardieu

Fixes #14678

changelog: none
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants