add rustfix annotation

This commit is contained in:
Icxolu 2023-05-03 21:05:50 +02:00
parent 4ed7fd1ecc
commit 160371550f
3 changed files with 126 additions and 5 deletions

View File

@ -0,0 +1,119 @@
//@run-rustfix
#![allow(unused)]
#![warn(clippy::default_constructed_unit_structs)]
use std::marker::PhantomData;
#[derive(Default)]
struct UnitStruct;
impl UnitStruct {
fn new() -> Self {
//should lint
Self
}
}
#[derive(Default)]
struct TupleStruct(usize);
impl TupleStruct {
fn new() -> Self {
// should not lint
Self(Default::default())
}
}
// 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,
}
}
fn new2() -> Self {
// should not lint
Self {
inner: Default::default(),
}
}
}
#[derive(Default)]
struct GenericStruct<T> {
t: T,
}
impl<T: Default> GenericStruct<T> {
fn new() -> Self {
// should not lint
Self { t: T::default() }
}
fn new2() -> Self {
// should not lint
Self { t: Default::default() }
}
}
struct FakeDefault;
impl FakeDefault {
fn default() -> Self {
Self
}
}
impl Default for FakeDefault {
fn default() -> Self {
Self
}
}
#[derive(Default)]
struct EmptyStruct {}
#[derive(Default)]
#[non_exhaustive]
struct NonExhaustiveStruct;
fn main() {
// should lint
let _ = PhantomData::<usize>;
let _: PhantomData<i32> = PhantomData;
let _ = UnitStruct;
// should not lint
let _ = TupleStruct::default();
let _ = NormalStruct::default();
let _ = NonExhaustiveStruct::default();
let _ = SomeEnum::default();
let _ = NonDefaultStruct::default();
let _ = EmptyStruct::default();
let _ = FakeDefault::default();
let _ = <FakeDefault as Default>::default();
}

View File

@ -1,3 +1,5 @@
//@run-rustfix
#![allow(unused)]
#![warn(clippy::default_constructed_unit_structs)]
use std::marker::PhantomData;

View File

@ -1,5 +1,5 @@
error: use of `default` to create a unit struct
--> $DIR/default_constructed_unit_structs.rs:11:13
--> $DIR/default_constructed_unit_structs.rs:13:13
|
LL | Self::default()
| ^^^^^^^^^^^ help: remove this call to `default`
@ -7,25 +7,25 @@ LL | Self::default()
= note: `-D clippy::default-constructed-unit-structs` implied by `-D warnings`
error: use of `default` to create a unit struct
--> $DIR/default_constructed_unit_structs.rs:53:31
--> $DIR/default_constructed_unit_structs.rs:55:31
|
LL | inner: PhantomData::default(),
| ^^^^^^^^^^^ help: remove this call to `default`
error: use of `default` to create a unit struct
--> $DIR/default_constructed_unit_structs.rs:104:33
--> $DIR/default_constructed_unit_structs.rs:106:33
|
LL | let _ = PhantomData::<usize>::default();
| ^^^^^^^^^^^ help: remove this call to `default`
error: use of `default` to create a unit struct
--> $DIR/default_constructed_unit_structs.rs:105:42
--> $DIR/default_constructed_unit_structs.rs:107:42
|
LL | let _: PhantomData<i32> = PhantomData::default();
| ^^^^^^^^^^^ help: remove this call to `default`
error: use of `default` to create a unit struct
--> $DIR/default_constructed_unit_structs.rs:106:23
--> $DIR/default_constructed_unit_structs.rs:108:23
|
LL | let _ = UnitStruct::default();
| ^^^^^^^^^^^ help: remove this call to `default`