rust-lang / rust-lang/rust-clippy

Lint for missing pair in match on pairs of enum

Open
#971 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint C-question E-hard L-correctness T-middle
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

This match:

match (&left.node, &right.node) {
    (&PatKind::Box(..), &PatKind::Box(..)) => …,
    (&PatKind::TupleStruct(..), &PatKind::TupleStruct(..)) => …,
    (&PatKind::Ident(..), &PatKind::Ident(..)) => …,
    (&PatKind::Lit(..), &PatKind::Lit(ref r)) => …,
    (&PatKind::QPath(..), &PatKind::QPath(..)) => …
    (&PatKind::Tuple(..), &PatKind::Tuple(..)) => …
    (&PatKind::Range(..), &PatKind::Range(..)) => …,
    (&PatKind::Ref(..), &PatKind::Ref(..)) => …,
    (&PatKind::Vec(..), &PatKind::Vec(..)) => …,
    (&PatKind::Wild, &PatKind::Wild) => …,
    _ => …,
}

looks like is missing some pair (&Path(..), &Path(..)) that was not added in a previous rustup. Of course in such case it would not be reasonable to list the n×(n-1) missing cases and we used _, but that means Rust won’t tell us about that missing pair 😢.

What do you think of a lint that would warn on such matches that users would explicitly mark?

#[deny(missing_tuple)] // ¹
match (&left.node, &right.node) {
    (&PatKind::Box(..), &PatKind::Box(..)) => …,
    [… same as above…],
    (&PatKind::Wild, &PatKind::Wild) => …,
    _ => …, // error: the tuple `(Path, Path)` is missing
}

1: alternatively we could use a #[clippy_missing_tuple] attribute on the match if #[allow/deny/warn] do not work on expressions.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the match in clippy_lints/src/utils/hir.rs at lines 143-165 and investigate how Clippy attributes can apply to match expressions. Define how an explicitly marked tuple match should report missing enum pairs, including whether #[deny], #[allow], or a dedicated attribute is required. Done means the proposed lint detects omitted pairs without requiring all n×(n-1) cases to be listed.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.