Skip to content

Commit 8235b6f

Browse files
committed
dead_code: Properly inspect fields in struct patterns with type relative paths
1 parent cf048cc commit 8235b6f

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

Diff for: src/librustc/middle/dead.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,9 @@ impl<'a, 'tcx> Visitor<'tcx> for MarkSymbolVisitor<'a, 'tcx> {
269269

270270
fn visit_pat(&mut self, pat: &'tcx hir::Pat) {
271271
match pat.node {
272-
PatKind::Struct(hir::QPath::Resolved(_, ref path), ref fields, _) => {
273-
self.handle_field_pattern_match(pat, path.res, fields);
272+
PatKind::Struct(ref path, ref fields, _) => {
273+
let res = self.tables.qpath_res(path, pat.hir_id);
274+
self.handle_field_pattern_match(pat, res, fields);
274275
}
275276
PatKind::Path(ref qpath @ hir::QPath::TypeRelative(..)) => {
276277
let res = self.tables.qpath_res(qpath, pat.hir_id);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// check-pass
2+
3+
// Regression test for the issue #63151:
4+
// Spurious unused field warning when matching variants under a `Self` scope
5+
//
6+
// This test checks that the `dead_code` lint properly inspects fields
7+
// in struct patterns that use a type relative path.
8+
9+
#![deny(dead_code)]
10+
11+
enum Enum {
12+
Variant { field: usize }
13+
}
14+
15+
impl Enum {
16+
fn read_field(self) -> usize {
17+
match self {
18+
Self::Variant { field } => field
19+
}
20+
}
21+
}
22+
23+
fn main() {
24+
let e = Enum::Variant { field: 42 };
25+
println!("{}", e.read_field());
26+
}

0 commit comments

Comments
 (0)