Skip to content

Commit

Permalink
Auto merge of rust-lang#10752 - Alexendoo:default-units-macros, r=gir…
Browse files Browse the repository at this point in the history
…affate

Ignore expressions from macros in `default_constructed_unit_structs`

changelog: none, should be the same release as the lint itself
  • Loading branch information
bors committed May 8, 2023
2 parents 371120b + 68eb864 commit 3aab0dd
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
5 changes: 3 additions & 2 deletions clippy_lints/src/default_constructed_unit_structs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clippy_utils::{diagnostics::span_lint_and_sugg, is_from_proc_macro, match_def_path, paths};
use clippy_utils::{diagnostics::span_lint_and_sugg, match_def_path, paths};
use hir::{def::Res, ExprKind};
use rustc_errors::Applicability;
use rustc_hir as hir;
Expand Down Expand Up @@ -55,7 +55,8 @@ impl LateLintPass<'_> for DefaultConstructedUnitStructs {
if let ty::Adt(def, ..) = cx.typeck_results().expr_ty(expr).kind();
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);
if !var.is_field_list_non_exhaustive();
if !expr.span.from_expansion() && !qpath.span().from_expansion();
then {
span_lint_and_sugg(
cx,
Expand Down
18 changes: 18 additions & 0 deletions tests/ui/default_constructed_unit_structs.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ fn main() {
// should lint
let _ = PhantomData::<usize>;
let _: PhantomData<i32> = PhantomData;
let _: PhantomData<i32> = std::marker::PhantomData;
let _ = UnitStruct;

// should not lint
Expand All @@ -116,4 +117,21 @@ fn main() {
let _ = EmptyStruct::default();
let _ = FakeDefault::default();
let _ = <FakeDefault as Default>::default();

macro_rules! in_macro {
($i:ident) => {{
let _ = UnitStruct::default();
let _ = $i::default();
}};
}

in_macro!(UnitStruct);

macro_rules! struct_from_macro {
() => {
UnitStruct
};
}

let _ = <struct_from_macro!()>::default();
}
18 changes: 18 additions & 0 deletions tests/ui/default_constructed_unit_structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ fn main() {
// should lint
let _ = PhantomData::<usize>::default();
let _: PhantomData<i32> = PhantomData::default();
let _: PhantomData<i32> = std::marker::PhantomData::default();
let _ = UnitStruct::default();

// should not lint
Expand All @@ -116,4 +117,21 @@ fn main() {
let _ = EmptyStruct::default();
let _ = FakeDefault::default();
let _ = <FakeDefault as Default>::default();

macro_rules! in_macro {
($i:ident) => {{
let _ = UnitStruct::default();
let _ = $i::default();
}};
}

in_macro!(UnitStruct);

macro_rules! struct_from_macro {
() => {
UnitStruct
};
}

let _ = <struct_from_macro!()>::default();
}
10 changes: 8 additions & 2 deletions tests/ui/default_constructed_unit_structs.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,16 @@ 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:108:23
--> $DIR/default_constructed_unit_structs.rs:108:55
|
LL | let _: PhantomData<i32> = std::marker::PhantomData::default();
| ^^^^^^^^^^^ help: remove this call to `default`

error: use of `default` to create a unit struct
--> $DIR/default_constructed_unit_structs.rs:109:23
|
LL | let _ = UnitStruct::default();
| ^^^^^^^^^^^ help: remove this call to `default`

error: aborting due to 5 previous errors
error: aborting due to 6 previous errors

0 comments on commit 3aab0dd

Please sign in to comment.