Skip to content

Commit 8cc4fd6

Browse files
committed
dead-code lint: say "constructed", "called" for structs, functions
Respectively. This is a sequel to November 2017's rust-lang#46103 / 1a9dc2e. It had been reported (more than once—at least rust-lang#19140, rust-lang#44083, and rust-lang#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 (rust-lang#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 rust-lang#46103, but the rationale given in the commit message doesn't actually make sense.) This resolves rust-lang#52325.
1 parent 299ee47 commit 8cc4fd6

14 files changed

+41
-35
lines changed

src/librustc/middle/dead.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -567,12 +567,17 @@ impl<'a, 'tcx> Visitor<'tcx> for DeadVisitor<'a, 'tcx> {
567567
hir::ItemImpl(..) => self.tcx.sess.codemap().def_span(item.span),
568568
_ => item.span,
569569
};
570+
let participle = match item.node {
571+
hir::ItemFn(..) => "called",
572+
hir::ItemStruct(..) => "constructed",
573+
_ => "used"
574+
};
570575
self.warn_dead_code(
571576
item.id,
572577
span,
573578
item.name,
574579
item.node.descriptive_variant(),
575-
"used",
580+
participle,
576581
);
577582
} else {
578583
// Only continue if we didn't warn
@@ -622,7 +627,8 @@ impl<'a, 'tcx> Visitor<'tcx> for DeadVisitor<'a, 'tcx> {
622627
hir::ImplItemKind::Method(_, body_id) => {
623628
if !self.symbol_is_live(impl_item.id, None) {
624629
let span = self.tcx.sess.codemap().def_span(impl_item.span);
625-
self.warn_dead_code(impl_item.id, span, impl_item.ident.name, "method", "used");
630+
self.warn_dead_code(impl_item.id, span, impl_item.ident.name,
631+
"method", "called");
626632
}
627633
self.visit_nested_body(body_id)
628634
}

src/test/compile-fail/fail-no-dead-code-core.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#[macro_use]
1515
extern crate core;
1616

17-
fn foo() { //~ ERROR function is never used
17+
fn foo() { //~ ERROR function is never called
1818

1919
// none of these should have any dead_code exposed to the user
2020
panic!();

src/test/compile-fail/fail-no-dead-code.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#![deny(dead_code)]
1212
#![allow(unreachable_code)]
1313

14-
fn foo() { //~ ERROR function is never used
14+
fn foo() { //~ ERROR function is never called
1515

1616
// none of these should have any dead_code exposed to the user
1717
panic!();

src/test/compile-fail/lint-dead-code-1.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
pub use foo2::Bar2;
2020

2121
mod foo {
22-
pub struct Bar; //~ ERROR: struct is never used
22+
pub struct Bar; //~ ERROR: struct is never constructed
2323
}
2424

2525
mod foo2 {
@@ -42,7 +42,7 @@ const CONST_USED_IN_ENUM_DISCRIMINANT: isize = 11;
4242

4343
pub type typ = *const UsedStruct4;
4444
pub struct PubStruct;
45-
struct PrivStruct; //~ ERROR: struct is never used
45+
struct PrivStruct; //~ ERROR: struct is never constructed
4646
struct UsedStruct1 {
4747
#[allow(dead_code)]
4848
x: isize
@@ -95,17 +95,17 @@ pub fn pub_fn() {
9595
}
9696
f::<StructUsedInGeneric>();
9797
}
98-
fn priv_fn() { //~ ERROR: function is never used
98+
fn priv_fn() { //~ ERROR: function is never called
9999
let unused_struct = PrivStruct;
100100
}
101101
fn used_fn() {}
102102

103-
fn foo() { //~ ERROR: function is never used
103+
fn foo() { //~ ERROR: function is never called
104104
bar();
105105
let unused_enum = priv_enum::foo2;
106106
}
107107

108-
fn bar() { //~ ERROR: function is never used
108+
fn bar() { //~ ERROR: function is never called
109109
foo();
110110
}
111111

src/test/compile-fail/lint-dead-code-2.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ impl Bar for Foo {
2929

3030
fn live_fn() {}
3131

32-
fn dead_fn() {} //~ ERROR: function is never used
32+
fn dead_fn() {} //~ ERROR: function is never called
3333

3434
#[main]
35-
fn dead_fn2() {} //~ ERROR: function is never used
35+
fn dead_fn2() {} //~ ERROR: function is never called
3636

3737
fn used_fn() {}
3838

@@ -45,7 +45,7 @@ fn start(_: isize, _: *const *const u8) -> isize {
4545
}
4646

4747
// this is not main
48-
fn main() { //~ ERROR: function is never used
48+
fn main() { //~ ERROR: function is never called
4949
dead_fn();
5050
dead_fn2();
5151
}

src/test/compile-fail/lint-dead-code-3.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ extern {
2020
pub fn extern_foo();
2121
}
2222

23-
struct Foo; //~ ERROR: struct is never used
23+
struct Foo; //~ ERROR: struct is never constructed
2424
impl Foo {
25-
fn foo(&self) { //~ ERROR: method is never used
25+
fn foo(&self) { //~ ERROR: method is never called
2626
bar()
2727
}
2828
}
2929

30-
fn bar() { //~ ERROR: function is never used
30+
fn bar() { //~ ERROR: function is never called
3131
fn baz() {}
3232

3333
Foo.foo();

src/test/compile-fail/test-warns-dead-code.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212

1313
#![deny(dead_code)]
1414

15-
fn dead() {} //~ error: function is never used: `dead`
15+
fn dead() {} //~ error: function is never called: `dead`
1616

1717
fn main() {}

src/test/ui-fulldeps/lint-plugin-cmdline-allow.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
warning: function is never used: `lintme`
1+
warning: function is never called: `lintme`
22
--> $DIR/lint-plugin-cmdline-allow.rs:20:1
33
|
44
LL | fn lintme() { }

src/test/ui/path-lookahead.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414

1515
// Parser test for #37765
1616

17-
fn with_parens<T: ToString>(arg: T) -> String { //~WARN function is never used: `with_parens`
17+
fn with_parens<T: ToString>(arg: T) -> String { //~WARN function is never called: `with_parens`
1818
return (<T as ToString>::to_string(&arg)); //~WARN unnecessary parentheses around `return` value
1919
}
2020

21-
fn no_parens<T: ToString>(arg: T) -> String { //~WARN function is never used: `no_parens`
21+
fn no_parens<T: ToString>(arg: T) -> String { //~WARN function is never called: `no_parens`
2222
return <T as ToString>::to_string(&arg);
2323
}
2424

src/test/ui/path-lookahead.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ LL | #![warn(unused)]
1111
| ^^^^^^
1212
= note: #[warn(unused_parens)] implied by #[warn(unused)]
1313

14-
warning: function is never used: `with_parens`
14+
warning: function is never called: `with_parens`
1515
--> $DIR/path-lookahead.rs:17:1
1616
|
17-
LL | fn with_parens<T: ToString>(arg: T) -> String { //~WARN function is never used: `with_parens`
17+
LL | fn with_parens<T: ToString>(arg: T) -> String { //~WARN function is never called: `with_parens`
1818
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1919
|
2020
note: lint level defined here
@@ -24,9 +24,9 @@ LL | #![warn(unused)]
2424
| ^^^^^^
2525
= note: #[warn(dead_code)] implied by #[warn(unused)]
2626

27-
warning: function is never used: `no_parens`
27+
warning: function is never called: `no_parens`
2828
--> $DIR/path-lookahead.rs:21:1
2929
|
30-
LL | fn no_parens<T: ToString>(arg: T) -> String { //~WARN function is never used: `no_parens`
30+
LL | fn no_parens<T: ToString>(arg: T) -> String { //~WARN function is never called: `no_parens`
3131
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3232

src/test/ui/span/macro-span-replacement.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
macro_rules! m {
1616
($a:tt $b:tt) => {
17-
$b $a; //~ WARN struct is never used
17+
$b $a; //~ WARN struct is never constructed
1818
}
1919
}
2020

src/test/ui/span/macro-span-replacement.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
warning: struct is never used: `S`
1+
warning: struct is never constructed: `S`
22
--> $DIR/macro-span-replacement.rs:17:14
33
|
4-
LL | $b $a; //~ WARN struct is never used
4+
LL | $b $a; //~ WARN struct is never constructed
55
| ^
66
...
77
LL | m!(S struct);

src/test/ui/span/unused-warning-point-at-signature.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@ enum Enum { //~ WARN enum is never used
1919
D,
2020
}
2121

22-
struct Struct { //~ WARN struct is never used
22+
struct Struct { //~ WARN struct is never constructed
2323
a: usize,
2424
b: usize,
2525
c: usize,
2626
d: usize,
2727
}
2828

29-
fn func() -> usize { //~ WARN function is never used
29+
fn func() -> usize { //~ WARN function is never called
3030
3
3131
}
3232

33-
fn //~ WARN function is never used
33+
fn //~ WARN function is never called
3434
func_complete_span()
3535
-> usize
3636
{

src/test/ui/span/unused-warning-point-at-signature.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,22 @@ LL | #![warn(unused)]
1111
| ^^^^^^
1212
= note: #[warn(dead_code)] implied by #[warn(unused)]
1313

14-
warning: struct is never used: `Struct`
14+
warning: struct is never constructed: `Struct`
1515
--> $DIR/unused-warning-point-at-signature.rs:22:1
1616
|
17-
LL | struct Struct { //~ WARN struct is never used
17+
LL | struct Struct { //~ WARN struct is never constructed
1818
| ^^^^^^^^^^^^^
1919

20-
warning: function is never used: `func`
20+
warning: function is never called: `func`
2121
--> $DIR/unused-warning-point-at-signature.rs:29:1
2222
|
23-
LL | fn func() -> usize { //~ WARN function is never used
23+
LL | fn func() -> usize { //~ WARN function is never called
2424
| ^^^^^^^^^^^^^^^^^^
2525

26-
warning: function is never used: `func_complete_span`
26+
warning: function is never called: `func_complete_span`
2727
--> $DIR/unused-warning-point-at-signature.rs:33:1
2828
|
29-
LL | / fn //~ WARN function is never used
29+
LL | / fn //~ WARN function is never called
3030
LL | | func_complete_span()
3131
LL | | -> usize
3232
LL | | {

0 commit comments

Comments
 (0)