Skip to content

Commit cc53db8

Browse files
committed
Correct unused field warning on &struct match
1 parent a997525 commit cc53db8

File tree

3 files changed

+41
-13
lines changed

3 files changed

+41
-13
lines changed

src/librustc/middle/liveness.rs

+19-9
Original file line numberDiff line numberDiff line change
@@ -412,18 +412,28 @@ fn visit_local<'a, 'tcx>(ir: &mut IrMaps<'a, 'tcx>, local: &'tcx hir::Local) {
412412
}
413413

414414
fn visit_arm<'a, 'tcx>(ir: &mut IrMaps<'a, 'tcx>, arm: &'tcx hir::Arm) {
415-
for pat in &arm.pats {
416-
// for struct patterns, take note of which fields used shorthand (`x` rather than `x: x`)
415+
for mut pat in &arm.pats {
416+
// For struct patterns, take note of which fields used shorthand
417+
// (`x` rather than `x: x`).
417418
//
418-
// FIXME: according to the rust-lang-nursery/rustc-guide book, `NodeId`s are to be phased
419-
// out in favor of `HirId`s; however, we need to match the signature of `each_binding`,
420-
// which uses `NodeIds`.
419+
// FIXME: according to the rust-lang-nursery/rustc-guide book, `NodeId`s are to be
420+
// phased out in favor of `HirId`s; however, we need to match the signature of
421+
// `each_binding`, which uses `NodeIds`.
421422
let mut shorthand_field_ids = NodeSet();
422-
if let hir::PatKind::Struct(_, ref fields, _) = pat.node {
423-
for field in fields {
424-
if field.node.is_shorthand {
425-
shorthand_field_ids.insert(field.node.pat.id);
423+
loop {
424+
match pat.node {
425+
hir::PatKind::Struct(_, ref fields, _) => {
426+
for field in fields {
427+
if field.node.is_shorthand {
428+
shorthand_field_ids.insert(field.node.pat.id);
429+
}
430+
}
431+
break;
432+
}
433+
hir::PatKind::Ref(ref deref_pat, _) => {
434+
pat = deref_pat;
426435
}
436+
_ => break
427437
}
428438
}
429439

src/test/ui/lint/issue-47390-unused-variable-in-struct-pattern.rs

+12
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ struct SoulHistory {
1818
endless_and_singing: bool
1919
}
2020

21+
enum Large {
22+
Suit { case: () }
23+
}
24+
2125
fn main() {
2226
let i_think_continually = 2;
2327
let who_from_the_womb_remembered = SoulHistory {
@@ -31,4 +35,12 @@ fn main() {
3135
endless_and_singing: true } = who_from_the_womb_remembered {
3236
hours_are_suns = false;
3337
}
38+
39+
let bag = &Large::Suit {
40+
case: ()
41+
};
42+
43+
match bag {
44+
&Large::Suit { case } => {}
45+
};
3446
}

src/test/ui/lint/issue-47390-unused-variable-in-struct-pattern.stderr

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
warning: unused variable: `i_think_continually`
2-
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:22:9
2+
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:26:9
33
|
44
LL | let i_think_continually = 2;
55
| ^^^^^^^^^^^^^^^^^^^ help: consider using `_i_think_continually` instead
@@ -12,21 +12,21 @@ LL | #![warn(unused)] // UI tests pass `-A unused` (#43896)
1212
= note: #[warn(unused_variables)] implied by #[warn(unused)]
1313

1414
warning: unused variable: `corridors_of_light`
15-
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:29:26
15+
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:33:26
1616
|
1717
LL | if let SoulHistory { corridors_of_light,
1818
| ^^^^^^^^^^^^^^^^^^ help: try ignoring the field: `corridors_of_light: _`
1919

2020
warning: variable `hours_are_suns` is assigned to, but never used
21-
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:30:26
21+
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:34:26
2222
|
2323
LL | mut hours_are_suns,
2424
| ^^^^^^^^^^^^^^^^^^
2525
|
2626
= note: consider using `_hours_are_suns` instead
2727

2828
warning: value assigned to `hours_are_suns` is never read
29-
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:32:9
29+
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:36:9
3030
|
3131
LL | hours_are_suns = false;
3232
| ^^^^^^^^^^^^^^
@@ -38,3 +38,9 @@ LL | #![warn(unused)] // UI tests pass `-A unused` (#43896)
3838
| ^^^^^^
3939
= note: #[warn(unused_assignments)] implied by #[warn(unused)]
4040

41+
warning: unused variable: `case`
42+
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:44:24
43+
|
44+
LL | &Large::Suit { case } => {}
45+
| ^^^^ help: try ignoring the field: `case: _`
46+

0 commit comments

Comments
 (0)