Skip to content

Commit

Permalink
Rollup merge of rust-lang#67783 - LeSeulArtichaut:pattern-ref-warning…
Browse files Browse the repository at this point in the history
…, r=Centril

Warn for bindings named same as variants when matching against a borrow

Fixes rust-lang#67776
  • Loading branch information
JohnTitor authored Jan 3, 2020
2 parents aefc3cd + 5cc9f6b commit 7affcd5
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/librustc_mir/hair/pattern/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down
Original file line number Diff line number Diff line change
@@ -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() {}
Original file line number Diff line number Diff line change
@@ -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`

0 comments on commit 7affcd5

Please sign in to comment.