Skip to content

Should match on bool with if-s on match arms be considered idiomatic? #5753

@eddyp

Description

@eddyp

I tried this code:

let (is_straight, opt_straight_rank) = match are_consecutive(&cards[1..]) {
    true if cards[0].rank - 1 == cards[1].rank => (true, Some(cards[0].rank)),
    true if cards[0].rank == 14 /* Ace */ && cards[1].rank == 5 => (true, Some(cards[1].rank)),
    _ => (false, None),
};

I expected no issues about the match on bool since the true match arms are conditioned and have some different content.

Instead, i got this error, which I think is suggesting to turn the match in a less readable pair of if-s (note the opt_straight_rank is bound to a different cards' .rank:

warning: you seem to be trying to match on a boolean expression
   --> src/lib.rs:196:48
    |
196 |           let (is_straight, opt_straight_rank) = match are_consecutive(&cards[1..]) {
    |  ________________________________________________^
197 | |             true if cards[0].rank - 1 == cards[1].rank => (true, Some(cards[0].rank)),
198 | |             true if cards[0].rank == 14 /* Ace */ && cards[1].rank == 5 => {
199 | |                 (true, Some(cards[1].rank))
200 | |             }
201 | |             _ => (false, None),
202 | |         };
    | |_________^
    |
    = note: `#[warn(clippy::match_bool)]` on by default
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#match_bool

I'm unsure if this is indeed a bug, but I found no option of an issue of an "inquiry" flavor.

I feel like the lint should be considering the if conditions on each arm are different/unrelated and/or if there are more than 2 arms of the match

Am I wrong? Is the match writable in a more compact/clear way without matching on bool? I like the current form as it makes it clear how the is_straight is working for both the regular condition as well as for the exception case of the straight starting with a 5 and ending with an ace.

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thinggood 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