rust-lang / rust-lang/rust-clippy

If-same-then-else bug for chained inequality expression

Open
#6,285 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

I tried this code:

if c.x < max_x && c.x > min_x && c.y <= max_y && c.y >= min_y {
        true
} else if c.x <= max_x && c.x >= min_x && c.y < max_y && c.y > min_y { 
        true 
} else {
        false
}

I expected to see this happen: everything works fine. Clippy does need to not complain. Both inequality chains within the two if conditions are different from each other.

Instead, this happened:

this `if` has identical blocks
   --> ghost\src\algorithms\intersection\segment_any.rs:119:74
    |
119 |       } else if c.x <= max_x && c.x >= min_x && c.y < max_y && c.y > min_y {
    |  __________________________________________________________________________^
120 | |         PointSegResult::Inside
121 | |     } else {
    | |_____^
    |
note: same as this
   --> ghost\src\algorithms\intersection\segment_any.rs:117:74
    |
117 |       } else if c.x < max_x && c.x > min_x && c.y <= max_y && c.y >= min_y {
    |  __________________________________________________________________________^
118 | |         PointSegResult::Inside
119 | |     } else if c.x <= max_x && c.x >= min_x && c.y < max_y && c.y > min_y {
    | |_____^
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#if_same_then_else
Meta
  • cargo clippy -V: e.g. clippy 0.0.212 (18bf6b4f0 2020-10-07)
  • rustc -Vv:

rustc 1.47.0 (18bf6b4f0 2020-10-07)
binary: rustc
commit-hash: 18bf6b4f01a6feaf7259ba7cdae58031af1b7b39
commit-date: 2020-10-07
host: x86_64-pc-windows-msvc
release: 1.47.0
LLVM version: 11.0

There is no need for a backtrace hence I omitted it.

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 reproducing the reported example with cargo clippy and inspect the if_same_then_else lint entry point. Compare the two chained inequality conditions in the report and verify that distinct conditions no longer produce an identical-block warning; the existing reproduction is in ghost\src\algorithms\intersection\segment_any.rs.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.