Open
Description
Code
fn main() {
match false {
True => println!("I am true!"),
False => println!("I am false!"),
}
}
Current output
Compiling playground v0.0.1 (/playground)
warning: unreachable pattern
--> src/main.rs:4:9
|
3 | True => println!("I am true!"),
| ---- matches any value
4 | False => println!("I am false!"),
| ^^^^^ unreachable pattern
|
= note: `#[warn(unreachable_patterns)]` on by default
warning: unused variable: `True`
--> src/main.rs:3:9
|
3 | True => println!("I am true!"),
| ^^^^ help: if this is intentional, prefix it with an underscore: `_True`
|
= note: `#[warn(unused_variables)]` on by default
warning: unused variable: `False`
--> src/main.rs:4:9
|
4 | False => println!("I am false!"),
| ^^^^^ help: if this is intentional, prefix it with an underscore: `_False`
warning: variable `True` should have a snake case name
--> src/main.rs:3:9
|
3 | True => println!("I am true!"),
| ^^^^
|
= note: `#[warn(non_snake_case)]` on by default
help: rename the identifier or convert it to a snake case raw identifier
|
3 | r#true => println!("I am true!"),
| ~~~~~~
warning: variable `False` should have a snake case name
--> src/main.rs:4:9
|
4 | False => println!("I am false!"),
| ^^^^^
|
help: rename the identifier or convert it to a snake case raw identifier
|
4 | r#false => println!("I am false!"),
| ~~~~~~~
warning: `playground` (bin "playground") generated 5 warnings (run `cargo fix --bin "playground"` to apply 2 suggestions)
Finished dev [unoptimized + debuginfo] target(s) in 0.40s
Running `target/debug/playground`
Desired output
error: `True` will unconditionally match both `true` and `false` values.
--> src/main.rs:3:9
|
3 | True => println!("I am true!"),
| ^^^^
|
note: to pattern match on booleans in Rust, use `true` or `false` instead (note the capitalization)
--> src/main.rs:3:9
|
3 | true => println!("I am true!"),
| ^^^^
|
Rationale and extra context
For new Rustaceans coming from Python, none of the warnings actually explain the logical error here and do not indicate that there is almost certainly a logical bug going on here.
I'm not sure if this is worthwhile to add a correctness lint (deny-by-default), but this is almost certainly will be confusing for folks who are expecting True
to be a value, not a pattern.
Other cases
This should probably error if a boolean is matched on False
as well.
Anything else?
Tested on Nightly version: 1.72.0-nightly (2023-06-12 df77afb)