Skip to content

should comparison_chain only trigger for "if x > y {} else if x <y {} else {}" ? #4725

Closed
@matthiaskrgr

Description

@matthiaskrgr

This code

fn bla(a: i32, b: i32) {
    if a > b {
    } else if a < b {
    }
}

Triggers clippy::comparison_chain:

warning: `if` chain can be rewritten with `match`
 --> src/lib.rs:2:5
  |
2 | /     if a > b {
3 | |     } else if a < b {
4 | |     }
  | |_____^
  |
  = note: `#[warn(clippy::comparison_chain)]` on by default
  = help: Consider rewriting the `if` chain to use `cmp` and `match`.
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#comparison_chain

however using a match forces us to cover one additional case which the if-construct does not have: Ordering::Equal is not covered originally and we would have to use something like
_ => {} to get our code to compile.

Perhaps the lint should be refactored to only warn if there is also a final else/if there are 3 cases that are handled by the if-condition construct?

clippy 0.0.212 (e8d5a9e 2019-10-22)

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveL-styleLint: Belongs in the style lint groupL-suggestionLint: Improving, adding or fixing lint suggestionsgood first issueThese issues are a good way to get started with Clippy

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions