rust-lang / rust-lang/rust-clippy

match_bool with named consts should be considered idiomatic

Open
#10,326 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

When a match condition on a bool has named consts in the match arms, this provides more clarity than using a if ... else expression.

Reproducer

I tried this code:

#![warn(clippy::match_bool)]
const UP: bool = true;
const DOWN: bool = false;
fn main() {
    let x = UP;
    match x {
        UP => println!("up"),
        DOWN => println!("down"),
    }
}

I expected no warning, because replacing this match expression with an if ... else would make it hard for the reader to remember what the true and false arms semantically represent.

Instead, this happened: a warning was generated.

Version
rustc 1.69.0-nightly (8996ea93b 2023-02-09)
binary: rustc
commit-hash: 8996ea93b6e554148c4286e62b613f12a3ee505c
commit-date: 2023-02-09
host: x86_64-unknown-linux-gnu
release: 1.69.0-nightly
LLVM version: 15.0.7
Additional Labels

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the match_bool lint implementation and its existing tests, then run the reproducer from the issue. Done means a match on a bool using named true and false constants does not produce the warning, while the lint continues to cover the intended cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.