From 8cc4fd6c23ab0d59a6c11967ca6b949e4c9f0ab4 Mon Sep 17 00:00:00 2001 From: "Zack M. Davis" Date: Thu, 12 Jul 2018 19:37:49 -0700 Subject: [PATCH] dead-code lint: say "constructed", "called" for structs, functions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Respectively. This is a sequel to November 2017's #46103 / 1a9dc2e9. It had been reported (more than onceā€”at least #19140, #44083, and #44565) that the "never used" language was confusing for enum variants that were "used" as match patterns, so the wording was changed to say never "constructed" specifically for enum variants. More recently, the same issue was raised for structs (#52325). It seems consistent to say "constructed" here, too, for the same reasons. While we're here, we can also use more specific word "called" for unused functions and methods. (We declined to do this in #46103, but the rationale given in the commit message doesn't actually make sense.) This resolves #52325. --- src/librustc/middle/dead.rs | 10 ++++++++-- src/test/compile-fail/fail-no-dead-code-core.rs | 2 +- src/test/compile-fail/fail-no-dead-code.rs | 2 +- src/test/compile-fail/lint-dead-code-1.rs | 10 +++++----- src/test/compile-fail/lint-dead-code-2.rs | 6 +++--- src/test/compile-fail/lint-dead-code-3.rs | 6 +++--- src/test/compile-fail/test-warns-dead-code.rs | 2 +- .../ui-fulldeps/lint-plugin-cmdline-allow.stderr | 2 +- src/test/ui/path-lookahead.rs | 4 ++-- src/test/ui/path-lookahead.stderr | 8 ++++---- src/test/ui/span/macro-span-replacement.rs | 2 +- src/test/ui/span/macro-span-replacement.stderr | 4 ++-- .../ui/span/unused-warning-point-at-signature.rs | 6 +++--- .../ui/span/unused-warning-point-at-signature.stderr | 12 ++++++------ 14 files changed, 41 insertions(+), 35 deletions(-) diff --git a/src/librustc/middle/dead.rs b/src/librustc/middle/dead.rs index 42775e3a1837f..368b955a42aaa 100644 --- a/src/librustc/middle/dead.rs +++ b/src/librustc/middle/dead.rs @@ -567,12 +567,17 @@ impl<'a, 'tcx> Visitor<'tcx> for DeadVisitor<'a, 'tcx> { hir::ItemImpl(..) => self.tcx.sess.codemap().def_span(item.span), _ => item.span, }; + let participle = match item.node { + hir::ItemFn(..) => "called", + hir::ItemStruct(..) => "constructed", + _ => "used" + }; self.warn_dead_code( item.id, span, item.name, item.node.descriptive_variant(), - "used", + participle, ); } else { // Only continue if we didn't warn @@ -622,7 +627,8 @@ impl<'a, 'tcx> Visitor<'tcx> for DeadVisitor<'a, 'tcx> { hir::ImplItemKind::Method(_, body_id) => { if !self.symbol_is_live(impl_item.id, None) { let span = self.tcx.sess.codemap().def_span(impl_item.span); - self.warn_dead_code(impl_item.id, span, impl_item.ident.name, "method", "used"); + self.warn_dead_code(impl_item.id, span, impl_item.ident.name, + "method", "called"); } self.visit_nested_body(body_id) } diff --git a/src/test/compile-fail/fail-no-dead-code-core.rs b/src/test/compile-fail/fail-no-dead-code-core.rs index aed76e36fca14..da00d6e981aa9 100644 --- a/src/test/compile-fail/fail-no-dead-code-core.rs +++ b/src/test/compile-fail/fail-no-dead-code-core.rs @@ -14,7 +14,7 @@ #[macro_use] extern crate core; -fn foo() { //~ ERROR function is never used +fn foo() { //~ ERROR function is never called // none of these should have any dead_code exposed to the user panic!(); diff --git a/src/test/compile-fail/fail-no-dead-code.rs b/src/test/compile-fail/fail-no-dead-code.rs index 6e5d3a313556e..f7161558157db 100644 --- a/src/test/compile-fail/fail-no-dead-code.rs +++ b/src/test/compile-fail/fail-no-dead-code.rs @@ -11,7 +11,7 @@ #![deny(dead_code)] #![allow(unreachable_code)] -fn foo() { //~ ERROR function is never used +fn foo() { //~ ERROR function is never called // none of these should have any dead_code exposed to the user panic!(); diff --git a/src/test/compile-fail/lint-dead-code-1.rs b/src/test/compile-fail/lint-dead-code-1.rs index d6ca5e6b1d969..b771e9726a99c 100644 --- a/src/test/compile-fail/lint-dead-code-1.rs +++ b/src/test/compile-fail/lint-dead-code-1.rs @@ -19,7 +19,7 @@ pub use foo2::Bar2; mod foo { - pub struct Bar; //~ ERROR: struct is never used + pub struct Bar; //~ ERROR: struct is never constructed } mod foo2 { @@ -42,7 +42,7 @@ const CONST_USED_IN_ENUM_DISCRIMINANT: isize = 11; pub type typ = *const UsedStruct4; pub struct PubStruct; -struct PrivStruct; //~ ERROR: struct is never used +struct PrivStruct; //~ ERROR: struct is never constructed struct UsedStruct1 { #[allow(dead_code)] x: isize @@ -95,17 +95,17 @@ pub fn pub_fn() { } f::(); } -fn priv_fn() { //~ ERROR: function is never used +fn priv_fn() { //~ ERROR: function is never called let unused_struct = PrivStruct; } fn used_fn() {} -fn foo() { //~ ERROR: function is never used +fn foo() { //~ ERROR: function is never called bar(); let unused_enum = priv_enum::foo2; } -fn bar() { //~ ERROR: function is never used +fn bar() { //~ ERROR: function is never called foo(); } diff --git a/src/test/compile-fail/lint-dead-code-2.rs b/src/test/compile-fail/lint-dead-code-2.rs index 4a0e4f4319e0a..e4c2761fcb7e2 100644 --- a/src/test/compile-fail/lint-dead-code-2.rs +++ b/src/test/compile-fail/lint-dead-code-2.rs @@ -29,10 +29,10 @@ impl Bar for Foo { fn live_fn() {} -fn dead_fn() {} //~ ERROR: function is never used +fn dead_fn() {} //~ ERROR: function is never called #[main] -fn dead_fn2() {} //~ ERROR: function is never used +fn dead_fn2() {} //~ ERROR: function is never called fn used_fn() {} @@ -45,7 +45,7 @@ fn start(_: isize, _: *const *const u8) -> isize { } // this is not main -fn main() { //~ ERROR: function is never used +fn main() { //~ ERROR: function is never called dead_fn(); dead_fn2(); } diff --git a/src/test/compile-fail/lint-dead-code-3.rs b/src/test/compile-fail/lint-dead-code-3.rs index f680e39544933..c4f16e113b934 100644 --- a/src/test/compile-fail/lint-dead-code-3.rs +++ b/src/test/compile-fail/lint-dead-code-3.rs @@ -20,14 +20,14 @@ extern { pub fn extern_foo(); } -struct Foo; //~ ERROR: struct is never used +struct Foo; //~ ERROR: struct is never constructed impl Foo { - fn foo(&self) { //~ ERROR: method is never used + fn foo(&self) { //~ ERROR: method is never called bar() } } -fn bar() { //~ ERROR: function is never used +fn bar() { //~ ERROR: function is never called fn baz() {} Foo.foo(); diff --git a/src/test/compile-fail/test-warns-dead-code.rs b/src/test/compile-fail/test-warns-dead-code.rs index 0e25f1e965ab9..8e50154685364 100644 --- a/src/test/compile-fail/test-warns-dead-code.rs +++ b/src/test/compile-fail/test-warns-dead-code.rs @@ -12,6 +12,6 @@ #![deny(dead_code)] -fn dead() {} //~ error: function is never used: `dead` +fn dead() {} //~ error: function is never called: `dead` fn main() {} diff --git a/src/test/ui-fulldeps/lint-plugin-cmdline-allow.stderr b/src/test/ui-fulldeps/lint-plugin-cmdline-allow.stderr index cda6ea8bb903b..6cba74367c550 100644 --- a/src/test/ui-fulldeps/lint-plugin-cmdline-allow.stderr +++ b/src/test/ui-fulldeps/lint-plugin-cmdline-allow.stderr @@ -1,4 +1,4 @@ -warning: function is never used: `lintme` +warning: function is never called: `lintme` --> $DIR/lint-plugin-cmdline-allow.rs:20:1 | LL | fn lintme() { } diff --git a/src/test/ui/path-lookahead.rs b/src/test/ui/path-lookahead.rs index 367a256015558..ecc08feceb744 100644 --- a/src/test/ui/path-lookahead.rs +++ b/src/test/ui/path-lookahead.rs @@ -14,11 +14,11 @@ // Parser test for #37765 -fn with_parens(arg: T) -> String { //~WARN function is never used: `with_parens` +fn with_parens(arg: T) -> String { //~WARN function is never called: `with_parens` return (::to_string(&arg)); //~WARN unnecessary parentheses around `return` value } -fn no_parens(arg: T) -> String { //~WARN function is never used: `no_parens` +fn no_parens(arg: T) -> String { //~WARN function is never called: `no_parens` return ::to_string(&arg); } diff --git a/src/test/ui/path-lookahead.stderr b/src/test/ui/path-lookahead.stderr index 89df5e894aa34..5de59c097923a 100644 --- a/src/test/ui/path-lookahead.stderr +++ b/src/test/ui/path-lookahead.stderr @@ -11,10 +11,10 @@ LL | #![warn(unused)] | ^^^^^^ = note: #[warn(unused_parens)] implied by #[warn(unused)] -warning: function is never used: `with_parens` +warning: function is never called: `with_parens` --> $DIR/path-lookahead.rs:17:1 | -LL | fn with_parens(arg: T) -> String { //~WARN function is never used: `with_parens` +LL | fn with_parens(arg: T) -> String { //~WARN function is never called: `with_parens` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | note: lint level defined here @@ -24,9 +24,9 @@ LL | #![warn(unused)] | ^^^^^^ = note: #[warn(dead_code)] implied by #[warn(unused)] -warning: function is never used: `no_parens` +warning: function is never called: `no_parens` --> $DIR/path-lookahead.rs:21:1 | -LL | fn no_parens(arg: T) -> String { //~WARN function is never used: `no_parens` +LL | fn no_parens(arg: T) -> String { //~WARN function is never called: `no_parens` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/src/test/ui/span/macro-span-replacement.rs b/src/test/ui/span/macro-span-replacement.rs index 4fe35042300e7..7374f15746851 100644 --- a/src/test/ui/span/macro-span-replacement.rs +++ b/src/test/ui/span/macro-span-replacement.rs @@ -14,7 +14,7 @@ macro_rules! m { ($a:tt $b:tt) => { - $b $a; //~ WARN struct is never used + $b $a; //~ WARN struct is never constructed } } diff --git a/src/test/ui/span/macro-span-replacement.stderr b/src/test/ui/span/macro-span-replacement.stderr index bb59fa206df1d..b12ca86d3c929 100644 --- a/src/test/ui/span/macro-span-replacement.stderr +++ b/src/test/ui/span/macro-span-replacement.stderr @@ -1,7 +1,7 @@ -warning: struct is never used: `S` +warning: struct is never constructed: `S` --> $DIR/macro-span-replacement.rs:17:14 | -LL | $b $a; //~ WARN struct is never used +LL | $b $a; //~ WARN struct is never constructed | ^ ... LL | m!(S struct); diff --git a/src/test/ui/span/unused-warning-point-at-signature.rs b/src/test/ui/span/unused-warning-point-at-signature.rs index 6a7071d49aed6..f399d61f41867 100644 --- a/src/test/ui/span/unused-warning-point-at-signature.rs +++ b/src/test/ui/span/unused-warning-point-at-signature.rs @@ -19,18 +19,18 @@ enum Enum { //~ WARN enum is never used D, } -struct Struct { //~ WARN struct is never used +struct Struct { //~ WARN struct is never constructed a: usize, b: usize, c: usize, d: usize, } -fn func() -> usize { //~ WARN function is never used +fn func() -> usize { //~ WARN function is never called 3 } -fn //~ WARN function is never used +fn //~ WARN function is never called func_complete_span() -> usize { diff --git a/src/test/ui/span/unused-warning-point-at-signature.stderr b/src/test/ui/span/unused-warning-point-at-signature.stderr index de234344d8409..dd05960b8208f 100644 --- a/src/test/ui/span/unused-warning-point-at-signature.stderr +++ b/src/test/ui/span/unused-warning-point-at-signature.stderr @@ -11,22 +11,22 @@ LL | #![warn(unused)] | ^^^^^^ = note: #[warn(dead_code)] implied by #[warn(unused)] -warning: struct is never used: `Struct` +warning: struct is never constructed: `Struct` --> $DIR/unused-warning-point-at-signature.rs:22:1 | -LL | struct Struct { //~ WARN struct is never used +LL | struct Struct { //~ WARN struct is never constructed | ^^^^^^^^^^^^^ -warning: function is never used: `func` +warning: function is never called: `func` --> $DIR/unused-warning-point-at-signature.rs:29:1 | -LL | fn func() -> usize { //~ WARN function is never used +LL | fn func() -> usize { //~ WARN function is never called | ^^^^^^^^^^^^^^^^^^ -warning: function is never used: `func_complete_span` +warning: function is never called: `func_complete_span` --> $DIR/unused-warning-point-at-signature.rs:33:1 | -LL | / fn //~ WARN function is never used +LL | / fn //~ WARN function is never called LL | | func_complete_span() LL | | -> usize LL | | {