Skip to content

Do not consider fields matched by wildcard patterns to be used #26500

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

Merged
merged 1 commit into from
Jun 22, 2015
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions src/librustc/middle/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
};
let fields = ty::lookup_struct_fields(self.tcx, id);
for pat in pats {
if let ast::PatWild(ast::PatWildSingle) = pat.node.pat.node {
continue;
}
let field_id = fields.iter()
.find(|field| field.name == pat.node.ident.name).unwrap().id;
self.live_symbols.insert(field_id.node);
Expand Down
10 changes: 6 additions & 4 deletions src/test/compile-fail/lint-dead-code-4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,23 @@ enum XYZ {
X, //~ ERROR variant is never used
Y { //~ ERROR variant is never used
a: String,
b: isize //~ ERROR: struct field is never used
b: i32, //~ ERROR: struct field is never used
c: i32, //~ ERROR: struct field is never used
},
Z
}

fn field_match_in_patterns(b: XYZ) -> String {
match b {
XYZ::Y { a, .. } => a,
XYZ::Y { a, b: _, .. } => a,
_ => "".to_string()
}
}

struct Bar {
x: usize, //~ ERROR: struct field is never used
b: bool,
c: bool, //~ ERROR: struct field is never used
_guard: ()
}

Expand All @@ -49,13 +51,13 @@ struct Baz {
}

fn field_match_in_let(f: Bar) -> bool {
let Bar { b, .. } = f;
let Bar { b, c: _, .. } = f;
b
}

fn main() {
field_read(Foo { x: 1, b: false });
field_match_in_patterns(XYZ::Z);
field_match_in_let(Bar { x: 42, b: true, _guard: () });
field_match_in_let(Bar { x: 42, b: true, c: false, _guard: () });
let _ = Baz { x: 0 };
}