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

suggest replacing if comparison chain with cmp match #4531

Closed
oli-cosmian opened this issue Sep 10, 2019 · 11 comments · Fixed by #4569
Closed

suggest replacing if comparison chain with cmp match #4531

oli-cosmian opened this issue Sep 10, 2019 · 11 comments · Fixed by #4569
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy L-style Lint: Belongs in the style lint group L-suggestion Lint: Improving, adding or fixing lint suggestions

Comments

@oli-cosmian
Copy link
Contributor

If one writes

if x < y {
     ...
} else if x > y {
     ...
} else {
     ...
}

one should instead be writing

match x.cmp(&y) {
     Ordering::Less => ...,
     Ordering::Greater => ...,
     Ordering::Equal => ...,
}
@flip1995 flip1995 added L-style Lint: Belongs in the style lint group L-suggestion Lint: Improving, adding or fixing lint suggestions A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy labels Sep 10, 2019
@james9909
Copy link
Contributor

I can take a stab at this issue

@flip1995
Copy link
Member

flip1995 commented Sep 17, 2019

Thanks! Let us know if you have any questions.

Also make sure to take a look at https://github.com/rust-lang/rust-clippy/blob/master/doc/adding_lints.md. That's a really good introduction on writing lints.

bors added a commit that referenced this issue Sep 25, 2019
Add a new lint for comparison chains

changelog: Adds a new lint: `comparison_chain`.

`comparison_chain` lints all `if` conditional chains where all the conditions are binary comparisons on the same two operands and will suggest a rewrite with `match`.

Closes #4531.
@bors bors closed this as completed in 4d30b08 Sep 26, 2019
@untitaker
Copy link
Contributor

This requires an additional import and is harder to read. Why is this a default lint?

@oli-cosmian
Copy link
Contributor Author

I don't think it's harder to read. imo this is much clearer what's going on, because you have the three cases very explicitly.

Also I don't consider additional or fewer imports to be relevant for whether a lint is useful or not

@untitaker
Copy link
Contributor

untitaker commented Dec 19, 2019

In my book a < b will always be easier to read than a.cmp(b) == Ordering::Less. Irrespective of whether your proposed coding style provides more compile-time safety than a regular comparison (I don't really care), because that has nothing to do with readability.

Why would I import more traits and enums from std if I can achieve the same thing without? e.g. I don't import FromStr in favor of using .parse() (even though FromStr might be more explicit and less indirect)

FWIW this lint also triggers for code that does not intend to handle all three cases: getsentry/symbolic@f928869#diff-526c79c0b4fc6984353f21705ba99a12R396

and it also triggers for code that wants to partialcmp instead of cmp.

@mitsuhiko
Copy link
Contributor

In the case of symbolic in particular it’s especially weird because the type doesn’t even implement Ord but only PartialOrd which requires one to use partial_cmp which is extra ugly.

@oli-cosmian
Copy link
Contributor Author

FWIW this lint also triggers for code that does not intend to handle all three cases:

That looks also fine to me. I think it should be warn by default.

Since some situations require partial_cmp, we should fix the lint of course.

To me this lint is a clear improvement (especially since it does help catch bugs).

@raphlinus
Copy link

FWIW, I dislike this lint, and find the direct comparisons easier to read. As a result, linebender/druid#406 is adding #[allow(clippy:comparison_chain)].

@llogiq
Copy link
Contributor

llogiq commented Dec 20, 2019

@raphlinus that's OK. Readability is not something that can be decided in the general case, but must be approached on a case-by-case basis.

@untitaker
Copy link
Contributor

untitaker commented Dec 20, 2019

Cool, so you agree such a general lint in defaults is wrong?

@flip1995
Copy link
Member

flip1995 commented Dec 21, 2019

A check for Ord was implemented in #4827. Maybe it didn't hit stable/beta/nightly (Not sure where we're in the release cycle, since I was on vacation the last week) yet.

Cool, so you agree such a general lint in defaults is wrong?

This is a style lint, which is the "weakest" default group and is expected to have the most allows. I also agree, that an exhaustive match is more rusty, than an if with comparisons.

this lint also triggers for code that does not intend to handle all three cases

See #4725

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy L-style Lint: Belongs in the style lint group L-suggestion Lint: Improving, adding or fixing lint suggestions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants