Skip to content

Commit 3aab0dd

Browse files
committed
Auto merge of #10752 - Alexendoo:default-units-macros, r=giraffate
Ignore expressions from macros in `default_constructed_unit_structs` changelog: none, should be the same release as the lint itself
2 parents 371120b + 68eb864 commit 3aab0dd

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

clippy_lints/src/default_constructed_unit_structs.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::{diagnostics::span_lint_and_sugg, is_from_proc_macro, match_def_path, paths};
1+
use clippy_utils::{diagnostics::span_lint_and_sugg, match_def_path, paths};
22
use hir::{def::Res, ExprKind};
33
use rustc_errors::Applicability;
44
use rustc_hir as hir;
@@ -55,7 +55,8 @@ impl LateLintPass<'_> for DefaultConstructedUnitStructs {
5555
if let ty::Adt(def, ..) = cx.typeck_results().expr_ty(expr).kind();
5656
if def.is_struct();
5757
if let var @ ty::VariantDef { ctor: Some((hir::def::CtorKind::Const, _)), .. } = def.non_enum_variant();
58-
if !var.is_field_list_non_exhaustive() && !is_from_proc_macro(cx, expr);
58+
if !var.is_field_list_non_exhaustive();
59+
if !expr.span.from_expansion() && !qpath.span().from_expansion();
5960
then {
6061
span_lint_and_sugg(
6162
cx,

tests/ui/default_constructed_unit_structs.fixed

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ fn main() {
105105
// should lint
106106
let _ = PhantomData::<usize>;
107107
let _: PhantomData<i32> = PhantomData;
108+
let _: PhantomData<i32> = std::marker::PhantomData;
108109
let _ = UnitStruct;
109110

110111
// should not lint
@@ -116,4 +117,21 @@ fn main() {
116117
let _ = EmptyStruct::default();
117118
let _ = FakeDefault::default();
118119
let _ = <FakeDefault as Default>::default();
120+
121+
macro_rules! in_macro {
122+
($i:ident) => {{
123+
let _ = UnitStruct::default();
124+
let _ = $i::default();
125+
}};
126+
}
127+
128+
in_macro!(UnitStruct);
129+
130+
macro_rules! struct_from_macro {
131+
() => {
132+
UnitStruct
133+
};
134+
}
135+
136+
let _ = <struct_from_macro!()>::default();
119137
}

tests/ui/default_constructed_unit_structs.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ fn main() {
105105
// should lint
106106
let _ = PhantomData::<usize>::default();
107107
let _: PhantomData<i32> = PhantomData::default();
108+
let _: PhantomData<i32> = std::marker::PhantomData::default();
108109
let _ = UnitStruct::default();
109110

110111
// should not lint
@@ -116,4 +117,21 @@ fn main() {
116117
let _ = EmptyStruct::default();
117118
let _ = FakeDefault::default();
118119
let _ = <FakeDefault as Default>::default();
120+
121+
macro_rules! in_macro {
122+
($i:ident) => {{
123+
let _ = UnitStruct::default();
124+
let _ = $i::default();
125+
}};
126+
}
127+
128+
in_macro!(UnitStruct);
129+
130+
macro_rules! struct_from_macro {
131+
() => {
132+
UnitStruct
133+
};
134+
}
135+
136+
let _ = <struct_from_macro!()>::default();
119137
}

tests/ui/default_constructed_unit_structs.stderr

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,16 @@ LL | let _: PhantomData<i32> = PhantomData::default();
2525
| ^^^^^^^^^^^ help: remove this call to `default`
2626

2727
error: use of `default` to create a unit struct
28-
--> $DIR/default_constructed_unit_structs.rs:108:23
28+
--> $DIR/default_constructed_unit_structs.rs:108:55
29+
|
30+
LL | let _: PhantomData<i32> = std::marker::PhantomData::default();
31+
| ^^^^^^^^^^^ help: remove this call to `default`
32+
33+
error: use of `default` to create a unit struct
34+
--> $DIR/default_constructed_unit_structs.rs:109:23
2935
|
3036
LL | let _ = UnitStruct::default();
3137
| ^^^^^^^^^^^ help: remove this call to `default`
3238

33-
error: aborting due to 5 previous errors
39+
error: aborting due to 6 previous errors
3440

0 commit comments

Comments
 (0)