rust-lang / rust-lang/rust-clippy
Lint when `>` or `>=` is used i.e. compared values are not listed in (number line) order
Open
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
Check that < and <= are used rather than > or >= so that compared values are listed in order.
Advantage
- unified style
- some find it easier to reason about (prompted by https://github.com/rust-lang/cargo/pull/14221#discussion_r1672445595)
Drawbacks
- Its rather opinionated so should probably be allow by default style/pedantic lint
- inversion might not be possible if only the
PartialEqimpl for one direction is implemented, so it might need to be limited to cases where the operands implementEq
Example
Could be written as:
if val >= min && max >= val {
// do something
}
if min <= val && val <= max {
// do something
}
Preferably it would also recognize common operands so that
if val <= max && min <= val {
// do something
}
if max <= val || val <= min {
// do something
}
would become
if min <= val && val <= max {
// do something
}
if val <= min && max <= val {
// do something
}
i.e. for && move common value to the operator and for || move common value away from the operator
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No source file, test, or entry point is named. Start from the comparison examples in this issue and define the supported operand and Eq cases; done means the requested forms are diagnosed and the suggested rewrites preserve the stated ordering for && and ||.
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
- 35/100