Skip to content
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

Improve dead code analysis for structs and traits defined locally #128637

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
321 changes: 167 additions & 154 deletions compiler/rustc_passes/src/dead.rs

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion library/core/src/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ use crate::ascii::Char as AsciiChar;
/// ```
#[cfg_attr(not(test), rustc_diagnostic_item = "Default")]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_trivial_field_reads]
pub trait Default: Sized {
/// Returns the "default value" for a type.
///
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/associated-type-bounds/union-bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

trait Tr1: Copy { type As1: Copy; }
trait Tr2: Copy { type As2: Copy; }
trait Tr3: Copy { type As3: Copy; }
trait Tr3: Copy { #[allow(dead_code)] type As3: Copy; }
trait Tr4<'a>: Copy { type As4: Copy; }
trait Tr5: Copy { type As5: Copy; }

Expand Down
1 change: 1 addition & 0 deletions tests/ui/associated-types/impl-wf-cycle-5.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ impl Fiz for bool {}

trait Grault {
type A;
#[allow(dead_code)]
type B;
}

Expand Down
1 change: 1 addition & 0 deletions tests/ui/associated-types/impl-wf-cycle-5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ impl Fiz for bool {}

trait Grault {
type A;
#[allow(dead_code)]
type B;
}

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/associated-types/impl-wf-cycle-5.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0275]: overflow evaluating the requirement `<(T,) as Grault>::A == _`
--> $DIR/impl-wf-cycle-5.rs:22:1
--> $DIR/impl-wf-cycle-5.rs:23:1
|
LL | / impl<T> Grault for (T,)
LL | |
Expand All @@ -12,7 +12,7 @@ LL | type A = ();
| ------ associated type `<(T,) as Grault>::A` is specified here
|
note: required for `(T,)` to implement `Grault`
--> $DIR/impl-wf-cycle-5.rs:22:9
--> $DIR/impl-wf-cycle-5.rs:23:9
|
LL | impl<T> Grault for (T,)
| ^^^^^^ ^^^^
Expand Down
1 change: 1 addition & 0 deletions tests/ui/associated-types/impl-wf-cycle-6.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ impl Fiz for bool {}

trait Grault {
type A;
#[allow(dead_code)]
type B;
}

Expand Down
1 change: 1 addition & 0 deletions tests/ui/associated-types/impl-wf-cycle-6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ impl Fiz for bool {}

trait Grault {
type A;
#[allow(dead_code)]
type B;
}

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/associated-types/impl-wf-cycle-6.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0275]: overflow evaluating the requirement `<(T,) as Grault>::A == _`
--> $DIR/impl-wf-cycle-6.rs:22:1
--> $DIR/impl-wf-cycle-6.rs:23:1
|
LL | / impl<T: Grault> Grault for (T,)
LL | |
Expand All @@ -11,7 +11,7 @@ LL | type A = ();
| ------ associated type `<(T,) as Grault>::A` is specified here
|
note: required for `(T,)` to implement `Grault`
--> $DIR/impl-wf-cycle-6.rs:22:17
--> $DIR/impl-wf-cycle-6.rs:23:17
|
LL | impl<T: Grault> Grault for (T,)
| ^^^^^^ ^^^^
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/const-generics/cross_crate_complex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ async fn foo() {
async_in_foo(async_out_foo::<4>().await).await;
}

struct Faz<const N: usize>;
struct Faz<const N: usize>; //~ WARN struct `Faz` is never constructed

impl<const N: usize> Foo<N> for Faz<N> {}
impl<const N: usize> Bar<N> for Faz<N> {
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/const-generics/cross_crate_complex.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
warning: struct `Faz` is never constructed
--> $DIR/cross_crate_complex.rs:14:8
|
LL | struct Faz<const N: usize>;
| ^^^
|
= note: `#[warn(dead_code)]` on by default

warning: 1 warning emitted

2 changes: 1 addition & 1 deletion tests/ui/const-generics/issues/issue-86535-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub trait Foo {
[(); Self::ASSOC_C]:;
}

struct Bar<const N: &'static ()>;
struct Bar<const N: &'static ()>; //~ WARN struct `Bar` is never constructed
impl<const N: &'static ()> Foo for Bar<N> {
const ASSOC_C: usize = 3;

Expand Down
10 changes: 10 additions & 0 deletions tests/ui/const-generics/issues/issue-86535-2.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
warning: struct `Bar` is never constructed
--> $DIR/issue-86535-2.rs:12:8
|
LL | struct Bar<const N: &'static ()>;
| ^^^
|
= note: `#[warn(dead_code)]` on by default

warning: 1 warning emitted

2 changes: 1 addition & 1 deletion tests/ui/const-generics/issues/issue-86535.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![feature(adt_const_params, unsized_const_params, generic_const_exprs)]
#![allow(incomplete_features, unused_variables)]

struct F<const S: &'static str>;
struct F<const S: &'static str>; //~ WARN struct `F` is never constructed
impl<const S: &'static str> X for F<{ S }> {
const W: usize = 3;

Expand Down
10 changes: 10 additions & 0 deletions tests/ui/const-generics/issues/issue-86535.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
warning: struct `F` is never constructed
--> $DIR/issue-86535.rs:5:8
|
LL | struct F<const S: &'static str>;
| ^
|
= note: `#[warn(dead_code)]` on by default

warning: 1 warning emitted

2 changes: 1 addition & 1 deletion tests/ui/deriving/deriving-in-macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ macro_rules! define_vec {
() => (
mod foo {
#[derive(PartialEq)]
pub struct bar;
pub struct bar; //~ WARN struct `bar` is never constructed
}
)
}
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/deriving/deriving-in-macro.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
warning: struct `bar` is never constructed
--> $DIR/deriving-in-macro.rs:9:24
|
LL | pub struct bar;
| ^^^
...
LL | define_vec![];
| ------------- in this macro invocation
|
= note: `#[warn(dead_code)]` on by default
= note: this warning originates in the macro `define_vec` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: 1 warning emitted

1 change: 1 addition & 0 deletions tests/ui/generic-associated-types/collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ trait Collection<T> {
type Iter<'iter>: Iterator<Item=&'iter T> where T: 'iter, Self: 'iter;
type Family: CollectionFamily;
// Test associated type defaults with parameters
#[allow(dead_code)]
type Sibling<U>: Collection<U> =
<<Self as Collection<T>>::Family as CollectionFamily>::Member<U>;

Expand Down
2 changes: 2 additions & 0 deletions tests/ui/generic-associated-types/missing-bounds.fixed
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//@ run-rustfix

#![allow(dead_code)]

use std::ops::Add;

struct A<B>(B);
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/generic-associated-types/missing-bounds.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//@ run-rustfix

#![allow(dead_code)]

use std::ops::Add;

struct A<B>(B);
Expand Down
18 changes: 9 additions & 9 deletions tests/ui/generic-associated-types/missing-bounds.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: equality constraints are not yet supported in `where` clauses
--> $DIR/missing-bounds.rs:37:33
--> $DIR/missing-bounds.rs:39:33
|
LL | impl<B: Add> Add for E<B> where <B as Add>::Output = B {
| ^^^^^^^^^^^^^^^^^^^^^^ not supported
Expand All @@ -11,7 +11,7 @@ LL | impl<B: Add> Add for E<B> where B: Add<Output = B> {
| ~~~~~~~~~~~~~~~~~~

error[E0308]: mismatched types
--> $DIR/missing-bounds.rs:11:11
--> $DIR/missing-bounds.rs:13:11
|
LL | impl<B> Add for A<B> where B: Add {
| - expected this type parameter
Expand All @@ -24,14 +24,14 @@ LL | A(self.0 + rhs.0)
= note: expected type parameter `B`
found associated type `<B as Add>::Output`
help: the type constructed contains `<B as Add>::Output` due to the type of the argument passed
--> $DIR/missing-bounds.rs:11:9
--> $DIR/missing-bounds.rs:13:9
|
LL | A(self.0 + rhs.0)
| ^^--------------^
| |
| this argument influences the type of `A`
note: tuple struct defined here
--> $DIR/missing-bounds.rs:5:8
--> $DIR/missing-bounds.rs:7:8
|
LL | struct A<B>(B);
| ^
Expand All @@ -41,7 +41,7 @@ LL | impl<B> Add for A<B> where B: Add<Output = B> {
| ++++++++++++

error[E0308]: mismatched types
--> $DIR/missing-bounds.rs:21:14
--> $DIR/missing-bounds.rs:23:14
|
LL | impl<B: Add> Add for C<B> {
| - expected this type parameter
Expand All @@ -54,7 +54,7 @@ LL | Self(self.0 + rhs.0)
= note: expected type parameter `B`
found associated type `<B as Add>::Output`
note: tuple struct defined here
--> $DIR/missing-bounds.rs:15:8
--> $DIR/missing-bounds.rs:17:8
|
LL | struct C<B>(B);
| ^
Expand All @@ -64,7 +64,7 @@ LL | impl<B: Add<Output = B>> Add for C<B> {
| ++++++++++++

error[E0369]: cannot add `B` to `B`
--> $DIR/missing-bounds.rs:31:21
--> $DIR/missing-bounds.rs:33:21
|
LL | Self(self.0 + rhs.0)
| ------ ^ ----- B
Expand All @@ -77,7 +77,7 @@ LL | impl<B: std::ops::Add<Output = B>> Add for D<B> {
| +++++++++++++++++++++++++++

error[E0308]: mismatched types
--> $DIR/missing-bounds.rs:42:14
--> $DIR/missing-bounds.rs:44:14
|
LL | impl<B: Add> Add for E<B> where <B as Add>::Output = B {
| - expected this type parameter
Expand All @@ -90,7 +90,7 @@ LL | Self(self.0 + rhs.0)
= note: expected type parameter `B`
found associated type `<B as Add>::Output`
note: tuple struct defined here
--> $DIR/missing-bounds.rs:35:8
--> $DIR/missing-bounds.rs:37:8
|
LL | struct E<B>(B);
| ^
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/impl-trait/extra-impl-in-trait-impl.fixed
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//@ run-rustfix

#![allow(dead_code)]

struct S<T>(T);
struct S2;

Expand Down
2 changes: 2 additions & 0 deletions tests/ui/impl-trait/extra-impl-in-trait-impl.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//@ run-rustfix

#![allow(dead_code)]

struct S<T>(T);
struct S2;

Expand Down
8 changes: 4 additions & 4 deletions tests/ui/impl-trait/extra-impl-in-trait-impl.stderr
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
error: unexpected `impl` keyword
--> $DIR/extra-impl-in-trait-impl.rs:6:18
--> $DIR/extra-impl-in-trait-impl.rs:8:18
|
LL | impl<T: Default> impl Default for S<T> {
| ^^^^^ help: remove the extra `impl`
|
note: this is parsed as an `impl Trait` type, but a trait is expected at this position
--> $DIR/extra-impl-in-trait-impl.rs:6:18
--> $DIR/extra-impl-in-trait-impl.rs:8:18
|
LL | impl<T: Default> impl Default for S<T> {
| ^^^^^^^^^^^^

error: unexpected `impl` keyword
--> $DIR/extra-impl-in-trait-impl.rs:12:6
--> $DIR/extra-impl-in-trait-impl.rs:14:6
|
LL | impl impl Default for S2 {
| ^^^^^ help: remove the extra `impl`
|
note: this is parsed as an `impl Trait` type, but a trait is expected at this position
--> $DIR/extra-impl-in-trait-impl.rs:12:6
--> $DIR/extra-impl-in-trait-impl.rs:14:6
|
LL | impl impl Default for S2 {
| ^^^^^^^^^^^^
Expand Down
19 changes: 19 additions & 0 deletions tests/ui/lint/dead-code/alias-type-used-as-generic-arg-in-impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//@ check-pass

#![deny(dead_code)]

struct T<X>(X);

type A<X> = T<X>;

trait Tr {
fn foo();
}

impl<X> Tr for T<A<X>> {
fn foo() {}
}

fn main() {
T::<T<()>>::foo();
}
2 changes: 1 addition & 1 deletion tests/ui/lint/dead-code/issue-59003.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

#![deny(dead_code)]

#[allow(dead_code)]
struct Foo {
#[allow(dead_code)]
inner: u32,
}

Expand Down
37 changes: 37 additions & 0 deletions tests/ui/lint/dead-code/lint-unused-adt-appeared-in-pattern.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#![deny(dead_code)]

struct Foo(u8); //~ ERROR struct `Foo` is never constructed

enum Bar { //~ ERROR enum `Bar` is never used
Var1(u8),
Var2(u8),
}

pub trait Tr1 {
fn f1() -> Self;
}

impl Tr1 for Foo {
fn f1() -> Foo {
let f = Foo(0);
let Foo(tag) = f;
Foo(tag)
}
}

impl Tr1 for Bar {
fn f1() -> Bar {
let b = Bar::Var1(0);
let b = if let Bar::Var1(_) = b {
Bar::Var1(0)
} else {
Bar::Var2(0)
};
match b {
Bar::Var1(_) => Bar::Var2(0),
Bar::Var2(_) => Bar::Var1(0),
}
}
}

fn main() {}
20 changes: 20 additions & 0 deletions tests/ui/lint/dead-code/lint-unused-adt-appeared-in-pattern.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
error: struct `Foo` is never constructed
--> $DIR/lint-unused-adt-appeared-in-pattern.rs:3:8
|
LL | struct Foo(u8);
| ^^^
|
note: the lint level is defined here
--> $DIR/lint-unused-adt-appeared-in-pattern.rs:1:9
|
LL | #![deny(dead_code)]
| ^^^^^^^^^

error: enum `Bar` is never used
--> $DIR/lint-unused-adt-appeared-in-pattern.rs:5:6
|
LL | enum Bar {
| ^^^

error: aborting due to 2 previous errors

Loading
Loading