Skip to content

Commit 7affcd5

Browse files
authored
Rollup merge of #67783 - LeSeulArtichaut:pattern-ref-warning, r=Centril
Warn for bindings named same as variants when matching against a borrow Fixes #67776
2 parents aefc3cd + 5cc9f6b commit 7affcd5

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

src/librustc_mir/hair/pattern/check_match.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ fn check_for_bindings_named_same_as_variants(cx: &MatchVisitor<'_, '_>, pat: &Pa
284284
if let Some(ty::BindByValue(hir::Mutability::Not)) =
285285
cx.tables.extract_binding_mode(cx.tcx.sess, p.hir_id, p.span)
286286
{
287-
let pat_ty = cx.tables.pat_ty(p);
287+
let pat_ty = cx.tables.pat_ty(p).peel_refs();
288288
if let ty::Adt(edef, _) = pat_ty.kind {
289289
if edef.is_enum()
290290
&& edef.variants.iter().any(|variant| {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Test for issue #67776: binding named the same as enum variant
2+
// should report a warning even when matching against a reference type
3+
4+
// check-pass
5+
6+
#![allow(unused_variables)]
7+
#![allow(non_snake_case)]
8+
9+
enum Foo {
10+
Bar,
11+
Baz,
12+
}
13+
14+
15+
fn fn1(e: Foo) {
16+
match e {
17+
Bar => {},
18+
//~^ WARNING named the same as one of the variants of the type `Foo`
19+
Baz => {},
20+
//~^ WARNING named the same as one of the variants of the type `Foo`
21+
}
22+
}
23+
24+
fn fn2(e: &Foo) {
25+
match e {
26+
Bar => {},
27+
//~^ WARNING named the same as one of the variants of the type `Foo`
28+
Baz => {},
29+
//~^ WARNING named the same as one of the variants of the type `Foo`
30+
}
31+
}
32+
33+
fn fn3(e: &mut &&mut Foo) {
34+
match e {
35+
Bar => {},
36+
//~^ WARNING named the same as one of the variants of the type `Foo`
37+
Baz => {},
38+
//~^ WARNING named the same as one of the variants of the type `Foo`
39+
}
40+
}
41+
42+
fn main() {}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
warning[E0170]: pattern binding `Bar` is named the same as one of the variants of the type `Foo`
2+
--> $DIR/issue-67776-match-same-name-enum-variant-refs.rs:17:9
3+
|
4+
LL | Bar => {},
5+
| ^^^ help: to match on the variant, qualify the path: `Foo::Bar`
6+
7+
warning[E0170]: pattern binding `Baz` is named the same as one of the variants of the type `Foo`
8+
--> $DIR/issue-67776-match-same-name-enum-variant-refs.rs:19:9
9+
|
10+
LL | Baz => {},
11+
| ^^^ help: to match on the variant, qualify the path: `Foo::Baz`
12+
13+
warning[E0170]: pattern binding `Bar` is named the same as one of the variants of the type `Foo`
14+
--> $DIR/issue-67776-match-same-name-enum-variant-refs.rs:26:9
15+
|
16+
LL | Bar => {},
17+
| ^^^ help: to match on the variant, qualify the path: `Foo::Bar`
18+
19+
warning[E0170]: pattern binding `Baz` is named the same as one of the variants of the type `Foo`
20+
--> $DIR/issue-67776-match-same-name-enum-variant-refs.rs:28:9
21+
|
22+
LL | Baz => {},
23+
| ^^^ help: to match on the variant, qualify the path: `Foo::Baz`
24+
25+
warning[E0170]: pattern binding `Bar` is named the same as one of the variants of the type `Foo`
26+
--> $DIR/issue-67776-match-same-name-enum-variant-refs.rs:35:9
27+
|
28+
LL | Bar => {},
29+
| ^^^ help: to match on the variant, qualify the path: `Foo::Bar`
30+
31+
warning[E0170]: pattern binding `Baz` is named the same as one of the variants of the type `Foo`
32+
--> $DIR/issue-67776-match-same-name-enum-variant-refs.rs:37:9
33+
|
34+
LL | Baz => {},
35+
| ^^^ help: to match on the variant, qualify the path: `Foo::Baz`
36+

0 commit comments

Comments
 (0)