rust/tests/ui/match_bool.fixed
Samuel Tardieu 0c3deeb246 match_bool: omit suggestion if guard is present
Without this check, the lint would suggest that

```rust
    match test {
        true if option == 5 => 10,
        _ => 1,
    };
```

is replaced by `if test { 10 } else { 1 }`.
2025-01-20 19:40:05 +01:00

58 lines
1 KiB
Rust

#![deny(clippy::match_bool)]
#![allow(clippy::nonminimal_bool, clippy::eq_op)]
fn match_bool() {
let test: bool = true;
if test { 0 } else { 42 };
let option = 1;
if option == 1 { 1 } else { 0 };
if !test {
println!("Noooo!");
};
if !test {
println!("Noooo!");
};
if !(test && test) {
println!("Noooo!");
};
if !test {
println!("Noooo!");
} else {
println!("Yes!");
};
// Not linted
match option {
1..=10 => 1,
11..=20 => 2,
_ => 3,
};
// Don't lint
let _ = match test {
#[cfg(feature = "foo")]
true if option == 5 => 10,
true => 0,
false => 1,
};
let _ = if test && option == 5 { 10 } else { 1 };
let _ = if !test && option == 5 { 10 } else { 1 };
if test && option == 5 { println!("Hello") };
if !(test && option == 5) { println!("Hello") };
if !test && option == 5 { println!("Hello") };
if !(!test && option == 5) { println!("Hello") };
}
fn main() {}