Skip to content

Commit da7b678

Browse files
authored
empty_struct_with_brackets: do not lint code coming from macro expansion (#14623)
Do not attempt to fetch a snippet from expansion. Without this change, the inside of macros could[*] be shown as the source of the problem. [*] Due to the way the source code is processed and reparsed in this macro, the declarative macro has to be located outside the current source file for the bug to appear. Otherwise, the macro call itself will be (mis)identified as a potential `struct` field definition and the lint will not trigger. changelog: [`empty_struct_with_brackets`]: do not lint code coming from macro expansion
2 parents daeb6a1 + 4de5b27 commit da7b678

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

clippy_lints/src/empty_with_brackets.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ impl_lint_pass!(EmptyWithBrackets => [EMPTY_STRUCTS_WITH_BRACKETS, EMPTY_ENUM_VA
9393
impl LateLintPass<'_> for EmptyWithBrackets {
9494
fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
9595
if let ItemKind::Struct(ident, var_data, _) = &item.kind
96+
&& !item.span.from_expansion()
9697
&& has_brackets(var_data)
9798
&& let span_after_ident = item.span.with_lo(ident.span.hi())
9899
&& has_no_fields(cx, var_data, span_after_ident)

tests/ui/empty_structs_with_brackets.fixed

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,12 @@ struct MyTupleStruct(usize, String); // should not trigger lint
2323
struct MySingleTupleStruct(usize); // should not trigger lint
2424
struct MyUnitLikeStruct; // should not trigger lint
2525

26+
macro_rules! empty_struct {
27+
($s:ident) => {
28+
struct $s {}
29+
};
30+
}
31+
32+
empty_struct!(FromMacro);
33+
2634
fn main() {}

tests/ui/empty_structs_with_brackets.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,12 @@ struct MyTupleStruct(usize, String); // should not trigger lint
2323
struct MySingleTupleStruct(usize); // should not trigger lint
2424
struct MyUnitLikeStruct; // should not trigger lint
2525

26+
macro_rules! empty_struct {
27+
($s:ident) => {
28+
struct $s {}
29+
};
30+
}
31+
32+
empty_struct!(FromMacro);
33+
2634
fn main() {}

0 commit comments

Comments
 (0)