Skip to content

Conversation

theodorebje
Copy link
Contributor

@theodorebje theodorebje commented Jul 7, 2025

Motivation

This PR is part of the broader PR #3394 (Activate more lints)

There are many different ways to express the same pattern matching and equality checking. This PR standardises those two things using clippy lints, and also makes the code more readable.

Solution

This PR adds 4 new lints to Cargo.toml:

  • manual_let_else
  • match_same_arms
  • single_match_else
  • equatable_if_let

Theodore Bjernhed and others added 4 commits July 7, 2025 22:09
`let...else` provides a standard construct for this same pattern that
people can easily recognize (and is IMO much less confusing). It's also
more compact. This fixes the `manual_let_else` clippy lint.
`match` arms with identical arm bodies are confusing to the reader
because they hide the intent to the reader. This fixes the
`match_same_arms` clippy lint.
`if let..else` statements nest less than `match` statements with two
arms and are therefore more readable. This fixes the `single_match_else`
clippy lint.
Pattern matching that are really just checking for equality are more
idiomatically described using the `matches!` macro instead of `if let`
statements. `if let` statements use Yoda notation [1] which increase
cognitive load while reading the code. This fixes the `equatable_if_let`
clippy lint.

[1]: https://en.wikipedia.org/wiki/Yoda_conditions
@theodorebje theodorebje mentioned this pull request Jul 7, 2025
5 tasks
Theodore Bjernhed and others added 2 commits July 7, 2025 23:40
Seems to not have been found by clippy on my machine. This fixes the
`manual_let_else` clippy lint.

See-also: 4332e94
Forgot to run cargo-fmt during the previous commits.
@jplatte jplatte self-requested a review July 8, 2025 16:08
Copy link
Member

@jplatte jplatte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@jplatte jplatte enabled auto-merge (squash) July 9, 2025 18:46
@jplatte jplatte merged commit 1b157a4 into tokio-rs:main Jul 9, 2025
18 checks passed
@theodorebje theodorebje deleted the clippy-if-else branch July 9, 2025 19:08
@theodorebje theodorebje mentioned this pull request Jul 11, 2025
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants