-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Closed
Labels
A-documentationArea: Adding or improving documentationArea: Adding or improving documentationgood first issueThese issues are a good way to get started with ClippyThese issues are a good way to get started with Clippy
Description
clippy 0.0.212 (23549a8 2020-03-16)
rustc 1.44.0-nightly (f509b26 2020-03-18) on macos
clippy suggests rewriting some chained if/else blocks in my code, like so
- let y = l[0];
- if y > x {
- return 0;
- } else if y == x {
- b = 0;
- } else {
- a = 1;
- c = 1;
- }
+ match l[0].cmp(&x) {
+ Ordering::Greater => return 0,
+ Ordering::Equal => b = 0,
+ Ordering::Less => {
+ a = 1;
+ c = 1;
+ }
+ };
This is semantically the same and arguably cleaner. But, according to a Criterion microbenchmark, Clippy's suggestion is significantly slower than the chained comparisons.
It looks like this might be related to the simple comparison being inline, versus the cmp
remaining as
call core::cmp::impls::<impl core::cmp::Ord for i32>::cmp
Probably in many cases the performance difference isn't noticeable and it's better style, but perhaps there should at least be a caveat in the Clippy docs.
(Also, ideally the compiler should be smart enough to make it just as fast.)
ikepggthb and pronebird
Metadata
Metadata
Assignees
Labels
A-documentationArea: Adding or improving documentationArea: Adding or improving documentationgood first issueThese issues are a good way to get started with ClippyThese issues are a good way to get started with Clippy