From 4f639854121261f55863dbf2a88c626abb07247a Mon Sep 17 00:00:00 2001 From: LeSeulArtichaut Date: Wed, 1 Jan 2020 18:40:13 +0100 Subject: [PATCH 1/5] Warn for bindings named same as variants when matching against a borrow --- src/librustc_mir/hair/pattern/check_match.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc_mir/hair/pattern/check_match.rs b/src/librustc_mir/hair/pattern/check_match.rs index 12d75033da2cf..bbbad36e4851d 100644 --- a/src/librustc_mir/hair/pattern/check_match.rs +++ b/src/librustc_mir/hair/pattern/check_match.rs @@ -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| { From f474c0084a23207f89bd082b441bfb4701c8c2dc Mon Sep 17 00:00:00 2001 From: LeSeulArtichaut Date: Thu, 2 Jan 2020 00:48:58 +0100 Subject: [PATCH 2/5] Added test --- .../ui/match/match-same-name-enum-variant.rs | 33 +++++++++++++++++++ .../match/match-same-name-enum-variant.stderr | 24 ++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 src/test/ui/match/match-same-name-enum-variant.rs create mode 100644 src/test/ui/match/match-same-name-enum-variant.stderr diff --git a/src/test/ui/match/match-same-name-enum-variant.rs b/src/test/ui/match/match-same-name-enum-variant.rs new file mode 100644 index 0000000000000..d106bf713412e --- /dev/null +++ b/src/test/ui/match/match-same-name-enum-variant.rs @@ -0,0 +1,33 @@ +// Test for issue #67776: binding named the same as enum variant +// should report a warning even when matching against a borrow + +// check-pass + +#![allow(unused_variables)] +#![allow(non_snake_case)] + +enum Foo { + Bar, + Baz, +} + + +fn fn2(e: Foo) { + match e { + Bar => println!("A"), + //~^ WARNING named the same as one of the variants of the type `Foo` + Baz => println!("B"), + //~^ WARNING named the same as one of the variants of the type `Foo` + } +} + +fn fn1(e: &Foo) { + match e { + Bar => println!("A"), + //~^ WARNING named the same as one of the variants of the type `Foo` + Baz => println!("B"), + //~^ WARNING named the same as one of the variants of the type `Foo` + } +} + +fn main() {} diff --git a/src/test/ui/match/match-same-name-enum-variant.stderr b/src/test/ui/match/match-same-name-enum-variant.stderr new file mode 100644 index 0000000000000..cadf5e6cdeeab --- /dev/null +++ b/src/test/ui/match/match-same-name-enum-variant.stderr @@ -0,0 +1,24 @@ +warning[E0170]: pattern binding `Bar` is named the same as one of the variants of the type `Foo` + --> $DIR/match-same-name-enum-variant.rs:14:9 + | +LL | Bar => println!("A"), + | ^^^ 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/match-same-name-enum-variant.rs:16:9 + | +LL | Baz => println!("B"), + | ^^^ 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/match-same-name-enum-variant.rs:23:9 + | +LL | Bar => println!("A"), + | ^^^ 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/match-same-name-enum-variant.rs:25:9 + | +LL | Baz => println!("B"), + | ^^^ help: to match on the variant, qualify the path: `Foo::Baz` + From 318280519d7a721e36d9508ab185e85bde21e2f5 Mon Sep 17 00:00:00 2001 From: LeSeulArtichaut Date: Thu, 2 Jan 2020 20:13:40 +0100 Subject: [PATCH 3/5] Move test Co-authored-by: Centril --- .../issue-67776-match-same-name-enum-variant-refs.rs} | 0 .../issue-67776-match-same-name-enum-variant-refs.stderr} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename src/test/ui/{match/match-same-name-enum-variant.rs => pattern/issue-67776-match-same-name-enum-variant-refs.rs} (100%) rename src/test/ui/{match/match-same-name-enum-variant.stderr => pattern/issue-67776-match-same-name-enum-variant-refs.stderr} (100%) diff --git a/src/test/ui/match/match-same-name-enum-variant.rs b/src/test/ui/pattern/issue-67776-match-same-name-enum-variant-refs.rs similarity index 100% rename from src/test/ui/match/match-same-name-enum-variant.rs rename to src/test/ui/pattern/issue-67776-match-same-name-enum-variant-refs.rs diff --git a/src/test/ui/match/match-same-name-enum-variant.stderr b/src/test/ui/pattern/issue-67776-match-same-name-enum-variant-refs.stderr similarity index 100% rename from src/test/ui/match/match-same-name-enum-variant.stderr rename to src/test/ui/pattern/issue-67776-match-same-name-enum-variant-refs.stderr From dc19b4842a2842916bca601b83abfdf1f5ae4395 Mon Sep 17 00:00:00 2001 From: LeSeulArtichaut Date: Thu, 2 Jan 2020 21:08:25 +0100 Subject: [PATCH 4/5] Enhance test Co-authored-by: Centril --- ...67776-match-same-name-enum-variant-refs.rs | 21 ++++++++++---- ...6-match-same-name-enum-variant-refs.stderr | 28 +++++++++++++------ 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/test/ui/pattern/issue-67776-match-same-name-enum-variant-refs.rs b/src/test/ui/pattern/issue-67776-match-same-name-enum-variant-refs.rs index d106bf713412e..5e6a97b3ff7dd 100644 --- a/src/test/ui/pattern/issue-67776-match-same-name-enum-variant-refs.rs +++ b/src/test/ui/pattern/issue-67776-match-same-name-enum-variant-refs.rs @@ -12,20 +12,29 @@ enum Foo { } -fn fn2(e: Foo) { +fn fn1(e: Foo) { match e { - Bar => println!("A"), + Bar => {}, //~^ WARNING named the same as one of the variants of the type `Foo` - Baz => println!("B"), + Baz => {}, //~^ WARNING named the same as one of the variants of the type `Foo` } } -fn fn1(e: &Foo) { +fn fn2(e: &Foo) { match e { - Bar => println!("A"), + Bar => {}, //~^ WARNING named the same as one of the variants of the type `Foo` - Baz => println!("B"), + 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` } } diff --git a/src/test/ui/pattern/issue-67776-match-same-name-enum-variant-refs.stderr b/src/test/ui/pattern/issue-67776-match-same-name-enum-variant-refs.stderr index cadf5e6cdeeab..abb8d6907e76d 100644 --- a/src/test/ui/pattern/issue-67776-match-same-name-enum-variant-refs.stderr +++ b/src/test/ui/pattern/issue-67776-match-same-name-enum-variant-refs.stderr @@ -1,24 +1,36 @@ warning[E0170]: pattern binding `Bar` is named the same as one of the variants of the type `Foo` - --> $DIR/match-same-name-enum-variant.rs:14:9 + --> $DIR/issue-67776-match-same-name-enum-variant-refs.rs:17:9 | -LL | Bar => println!("A"), +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/match-same-name-enum-variant.rs:16:9 + --> $DIR/issue-67776-match-same-name-enum-variant-refs.rs:19:9 | -LL | Baz => println!("B"), +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/match-same-name-enum-variant.rs:23:9 + --> $DIR/issue-67776-match-same-name-enum-variant-refs.rs:26:9 | -LL | Bar => println!("A"), +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/match-same-name-enum-variant.rs:25:9 + --> $DIR/issue-67776-match-same-name-enum-variant-refs.rs:28:9 | -LL | Baz => println!("B"), +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` From 5cc9f6b70681617300655561b9ff49cc5e4a2a04 Mon Sep 17 00:00:00 2001 From: LeSeulArtichaut Date: Thu, 2 Jan 2020 21:11:43 +0100 Subject: [PATCH 5/5] Reformulate test description Co-authored-by: Centril --- .../ui/pattern/issue-67776-match-same-name-enum-variant-refs.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/ui/pattern/issue-67776-match-same-name-enum-variant-refs.rs b/src/test/ui/pattern/issue-67776-match-same-name-enum-variant-refs.rs index 5e6a97b3ff7dd..6fd5768a5a26d 100644 --- a/src/test/ui/pattern/issue-67776-match-same-name-enum-variant-refs.rs +++ b/src/test/ui/pattern/issue-67776-match-same-name-enum-variant-refs.rs @@ -1,5 +1,5 @@ // Test for issue #67776: binding named the same as enum variant -// should report a warning even when matching against a borrow +// should report a warning even when matching against a reference type // check-pass