Skip to content

Commit 3c46da8

Browse files
Fix dead code lint for functions using impl Trait
1 parent 088fc73 commit 3c46da8

9 files changed

+102
-2
lines changed

src/librustc/middle/dead.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,13 @@ fn create_and_seed_worklist<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
395395
krate: &hir::Crate)
396396
-> Vec<ast::NodeId>
397397
{
398-
let worklist = access_levels.map.iter().map(|(&id, _)| id).chain(
398+
let worklist = access_levels.map.iter().filter_map(|(&id, level)| {
399+
if level >= &privacy::AccessLevel::Reachable {
400+
Some(id)
401+
} else {
402+
None
403+
}
404+
}).chain(
399405
// Seed entry point
400406
tcx.sess.entry_fn.borrow().map(|(id, _, _)| id)
401407
).collect::<Vec<_>>();

src/test/run-pass/async-await.stderr

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
warning: struct is never constructed: `Foo`
2+
--> $DIR/async-await.rs:122:1
3+
|
4+
LL | struct Foo;
5+
| ^^^^^^^^^^^
6+
|
7+
= note: #[warn(dead_code)] on by default
8+
9+
warning: method is never used: `async_method`
10+
--> $DIR/async-await.rs:129:5
11+
|
12+
LL | async fn async_method(x: u8) -> u8 {
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
warning: function is never used: `foo`
2+
--> $DIR/existential-minimal.rs:15:1
3+
|
4+
LL | fn foo() -> impl std::fmt::Debug { "cake" }
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: #[warn(dead_code)] on by default
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
warning: struct is never constructed: `Foo`
2+
--> $DIR/issue-42479.rs:15:1
3+
|
4+
LL | struct Foo {
5+
| ^^^^^^^^^^
6+
|
7+
= note: #[warn(dead_code)] on by default
8+
9+
warning: method is never used: `inside`
10+
--> $DIR/issue-42479.rs:20:5
11+
|
12+
LL | fn inside(&self) -> impl Iterator<Item = &i32> {
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
warning: function is never used: `gen`
2+
--> $DIR/issue-49376.rs:18:1
3+
|
4+
LL | fn gen() -> impl PartialOrd + PartialEq + Debug { }
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: #[warn(dead_code)] on by default
8+
9+
warning: struct is never constructed: `Bar`
10+
--> $DIR/issue-49376.rs:20:1
11+
|
12+
LL | struct Bar {}
13+
| ^^^^^^^^^^
14+
15+
warning: function is never used: `foo`
16+
--> $DIR/issue-49376.rs:24:1
17+
|
18+
LL | fn foo() -> impl Foo {
19+
| ^^^^^^^^^^^^^^^^^^^^
20+
21+
warning: function is never used: `test_impl_ops`
22+
--> $DIR/issue-49376.rs:28:1
23+
|
24+
LL | fn test_impl_ops() -> impl Add + Sub + Mul + Div { 1 }
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
26+
27+
warning: function is never used: `test_impl_assign_ops`
28+
--> $DIR/issue-49376.rs:29:1
29+
|
30+
LL | fn test_impl_assign_ops() -> impl AddAssign + SubAssign + MulAssign + DivAssign { 1 }
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
32+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
warning: function is never used: `iter`
2+
--> $DIR/issue-49556.rs:12:1
3+
|
4+
LL | fn iter<'a>(data: &'a [usize]) -> impl Iterator<Item = usize> + 'a {
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: #[warn(dead_code)] on by default
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
warning: function is never used: `batches`
2+
--> $DIR/conservative_impl_trait.rs:14:1
3+
|
4+
LL | fn batches(n: &u32) -> impl Iterator<Item=&u32> {
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: #[warn(dead_code)] on by default
8+

src/test/ui/lint/lint-dead-code-1.rs

+4
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ fn bar() { //~ ERROR: function is never used
109109
foo();
110110
}
111111

112+
fn baz() -> impl Copy { //~ ERROR: function is never used
113+
"I'm unused, too"
114+
}
115+
112116
// Code with #[allow(dead_code)] should be marked live (and thus anything it
113117
// calls is marked live)
114118
#[allow(dead_code)]

src/test/ui/lint/lint-dead-code-1.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,11 @@ error: function is never used: `bar`
5858
LL | fn bar() { //~ ERROR: function is never used
5959
| ^^^^^^^^
6060

61-
error: aborting due to 9 previous errors
61+
error: function is never used: `baz`
62+
--> $DIR/lint-dead-code-1.rs:112:1
63+
|
64+
LL | fn baz() -> impl Copy { //~ ERROR: function is never used
65+
| ^^^^^^^^^^^^^^^^^^^^^
66+
67+
error: aborting due to 10 previous errors
6268

0 commit comments

Comments
 (0)