adds lint to detect construction of unit struct using default
Using `default` to construct a unit struct increases code complexity and adds a function call. This can be avoided by simply removing the call to `default` and simply construct by name.
This commit is contained in:
parent
990bbdc2be
commit
9428138562
6 changed files with 170 additions and 0 deletions
72
tests/ui/default_constructed_unit_struct.rs
Normal file
72
tests/ui/default_constructed_unit_struct.rs
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
#![allow(unused)]
|
||||
#![warn(clippy::default_constructed_unit_struct)]
|
||||
use std::marker::PhantomData;
|
||||
|
||||
#[derive(Default)]
|
||||
struct UnitStruct;
|
||||
|
||||
#[derive(Default)]
|
||||
struct TupleStruct(usize);
|
||||
|
||||
// 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(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct GenericStruct<T> {
|
||||
t: T,
|
||||
}
|
||||
|
||||
impl<T: Default> GenericStruct<T> {
|
||||
fn new() -> Self {
|
||||
// should not lint
|
||||
Self { t: T::default() }
|
||||
}
|
||||
}
|
||||
|
||||
#[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();
|
||||
}
|
||||
28
tests/ui/default_constructed_unit_struct.stderr
Normal file
28
tests/ui/default_constructed_unit_struct.stderr
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
error: Use of `default` to create a unit struct.
|
||||
--> $DIR/default_constructed_unit_struct.rs:39:33
|
||||
|
|
||||
LL | inner: PhantomData::default(),
|
||||
| ^^^^^^^ help: remove this call to `default`
|
||||
|
|
||||
= note: `-D clippy::default-constructed-unit-struct` implied by `-D warnings`
|
||||
|
||||
error: Use of `default` to create a unit struct.
|
||||
--> $DIR/default_constructed_unit_struct.rs:62:35
|
||||
|
|
||||
LL | let _ = PhantomData::<usize>::default();
|
||||
| ^^^^^^^ help: remove this call to `default`
|
||||
|
||||
error: Use of `default` to create a unit struct.
|
||||
--> $DIR/default_constructed_unit_struct.rs:63:44
|
||||
|
|
||||
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_struct.rs:64:25
|
||||
|
|
||||
LL | let _ = UnitStruct::default();
|
||||
| ^^^^^^^ help: remove this call to `default`
|
||||
|
||||
error: aborting due to 4 previous errors
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue