rust-lang / rust-lang/rust-clippy

detect if comparison chain that could be a tuple comparison instead

Open
#1,111 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

The if_same_then_else lint triggered on the following code I use to sort some stuff:

positions.sort_by(|&(x1, y1, z1), &(x2, y2, z2)| {
    use std::cmp::Ordering::*;
    if z1 < z2 {
        Less
    } else if z1 > z2 {
        Greater
    } else if x1 > x2 {
        Less
    } else if x1 < x2 {
        Greater
    } else if y1 < y2 {
        Less
    } else {
        Greater
    }
});

I'm not sure if Clippy could reason about this or if it's even worth the trouble thinking about that but perhaps this is worthy of a footnote in the wiki?

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 existing if_same_then_else lint and searching its coverage for the reported comparison-chain pattern. Determine whether the shown ordering logic should be recognized as tuple comparison, then define tests that establish which chains trigger; done means the intended cases are detected without unrelated warnings.

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
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.