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

Merge nested matches #1252

Closed
mcarton opened this issue Oct 1, 2016 · 2 comments · Fixed by #6402
Closed

Merge nested matches #1252

mcarton opened this issue Oct 1, 2016 · 2 comments · Fixed by #6402
Labels
A-lint Area: New lints E-medium Call for participation: Medium difficulty level problem and requires some initial experience. T-AST Type: Requires working with the AST

Comments

@mcarton
Copy link
Member

mcarton commented Oct 1, 2016

We should have a lint that suggest to change

match foo {
    Ok(row) => match row {
        Some(pixels) => pixels,
        None => return Err(),
    },
    Err(err) => return Err(),
};

into

match foo {
    Ok(Some(pixels)) => pixels,
    Ok(None) => return Err(),
    Err(...) => return Err(),
}

Copied from https://github.com/Manishearth/rust-clippy/issues/1245#issuecomment-250666810.

@mcarton mcarton added E-medium Call for participation: Medium difficulty level problem and requires some initial experience. T-AST Type: Requires working with the AST A-lint Area: New lints labels Oct 1, 2016
@oli-obk
Copy link
Contributor

oli-obk commented Oct 1, 2016

5 seconds!

@mcarton
Copy link
Member Author

mcarton commented Oct 1, 2016

Héhé 😄

bors added a commit that referenced this issue Nov 30, 2020
Add Collapsible match lint

changelog: Add collapsible_match lint

Closes #1252
Closes #2521

This lint finds nested `match` or `if let` patterns that can be squashed together. It is designed to be very conservative to only find cases where merging the patterns would most likely reduce cognitive complexity.

Example:

```rust
match result {
    Ok(opt) => match opt {
        Some(x) => x,
        _ => return,
    }
    _ => return,
}
```
to
```rust
match result {
    Ok(Some(x)) => x,
    _ => return,
}
```

These criteria must be met for the lint to fire:

* The inner match has exactly 2 branches.
* Both the outer and inner match have a "wild" branch like `_ => ..`. There is a special case for `None => ..` to also be considered "wild-like".
* The contents of the wild branches are identical.
* The binding which "links" the matches is never used elsewhere.

Thanks to the hir, `if let`'s are easily included with this lint since they are desugared into equivalent `match`'es.

I think this would fit into the style category, but I would also understand changing it to pedantic.
@bors bors closed this as completed in e2ecc4a Dec 3, 2020
@bors bors closed this as completed in #6402 Dec 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints E-medium Call for participation: Medium difficulty level problem and requires some initial experience. T-AST Type: Requires working with the AST
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants