1

single_match: Don't lint non-exhaustive matches; support tuples by jubnzv · Pull...

 2 years ago
source link: https://github.com/rust-lang/rust-clippy/pull/8322
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Copy link

Collaborator

@llogiq llogiq left a comment

I commented where I found shorter ways or was befuddled with the implementation. In general, this should be mostly merge-worthy already apart from the nitpick comments I added.

clippy_lints/src/matches.rs

Outdated Show resolved

match (&left.kind, &right.kind) {

(PatKind::Wild, _) | (_, PatKind::Wild) => true,

(PatKind::Tuple(left_in, left_pos), PatKind::Tuple(right_in, right_pos)) => {

// We don't actually know the position and presence of the `..` (dotdot) operator in

Copy link

Collaborator

@llogiq llogiq 28 days ago

This puzzles me. Why don't we know at which argument #no is the ..? And why do we need the span for this? It seems like a very roundabout way to implement this.

Copy link

Contributor

Author

@jubnzv jubnzv 27 days ago

edited

The point is that we don't know the actual number of the patterns replaced by ...

For example:

match (Some(E::V), Some(E::V), Some(E::V), Some(E::V)) {
  (Some(E::V), .., None) => todo!(), // `..` replaces [1, 2]
  (.., None) => todo!(), // `..` replaces [0, 1, 2]
}

We want to iterate to the both arms at the same time, to make sure that the elements with the same index form the exhaustive match. So, the logic implemented here basically evaluates the maximum possible length of the patterns and traverses both arms, considering entries in .. as wildcards (_).

Probably, the most simple solution would be to run the lint iff the second arm contains only wildcards. But this will make the lint a bit less accurate, because we won't be able to generate warnings in some cases.

Copy link

Collaborator

@llogiq llogiq 26 days ago

In that case shouldn't, position- and rpositioning the .. pattern in the subpatterns be enough? Also "span" has a different meaning in most lints, so the naming could be improved (if needed at all).

Copy link

Contributor

Author

@jubnzv jubnzv 22 days ago

edited

I fixed the naming to make it differ from rustc's Spans.

I'm not sure if we want to remove these variables at all, because this would complicate the loop in places when we need to evaluate the relative offset from the current pattern to the .. position.

tests/ui/single_match.rs

Outdated Show resolved

tests/ui/single_match.rs

Outdated Show resolved

tests/ui/single_match.rs

Outdated Show resolved


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK