rust/tests/ui/or-patterns/binding-typo.rs
Esteban Küber cb9cd8f830 On binding not present in all patterns, look at consts and unit structs/variants for suggestions
When encountering an or-pattern with a binding not available in all patterns, look for consts and unit struct/variants that have similar names as the binding to detect typos.

```
error[E0408]: variable `Ban` is not bound in all patterns
  --> $DIR/binding-typo.rs:22:9
   |
LL |         (Foo, _) | (Ban, Foo) => {}
   |         ^^^^^^^^    --- variable not in all patterns
   |         |
   |         pattern doesn't bind `Ban`
   |
help: you might have meant to use the similarly named unit variant `Bar`
   |
LL -         (Foo, _) | (Ban, Foo) => {}
LL +         (Foo, _) | (Bar, Foo) => {}
   |
```

For items that are not in the immedate scope, suggest the full path for them:

```
error[E0408]: variable `Non` is not bound in all patterns
  --> $DIR/binding-typo-2.rs:51:16
   |
LL |         (Non | Some(_))=> {}
   |          ---   ^^^^^^^ pattern doesn't bind `Non`
   |          |
   |          variable not in all patterns
   |
help: you might have meant to use the similarly named unit variant `None`
   |
LL -         (Non | Some(_))=> {}
LL +         (core::option::Option::None | Some(_))=> {}
   |
```
2025-08-30 02:54:46 +00:00

37 lines
1.1 KiB
Rust

// Issue #51976
//@ run-rustfix
#![deny(unused_variables)] //~ NOTE: the lint level is defined here
enum Lol {
Foo,
Bar,
}
fn foo(x: (Lol, Lol)) {
use Lol::*;
match &x {
(Foo, Bar) | (Ban, Foo) => {}
//~^ ERROR: variable `Ban` is not bound in all patterns
//~| HELP: you might have meant to use the similarly named previously used binding `Bar`
//~| NOTE: pattern doesn't bind `Ban`
//~| NOTE: variable not in all patterns
//~| ERROR: variable `Ban` is assigned to, but never used
//~| NOTE: consider using `_Ban` instead
_ => {}
}
match &x {
(Foo, _) | (Ban, Foo) => {}
//~^ ERROR: variable `Ban` is not bound in all patterns
//~| HELP: you might have meant to use the similarly named unit variant `Bar`
//~| NOTE: pattern doesn't bind `Ban`
//~| NOTE: variable not in all patterns
//~| ERROR: variable `Ban` is assigned to, but never used
//~| NOTE: consider using `_Ban` instead
_ => {}
}
}
fn main() {
use Lol::*;
foo((Foo, Bar));
}