From fe8438d4a2c96295dcee95af415b9762c84ec6c6 Mon Sep 17 00:00:00 2001 From: Josh Triplett Date: Sat, 3 Sep 2016 15:29:16 -0700 Subject: [PATCH 1/2] Fix "field is never used" warning to take unions into account Rather than saying "struct or union" or adding logic to determine the type of the item, just change the message to "field is never used", dropping the "struct". Update tests accordingly. --- src/librustc/middle/dead.rs | 2 +- src/test/compile-fail/lint-dead-code-4.rs | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/librustc/middle/dead.rs b/src/librustc/middle/dead.rs index 0b1d9e8d8f69e..80d45def960d4 100644 --- a/src/librustc/middle/dead.rs +++ b/src/librustc/middle/dead.rs @@ -548,7 +548,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for DeadVisitor<'a, 'tcx> { fn visit_struct_field(&mut self, field: &hir::StructField) { if self.should_warn_about_field(&field) { self.warn_dead_code(field.id, field.span, - field.name, "struct field"); + field.name, "field"); } intravisit::walk_struct_field(self, field); diff --git a/src/test/compile-fail/lint-dead-code-4.rs b/src/test/compile-fail/lint-dead-code-4.rs index 20cd13c1875d6..3df089fc20048 100644 --- a/src/test/compile-fail/lint-dead-code-4.rs +++ b/src/test/compile-fail/lint-dead-code-4.rs @@ -14,7 +14,7 @@ struct Foo { x: usize, - b: bool, //~ ERROR: struct field is never used + b: bool, //~ ERROR: field is never used } fn field_read(f: Foo) -> usize { @@ -46,8 +46,8 @@ enum IJK { I, //~ ERROR variant is never used J { a: String, - b: i32, //~ ERROR struct field is never used - c: i32, //~ ERROR struct field is never used + b: i32, //~ ERROR field is never used + c: i32, //~ ERROR field is never used }, K //~ ERROR variant is never used @@ -68,9 +68,9 @@ fn field_match_in_patterns(b: XYZ) -> String { } struct Bar { - x: usize, //~ ERROR: struct field is never used + x: usize, //~ ERROR: field is never used b: bool, - c: bool, //~ ERROR: struct field is never used + c: bool, //~ ERROR: field is never used _guard: () } From 046c7f23680f9a8f24209be4097656f91ade0bbb Mon Sep 17 00:00:00 2001 From: Josh Triplett Date: Mon, 5 Sep 2016 10:19:00 -0700 Subject: [PATCH 2/2] Add test for unused field in union --- .../union/union-lint-dead-code.rs | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/test/compile-fail/union/union-lint-dead-code.rs diff --git a/src/test/compile-fail/union/union-lint-dead-code.rs b/src/test/compile-fail/union/union-lint-dead-code.rs new file mode 100644 index 0000000000000..7a552c8f8b76a --- /dev/null +++ b/src/test/compile-fail/union/union-lint-dead-code.rs @@ -0,0 +1,26 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(untagged_unions)] +#![deny(dead_code)] + +union Foo { + x: usize, + b: bool, //~ ERROR: field is never used + _unused: u16, +} + +fn field_read(f: Foo) -> usize { + unsafe { f.x } +} + +fn main() { + let _ = field_read(Foo { x: 2 }); +}