2023-05-03 14:05:50 -05:00
|
|
|
//@run-rustfix
|
|
|
|
|
2023-04-25 13:44:09 -05:00
|
|
|
#![allow(unused)]
|
2023-05-03 12:25:25 -05:00
|
|
|
#![warn(clippy::default_constructed_unit_structs)]
|
2023-04-25 13:44:09 -05:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
struct UnitStruct;
|
|
|
|
|
2023-05-03 13:06:29 -05:00
|
|
|
impl UnitStruct {
|
|
|
|
fn new() -> Self {
|
|
|
|
//should lint
|
|
|
|
Self::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-25 13:44:09 -05:00
|
|
|
#[derive(Default)]
|
|
|
|
struct TupleStruct(usize);
|
|
|
|
|
2023-05-03 13:06:29 -05:00
|
|
|
impl TupleStruct {
|
|
|
|
fn new() -> Self {
|
|
|
|
// should not lint
|
|
|
|
Self(Default::default())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-25 13:44:09 -05:00
|
|
|
// no lint for derived impl
|
|
|
|
#[derive(Default)]
|
|
|
|
struct NormalStruct {
|
|
|
|
inner: PhantomData<usize>,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct NonDefaultStruct;
|
|
|
|
|
|
|
|
impl NonDefaultStruct {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
enum SomeEnum {
|
|
|
|
#[default]
|
|
|
|
Unit,
|
|
|
|
Tuple(UnitStruct),
|
|
|
|
Struct {
|
|
|
|
inner: usize,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
impl NormalStruct {
|
|
|
|
fn new() -> Self {
|
|
|
|
// should lint
|
|
|
|
Self {
|
|
|
|
inner: PhantomData::default(),
|
|
|
|
}
|
|
|
|
}
|
2023-05-03 13:06:29 -05:00
|
|
|
|
|
|
|
fn new2() -> Self {
|
|
|
|
// should not lint
|
|
|
|
Self {
|
|
|
|
inner: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
2023-04-25 13:44:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
struct GenericStruct<T> {
|
|
|
|
t: T,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Default> GenericStruct<T> {
|
|
|
|
fn new() -> Self {
|
|
|
|
// should not lint
|
|
|
|
Self { t: T::default() }
|
|
|
|
}
|
2023-05-03 13:06:29 -05:00
|
|
|
|
|
|
|
fn new2() -> Self {
|
|
|
|
// should not lint
|
|
|
|
Self { t: Default::default() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct FakeDefault;
|
|
|
|
impl FakeDefault {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self
|
|
|
|
}
|
2023-04-25 13:44:09 -05:00
|
|
|
}
|
|
|
|
|
2023-05-03 13:06:29 -05:00
|
|
|
impl Default for FakeDefault {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
struct EmptyStruct {}
|
|
|
|
|
2023-04-25 13:44:09 -05:00
|
|
|
#[derive(Default)]
|
|
|
|
#[non_exhaustive]
|
|
|
|
struct NonExhaustiveStruct;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
// should lint
|
|
|
|
let _ = PhantomData::<usize>::default();
|
|
|
|
let _: PhantomData<i32> = PhantomData::default();
|
|
|
|
let _ = UnitStruct::default();
|
|
|
|
|
|
|
|
// should not lint
|
|
|
|
let _ = TupleStruct::default();
|
|
|
|
let _ = NormalStruct::default();
|
|
|
|
let _ = NonExhaustiveStruct::default();
|
|
|
|
let _ = SomeEnum::default();
|
|
|
|
let _ = NonDefaultStruct::default();
|
2023-05-03 13:06:29 -05:00
|
|
|
let _ = EmptyStruct::default();
|
|
|
|
let _ = FakeDefault::default();
|
|
|
|
let _ = <FakeDefault as Default>::default();
|
2023-04-25 13:44:09 -05:00
|
|
|
}
|