rust-lang / rust-lang/rust-clippy

Unnecessary match sub-argument lint

Open
#8,955 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

What it does

Clippy could spot situations similar to this one, where the second input (b) is unnecessary.

This is a very simple example, easy to spot visually. Clippy should be able to spot all similar situations.

Lint Name

unnecessary_match_argument

Category

suspicious

Advantage

No response

Drawbacks

No response

Example
fn test1(a: bool, b: bool) -> u32 {
    match (a, b) {
        (true, true) => 1,
        (true, false) => 1,
        (false, true) => 2,
        (false, false) => 2,
    }
}

test1 is equivalent to test2:

fn test2(a: bool, b: bool) -> u32 {
    match a {
        true => 1,
        false => 2,
    }
}

There are more complex situations that I am less sure are worth creating lints for. In this match the the first item of the 2-tuple output depends only of the first item of the 2-tuple input. And the same is true for the second inputs and outputs. So this match could be decomposed into two simpler and smaller matches.

fn test3(a: bool, b: bool) -> (bool, bool) {
    match (a, b) {
        (true, true) => (false, true),
        (true, false) => (false, false),
        (false, true) => (true, true),
        (false, false) => (true, false),
    }
}

(Ideas from: https://www.hillelwayne.com/post/decision-table-patterns/ ).

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 with the issue's test1 and test2 examples and determine the intended scope of the unnecessary_match_argument lint. No implementation files, tests, or entry points are named; identify the relevant Clippy lint and test locations, then define completion as detecting the stated unnecessary match argument without overreaching into the more complex test3 idea.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.