stacks-core icon indicating copy to clipboard operation
stacks-core copied to clipboard

chore: Apply Clippy lint `comparison_chain`

Open jbencin opened this issue 11 months ago • 2 comments

Description

Apply Clippy lint comparison_chain, which finds places where if ... else if statements can be replaced with match a.cmp(b)

There were a few locations where I didn't want to make this change, because I felt it would decrease readability by increasing the indent level for a large segment of code. I added #[allow(clippy::comparison_chain)] so Clippy will ignore them

jbencin avatar Jan 30 '25 01:01 jbencin

@kantai I had mixed feelings on this, and some of the suggestions I skipped for this reason. Some of these do still result in more lines/indentation, but I think doing the cmp once and then matching the result is more clear than repeatedly comparing the same two things in an if ... else if ... else statement

I have to remember the directionality of cmp

It's intuitive, it's left-to-right just like you would read it. For example, 2.cmp(&4) returns Ordering::Less

jbencin avatar Jan 30 '25 02:01 jbencin

From the call: let's take a closer look at this PR. Most of the changes here are in tests. Perhaps we enable this lint, and use its warnings as evidence that your code needs refactoring. If we go forward with matchs, we should document the if/else condition being matched to improve code clarity. The main pushback is that matchs are harder to read.

jcnelson avatar Feb 07 '25 16:02 jcnelson