rust-lang / rust-lang/rust-clippy

Lint x != a || x != b

Open
#3,715 4 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I recently came across a bug in production code of the form:

if x != a || x != b {
    // create entry with value x
}

This was a bug, but it's not terribly obvious in that form. My guess is that it was copy-pasted from a different part of the code that did a something like:

if x == a || x == b {
    // use x
}

which was then inverted but without changing the || to &&. If we use De Morgan's law, it becomes clearer why a test like x != a || x != b is almost certainly wrong:

x != a || x != b

is equivalent to

!(x == a && x == b)

which makes it easier to see that the inner expression is false unless a == b. But this way of writing it is very unclear. If that's the actual intent, this reformulation is more legible (assuming transitivity of std::cmpEq:

a == b && x == a

Since I think x != a || x != b is more likely the result of a a mistaken conversion of x == a || x == b, it's worth linting.

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 from the Rust condition examples in the issue and define the exact pattern the lint should detect, including the intended comparison semantics. Identify the relevant Clippy lint entry point and add coverage showing that the mistaken || form is reported while the legible or intended alternatives are not; done means the lint handles the motivating case without inappropriate warnings.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 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.