rust-lang / rust-lang/rust-clippy
match_bool with named consts should be considered idiomatic
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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