Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lint when > or >= is used i.e. compared values are not listed in (number line) order #13082

Open
Skgland opened this issue Jul 10, 2024 · 0 comments
Labels
A-lint Area: New lints

Comments

@Skgland
Copy link
Contributor

Skgland commented Jul 10, 2024

What it does

Check that < and <= are used rather than > or >= so that compared values are listed in order.

Advantage

Drawbacks

  • Its rather opinionated so should probably be allow by default style/pedantic lint
  • inversion might not be possible if only the PartialEq impl for one direction is implemented, so it might need to be limited to cases where the operands implement Eq

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

@Skgland Skgland added the A-lint Area: New lints label Jul 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

No branches or pull requests

1 participant