rust-lang / rust-lang/rust-clippy

collapsible_if: comparison chain `x || y` -> `x` -> `y` can be optimized to `x && y`

Open
#5,282 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I stumbled upon such a piece of code (x and y represent bool expressions):

if x || y {
    if x {
        if y {
            unimplemented!("x and y");
        } else {
            unimplemented!("x");
        }
    } else {
        unimplemented!("y");
    }
}

which can be optimized to

if x && y {
    unimplemented!("x and y");
} else if x {
    unimplemented!("x");
} else if y {
    unimplemented!("y");
}

The combination of nested if expressions

  • x || y -> x -> y equals x && y
  • x || y -> x equals x
  • x || y -> y equals y

or

  • x || y -> x -> y equals (x || y) && x && y
  • x || y -> x equals (x || y) && x
  • x || y -> y equals (x || y) && y

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 collapsible_if lint named in the issue title and compare its existing behavior with the nested boolean examples in the issue body. Determine how the lint's tests represent these condition chains; done means the intended transformations are implemented with regression coverage and their behavior is reviewed.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.