Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 4de5b27

Browse files
committed
empty_struct_with_brackets: do not lint macro code
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.
1 parent 9663da3 commit 4de5b27

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
@@ -75,6 +75,7 @@ declare_lint_pass!(EmptyWithBrackets => [EMPTY_STRUCTS_WITH_BRACKETS, EMPTY_ENUM
7575
impl EarlyLintPass for EmptyWithBrackets {
7676
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
7777
if let ItemKind::Struct(ident, var_data, _) = &item.kind
78+
&& !item.span.from_expansion()
7879
&& has_brackets(var_data)
7980
&& let span_after_ident = item.span.with_lo(ident.span.hi())
8081
&& 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)