Rollup merge of #67783 - LeSeulArtichaut:pattern-ref-warning, r=Centril
Warn for bindings named same as variants when matching against a borrow Fixes #67776
This commit is contained in:
commit
7affcd5394
3 changed files with 79 additions and 1 deletions
|
|
@ -284,7 +284,7 @@ fn check_for_bindings_named_same_as_variants(cx: &MatchVisitor<'_, '_>, pat: &Pa
|
|||
if let Some(ty::BindByValue(hir::Mutability::Not)) =
|
||||
cx.tables.extract_binding_mode(cx.tcx.sess, p.hir_id, p.span)
|
||||
{
|
||||
let pat_ty = cx.tables.pat_ty(p);
|
||||
let pat_ty = cx.tables.pat_ty(p).peel_refs();
|
||||
if let ty::Adt(edef, _) = pat_ty.kind {
|
||||
if edef.is_enum()
|
||||
&& edef.variants.iter().any(|variant| {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,42 @@
|
|||
// Test for issue #67776: binding named the same as enum variant
|
||||
// should report a warning even when matching against a reference type
|
||||
|
||||
// check-pass
|
||||
|
||||
#![allow(unused_variables)]
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
enum Foo {
|
||||
Bar,
|
||||
Baz,
|
||||
}
|
||||
|
||||
|
||||
fn fn1(e: Foo) {
|
||||
match e {
|
||||
Bar => {},
|
||||
//~^ WARNING named the same as one of the variants of the type `Foo`
|
||||
Baz => {},
|
||||
//~^ WARNING named the same as one of the variants of the type `Foo`
|
||||
}
|
||||
}
|
||||
|
||||
fn fn2(e: &Foo) {
|
||||
match e {
|
||||
Bar => {},
|
||||
//~^ WARNING named the same as one of the variants of the type `Foo`
|
||||
Baz => {},
|
||||
//~^ WARNING named the same as one of the variants of the type `Foo`
|
||||
}
|
||||
}
|
||||
|
||||
fn fn3(e: &mut &&mut Foo) {
|
||||
match e {
|
||||
Bar => {},
|
||||
//~^ WARNING named the same as one of the variants of the type `Foo`
|
||||
Baz => {},
|
||||
//~^ WARNING named the same as one of the variants of the type `Foo`
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
warning[E0170]: pattern binding `Bar` is named the same as one of the variants of the type `Foo`
|
||||
--> $DIR/issue-67776-match-same-name-enum-variant-refs.rs:17:9
|
||||
|
|
||||
LL | Bar => {},
|
||||
| ^^^ help: to match on the variant, qualify the path: `Foo::Bar`
|
||||
|
||||
warning[E0170]: pattern binding `Baz` is named the same as one of the variants of the type `Foo`
|
||||
--> $DIR/issue-67776-match-same-name-enum-variant-refs.rs:19:9
|
||||
|
|
||||
LL | Baz => {},
|
||||
| ^^^ help: to match on the variant, qualify the path: `Foo::Baz`
|
||||
|
||||
warning[E0170]: pattern binding `Bar` is named the same as one of the variants of the type `Foo`
|
||||
--> $DIR/issue-67776-match-same-name-enum-variant-refs.rs:26:9
|
||||
|
|
||||
LL | Bar => {},
|
||||
| ^^^ help: to match on the variant, qualify the path: `Foo::Bar`
|
||||
|
||||
warning[E0170]: pattern binding `Baz` is named the same as one of the variants of the type `Foo`
|
||||
--> $DIR/issue-67776-match-same-name-enum-variant-refs.rs:28:9
|
||||
|
|
||||
LL | Baz => {},
|
||||
| ^^^ help: to match on the variant, qualify the path: `Foo::Baz`
|
||||
|
||||
warning[E0170]: pattern binding `Bar` is named the same as one of the variants of the type `Foo`
|
||||
--> $DIR/issue-67776-match-same-name-enum-variant-refs.rs:35:9
|
||||
|
|
||||
LL | Bar => {},
|
||||
| ^^^ help: to match on the variant, qualify the path: `Foo::Bar`
|
||||
|
||||
warning[E0170]: pattern binding `Baz` is named the same as one of the variants of the type `Foo`
|
||||
--> $DIR/issue-67776-match-same-name-enum-variant-refs.rs:37:9
|
||||
|
|
||||
LL | Baz => {},
|
||||
| ^^^ help: to match on the variant, qualify the path: `Foo::Baz`
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue