1
Fork 0

Rollup merge of #126302 - mu001999-contrib:ignore/default, r=michaelwoerister

Detect unused structs which derived Default

<!--
If this PR is related to an unstable feature or an otherwise tracked effort,
please link to the relevant tracking issue here. If you don't know of a related
tracking issue or there are none, feel free to ignore this.

This PR will get automatically assigned to a reviewer. In case you would like
a specific user to review your work, you can assign it to them by using

    r​? <reviewer name>
-->

Fixes #98871
This commit is contained in:
Matthias Krüger 2024-06-25 21:33:41 +02:00 committed by GitHub
commit 58bbade921
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 78 additions and 2 deletions

View file

@ -22,6 +22,6 @@ enum MyOption<T> {
}
fn main() {
assert_eq!(Foo::default(), Foo::Alpha);
assert!(matches!(Foo::default(), Foo::Alpha));
assert!(matches!(MyOption::<NotDefault>::default(), MyOption::None));
}

View file

@ -7,6 +7,7 @@
use std::panic::catch_unwind;
#[allow(dead_code)]
#[derive(Default)]
struct Guard;

View file

@ -0,0 +1,25 @@
#![deny(dead_code)]
#[derive(Default)]
struct T; //~ ERROR struct `T` is never constructed
#[derive(Default)]
struct Used;
#[derive(Default)]
enum E {
#[default]
A,
B, //~ ERROR variant `B` is never constructed
}
// external crate can call T2::default() to construct T2,
// so that no warnings for pub adts
#[derive(Default)]
pub struct T2 {
_unread: i32,
}
fn main() {
let _x: Used = Default::default();
}

View file

@ -0,0 +1,24 @@
error: struct `T` is never constructed
--> $DIR/unused-struct-derive-default.rs:4:8
|
LL | struct T;
| ^
|
= note: `T` has a derived impl for the trait `Default`, but this is intentionally ignored during dead code analysis
note: the lint level is defined here
--> $DIR/unused-struct-derive-default.rs:1:9
|
LL | #![deny(dead_code)]
| ^^^^^^^^^
error: variant `B` is never constructed
--> $DIR/unused-struct-derive-default.rs:13:5
|
LL | enum E {
| - variant in this enum
...
LL | B,
| ^
error: aborting due to 2 previous errors