Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from
Jan 3, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
33 changes: 33 additions & 0 deletions src/test/ui/match/match-same-name-enum-variant.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Test for issue #67776: binding named the same as enum variant
LeSeulArtichaut marked this conversation as resolved.
Show resolved Hide resolved
// should report a warning even when matching against a borrow
LeSeulArtichaut marked this conversation as resolved.
Show resolved Hide resolved

// check-pass

#![allow(unused_variables)]
#![allow(non_snake_case)]

enum Foo {
Bar,
Baz,
}


fn fn2(e: Foo) {
match e {
Bar => println!("A"),
LeSeulArtichaut marked this conversation as resolved.
Show resolved Hide resolved
//~^ 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) {
LeSeulArtichaut marked this conversation as resolved.
Show resolved Hide resolved
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() {}
24 changes: 24 additions & 0 deletions src/test/ui/match/match-same-name-enum-variant.stderr
Original file line number Diff line number Diff line change
@@ -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`