add more test cases
This commit is contained in:
parent
4e04903631
commit
8701009860
@ -42,14 +42,14 @@ impl LateLintPass<'_> for DefaultConstructedUnitStructs {
|
||||
if_chain!(
|
||||
// make sure we have a call to `Default::default`
|
||||
if let hir::ExprKind::Call(fn_expr, &[]) = expr.kind;
|
||||
if let ExprKind::Path(ref qpath) = fn_expr.kind;
|
||||
if let ExprKind::Path(ref qpath@ hir::QPath::TypeRelative(_,_)) = fn_expr.kind;
|
||||
if let Res::Def(_, def_id) = cx.qpath_res(qpath, fn_expr.hir_id);
|
||||
if match_def_path(cx, def_id, &paths::DEFAULT_TRAIT_METHOD);
|
||||
// make sure we have a struct with no fields (unit struct)
|
||||
if let ty::Adt(def, ..) = cx.typeck_results().expr_ty(expr).kind();
|
||||
if def.is_struct() && def.is_payloadfree()
|
||||
&& !def.non_enum_variant().is_field_list_non_exhaustive()
|
||||
&& !is_from_proc_macro(cx, expr);
|
||||
if def.is_struct();
|
||||
if let var @ ty::VariantDef { ctor: Some((hir::def::CtorKind::Const, _)), .. } = def.non_enum_variant();
|
||||
if !var.is_field_list_non_exhaustive() && !is_from_proc_macro(cx, expr);
|
||||
then {
|
||||
span_lint_and_sugg(
|
||||
cx,
|
||||
|
@ -5,9 +5,23 @@ use std::marker::PhantomData;
|
||||
#[derive(Default)]
|
||||
struct UnitStruct;
|
||||
|
||||
impl UnitStruct {
|
||||
fn new() -> Self {
|
||||
//should lint
|
||||
Self::default()
|
||||
}
|
||||
}
|
||||
|
||||
#[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 {
|
||||
@ -39,6 +53,13 @@ impl NormalStruct {
|
||||
inner: PhantomData::default(),
|
||||
}
|
||||
}
|
||||
|
||||
fn new2() -> Self {
|
||||
// should not lint
|
||||
Self {
|
||||
inner: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
@ -51,8 +72,29 @@ impl<T: Default> GenericStruct<T> {
|
||||
// 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;
|
||||
@ -69,4 +111,7 @@ fn main() {
|
||||
let _ = NonExhaustiveStruct::default();
|
||||
let _ = SomeEnum::default();
|
||||
let _ = NonDefaultStruct::default();
|
||||
let _ = EmptyStruct::default();
|
||||
let _ = FakeDefault::default();
|
||||
let _ = <FakeDefault as Default>::default();
|
||||
}
|
||||
|
@ -1,28 +1,34 @@
|
||||
error: use of `default` to create a unit struct
|
||||
--> $DIR/default_constructed_unit_structs.rs:39:31
|
||||
--> $DIR/default_constructed_unit_structs.rs:11:13
|
||||
|
|
||||
LL | inner: PhantomData::default(),
|
||||
| ^^^^^^^^^^^ help: remove this call to `default`
|
||||
LL | Self::default()
|
||||
| ^^^^^^^^^^^ help: remove this call to `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:62:33
|
||||
--> $DIR/default_constructed_unit_structs.rs:53: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
|
||||
|
|
||||
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:63:42
|
||||
--> $DIR/default_constructed_unit_structs.rs:105: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:64:23
|
||||
--> $DIR/default_constructed_unit_structs.rs:106:23
|
||||
|
|
||||
LL | let _ = UnitStruct::default();
|
||||
| ^^^^^^^^^^^ help: remove this call to `default`
|
||||
|
||||
error: aborting due to 4 previous errors
|
||||
error: aborting due to 5 previous errors
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user