diff options
| author | Esteban Küber <esteban@kuber.com.ar> | 2025-08-30 16:24:26 +0000 |
|---|---|---|
| committer | Esteban Küber <esteban@kuber.com.ar> | 2025-08-30 16:24:26 +0000 |
| commit | 00f6fe1b6cbb211c4396b306566816f91a4ed371 (patch) | |
| tree | ec70227ecaf84c69e675d493f61f8dd784bbc600 /tests/ui/or-patterns | |
| parent | cb9cd8f8306e6e1b16661704aed8d6f09aa5db73 (diff) | |
| download | rust-00f6fe1b6cbb211c4396b306566816f91a4ed371.tar.gz rust-00f6fe1b6cbb211c4396b306566816f91a4ed371.zip | |
On unused binding in pattern, suggest unit struct/variant with similar name
When encountering a typo in a pattern that gets interpreted as an unused binding, look for unit struct/variant of the same type as the binding:
```
error: unused variable: `Non`
--> $DIR/binding-typo-2.rs:36:9
|
LL | Non => {}
| ^^^
|
help: if this is intentional, prefix it with an underscore
|
LL | _Non => {}
| +
help: you might have meant to pattern match on the similarly named variant `None`
|
LL - Non => {}
LL + std::prelude::v1::None => {}
|
```
Diffstat (limited to 'tests/ui/or-patterns')
| -rw-r--r-- | tests/ui/or-patterns/binding-typo-2.rs | 25 | ||||
| -rw-r--r-- | tests/ui/or-patterns/binding-typo-2.stderr | 197 | ||||
| -rw-r--r-- | tests/ui/or-patterns/binding-typo.fixed | 6 | ||||
| -rw-r--r-- | tests/ui/or-patterns/binding-typo.rs | 6 | ||||
| -rw-r--r-- | tests/ui/or-patterns/binding-typo.stderr | 25 |
5 files changed, 189 insertions, 70 deletions
diff --git a/tests/ui/or-patterns/binding-typo-2.rs b/tests/ui/or-patterns/binding-typo-2.rs index 4dfb84d4f0c..d763ce60ce9 100644 --- a/tests/ui/or-patterns/binding-typo-2.rs +++ b/tests/ui/or-patterns/binding-typo-2.rs @@ -17,6 +17,7 @@ fn foo(x: (Lol, Lol)) { //~| NOTE: variable not in all patterns //~| ERROR: variable `Ban` is assigned to, but never used //~| NOTE: consider using `_Ban` instead + //~| HELP: you might have meant to pattern match on the similarly named _ => {} } match &x { @@ -27,15 +28,18 @@ fn foo(x: (Lol, Lol)) { //~| NOTE: variable not in all patterns //~| ERROR: variable `Ban` is assigned to, but never used //~| NOTE: consider using `_Ban` instead + //~| HELP: you might have meant to pattern match on the similarly named _ => {} } match Some(42) { + Some(_) => {} Non => {} //~^ ERROR: unused variable: `Non` //~| HELP: if this is intentional, prefix it with an underscore - _ => {} + //~| HELP: you might have meant to pattern match on the similarly named } match Some(42) { + Some(_) => {} Non | None => {} //~^ ERROR: unused variable: `Non` //~| HELP: if this is intentional, prefix it with an underscore @@ -43,7 +47,7 @@ fn foo(x: (Lol, Lol)) { //~| NOTE: pattern doesn't bind `Non` //~| NOTE: variable not in all patterns //~| HELP: you might have meant to use the similarly named previously used binding `None` - _ => {} + //~| HELP: you might have meant to pattern match on the similarly named } match Some(42) { Non | Some(_) => {} @@ -53,14 +57,13 @@ fn foo(x: (Lol, Lol)) { //~| NOTE: pattern doesn't bind `Non` //~| NOTE: variable not in all patterns //~| HELP: you might have meant to use the similarly named unit variant `None` - _ => {} + //~| HELP: you might have meant to pattern match on the similarly named } } fn bar(x: (Lol, Lol)) { use Lol::*; use Bat; use Bay; - use std::option::Option::None; match &x { (Foo, _) | (Ban, Foo) => {} //~^ ERROR: variable `Ban` is not bound in all patterns @@ -71,6 +74,7 @@ fn bar(x: (Lol, Lol)) { //~| NOTE: variable not in all patterns //~| ERROR: variable `Ban` is assigned to, but never used //~| NOTE: consider using `_Ban` instead + //~| HELP: you might have meant to pattern match on the similarly named _ => {} } } @@ -86,8 +90,21 @@ fn baz(x: (Lol, Lol)) { //~| NOTE: variable not in all patterns //~| ERROR: variable `Ban` is assigned to, but never used //~| NOTE: consider using `_Ban` instead + //~| HELP: you might have meant to pattern match on the similarly named _ => {} } + match &x { + (Ban, _) => {} + //~^ ERROR: unused variable: `Ban` + //~| HELP: if this is intentional, prefix it with an underscore + //~| HELP: you might have meant to pattern match on the similarly named + } + match Bay { + Ban => {} + //~^ ERROR: unused variable: `Ban` + //~| HELP: if this is intentional, prefix it with an underscore + //~| HELP: you might have meant to pattern match on the similarly named + } } fn main() { diff --git a/tests/ui/or-patterns/binding-typo-2.stderr b/tests/ui/or-patterns/binding-typo-2.stderr index a2099572485..d683cb2b121 100644 --- a/tests/ui/or-patterns/binding-typo-2.stderr +++ b/tests/ui/or-patterns/binding-typo-2.stderr @@ -13,7 +13,48 @@ LL + (Foo, Bar) | (Bar, Foo) => {} | error[E0408]: variable `Ban` is not bound in all patterns - --> $DIR/binding-typo-2.rs:23:9 + --> $DIR/binding-typo-2.rs:24: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) => {} + | + +error[E0408]: variable `Non` is not bound in all patterns + --> $DIR/binding-typo-2.rs:43:15 + | +LL | Non | None => {} + | --- ^^^^ pattern doesn't bind `Non` + | | + | variable not in all patterns + | +help: you might have meant to use the similarly named previously used binding `None` + | +LL | None | None => {} + | + + +error[E0408]: variable `Non` is not bound in all patterns + --> $DIR/binding-typo-2.rs:53:15 + | +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(_) => {} + | + +error[E0408]: variable `Ban` is not bound in all patterns + --> $DIR/binding-typo-2.rs:68:9 | LL | (Foo, _) | (Ban, Foo) => {} | ^^^^^^^^ --- variable not in all patterns @@ -28,39 +69,31 @@ LL + (Foo, _) | (Bar, Foo) => {} help: you might have meant to use the similarly named unit struct `Bay` | LL - (Foo, _) | (Ban, Foo) => {} -LL + (Foo, _) | (::Bay, Foo) => {} +LL + (Foo, _) | (Bay, Foo) => {} | help: you might have meant to use the similarly named constant `Bat` | LL - (Foo, _) | (Ban, Foo) => {} -LL + (Foo, _) | (::Bat, Foo) => {} +LL + (Foo, _) | (Bat, Foo) => {} | -error[E0408]: variable `Non` is not bound in all patterns - --> $DIR/binding-typo-2.rs:41:16 - | -LL | (Non | None)=> {} - | --- ^^^^ pattern doesn't bind `Non` - | | - | variable not in all patterns +error[E0408]: variable `Ban` is not bound in all patterns + --> $DIR/binding-typo-2.rs:85:9 | -help: you might have meant to use the similarly named previously used binding `None` +LL | (Foo, _) | (Ban, Foo) => {} + | ^^^^^^^^ --- variable not in all patterns + | | + | pattern doesn't bind `Ban` | -LL | (None | None)=> {} - | + - -error[E0408]: variable `Non` is not bound in all patterns - --> $DIR/binding-typo-2.rs:51:16 +help: you might have meant to use the similarly named unit variant `Bar` | -LL | (Non | Some(_))=> {} - | --- ^^^^^^^ pattern doesn't bind `Non` - | | - | variable not in all patterns +LL - (Foo, _) | (Ban, Foo) => {} +LL + (Foo, _) | (Bar, Foo) => {} | -help: you might have meant to use the similarly named unit variant `None` +help: you might have meant to use the similarly named constant `Bat` | -LL - (Non | Some(_))=> {} -LL + (core::option::Option::None | Some(_))=> {} +LL - (Foo, _) | (Ban, Foo) => {} +LL + (Foo, _) | (Bat, Foo) => {} | error: variable `Ban` is assigned to, but never used @@ -75,33 +108,131 @@ note: the lint level is defined here | LL | #![deny(unused_variables)] | ^^^^^^^^^^^^^^^^ +help: you might have meant to pattern match on the similarly named variant `Bar` + | +LL - (Foo, Bar) | (Ban, Foo) => {} +LL + (Foo, Bar) | (Lol::Bar, Foo) => {} + | error: variable `Ban` is assigned to, but never used - --> $DIR/binding-typo-2.rs:23:21 + --> $DIR/binding-typo-2.rs:24:21 | LL | (Foo, _) | (Ban, Foo) => {} | ^^^ | = note: consider using `_Ban` instead +help: you might have meant to pattern match on the similarly named variant `Bar` + | +LL - (Foo, _) | (Ban, Foo) => {} +LL + (Foo, _) | (Lol::Bar, Foo) => {} + | error: unused variable: `Non` - --> $DIR/binding-typo-2.rs:35:9 + --> $DIR/binding-typo-2.rs:36:9 | LL | Non => {} - | ^^^ help: if this is intentional, prefix it with an underscore: `_Non` + | ^^^ + | +help: if this is intentional, prefix it with an underscore + | +LL | _Non => {} + | + +help: you might have meant to pattern match on the similarly named variant `None` + | +LL - Non => {} +LL + std::prelude::v1::None => {} + | error: unused variable: `Non` - --> $DIR/binding-typo-2.rs:41:10 + --> $DIR/binding-typo-2.rs:43:9 + | +LL | Non | None => {} + | ^^^ + | +help: if this is intentional, prefix it with an underscore + | +LL | _Non | None => {} + | + +help: you might have meant to pattern match on the similarly named variant `None` + | +LL - Non | None => {} +LL + std::prelude::v1::None | None => {} | -LL | (Non | None)=> {} - | ^^^ help: if this is intentional, prefix it with an underscore: `_Non` error: unused variable: `Non` - --> $DIR/binding-typo-2.rs:51:10 + --> $DIR/binding-typo-2.rs:53:9 + | +LL | Non | Some(_) => {} + | ^^^ + | +help: if this is intentional, prefix it with an underscore + | +LL | _Non | Some(_) => {} + | + +help: you might have meant to pattern match on the similarly named variant `None` + | +LL - Non | Some(_) => {} +LL + std::prelude::v1::None | Some(_) => {} + | + +error: variable `Ban` is assigned to, but never used + --> $DIR/binding-typo-2.rs:68:21 + | +LL | (Foo, _) | (Ban, Foo) => {} + | ^^^ + | + = note: consider using `_Ban` instead +help: you might have meant to pattern match on the similarly named variant `Bar` + | +LL - (Foo, _) | (Ban, Foo) => {} +LL + (Foo, _) | (Lol::Bar, Foo) => {} + | + +error: variable `Ban` is assigned to, but never used + --> $DIR/binding-typo-2.rs:85:21 + | +LL | (Foo, _) | (Ban, Foo) => {} + | ^^^ + | + = note: consider using `_Ban` instead +help: you might have meant to pattern match on the similarly named variant `Bar` + | +LL - (Foo, _) | (Ban, Foo) => {} +LL + (Foo, _) | (Lol::Bar, Foo) => {} + | + +error: unused variable: `Ban` + --> $DIR/binding-typo-2.rs:97:10 + | +LL | (Ban, _) => {} + | ^^^ + | +help: if this is intentional, prefix it with an underscore + | +LL | (_Ban, _) => {} + | + +help: you might have meant to pattern match on the similarly named variant `Bar` + | +LL - (Ban, _) => {} +LL + (Lol::Bar, _) => {} + | + +error: unused variable: `Ban` + --> $DIR/binding-typo-2.rs:103:9 + | +LL | Ban => {} + | ^^^ + | +help: if this is intentional, prefix it with an underscore + | +LL | _Ban => {} + | + +help: you might have meant to pattern match on the similarly named struct `Bay` + | +LL - Ban => {} +LL + Bay => {} | -LL | (Non | Some(_))=> {} - | ^^^ help: if this is intentional, prefix it with an underscore: `_Non` -error: aborting due to 9 previous errors +error: aborting due to 15 previous errors For more information about this error, try `rustc --explain E0408`. diff --git a/tests/ui/or-patterns/binding-typo.fixed b/tests/ui/or-patterns/binding-typo.fixed index 2902ca30393..f209ad644db 100644 --- a/tests/ui/or-patterns/binding-typo.fixed +++ b/tests/ui/or-patterns/binding-typo.fixed @@ -1,6 +1,6 @@ // Issue #51976 //@ run-rustfix -#![deny(unused_variables)] //~ NOTE: the lint level is defined here +#![allow(unused_variables)] // allowed so we don't get overlapping suggestions enum Lol { Foo, Bar, @@ -14,8 +14,6 @@ fn foo(x: (Lol, Lol)) { //~| 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 { @@ -24,8 +22,6 @@ fn foo(x: (Lol, Lol)) { //~| 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 _ => {} } } diff --git a/tests/ui/or-patterns/binding-typo.rs b/tests/ui/or-patterns/binding-typo.rs index 2ffe0e45c83..6be9b801a0d 100644 --- a/tests/ui/or-patterns/binding-typo.rs +++ b/tests/ui/or-patterns/binding-typo.rs @@ -1,6 +1,6 @@ // Issue #51976 //@ run-rustfix -#![deny(unused_variables)] //~ NOTE: the lint level is defined here +#![allow(unused_variables)] // allowed so we don't get overlapping suggestions enum Lol { Foo, Bar, @@ -14,8 +14,6 @@ fn foo(x: (Lol, Lol)) { //~| 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 { @@ -24,8 +22,6 @@ fn foo(x: (Lol, Lol)) { //~| 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 _ => {} } } diff --git a/tests/ui/or-patterns/binding-typo.stderr b/tests/ui/or-patterns/binding-typo.stderr index f90301d2cee..fb6d5f71209 100644 --- a/tests/ui/or-patterns/binding-typo.stderr +++ b/tests/ui/or-patterns/binding-typo.stderr @@ -13,7 +13,7 @@ LL + (Foo, Bar) | (Bar, Foo) => {} | error[E0408]: variable `Ban` is not bound in all patterns - --> $DIR/binding-typo.rs:22:9 + --> $DIR/binding-typo.rs:20:9 | LL | (Foo, _) | (Ban, Foo) => {} | ^^^^^^^^ --- variable not in all patterns @@ -26,27 +26,6 @@ LL - (Foo, _) | (Ban, Foo) => {} LL + (Foo, _) | (Bar, Foo) => {} | -error: variable `Ban` is assigned to, but never used - --> $DIR/binding-typo.rs:12:23 - | -LL | (Foo, Bar) | (Ban, Foo) => {} - | ^^^ - | - = note: consider using `_Ban` instead -note: the lint level is defined here - --> $DIR/binding-typo.rs:3:9 - | -LL | #![deny(unused_variables)] - | ^^^^^^^^^^^^^^^^ - -error: variable `Ban` is assigned to, but never used - --> $DIR/binding-typo.rs:22:21 - | -LL | (Foo, _) | (Ban, Foo) => {} - | ^^^ - | - = note: consider using `_Ban` instead - -error: aborting due to 4 previous errors +error: aborting due to 2 previous errors For more information about this error, try `rustc --explain E0408`. |
