Skip to content

Commit 4067b99

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 4067b99

File tree

5 files changed

+16
-2
lines changed

5 files changed

+16
-2
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/auxiliary/macro_rules.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,10 @@ macro_rules! bad_transmute {
5757
std::mem::transmute($e)
5858
};
5959
}
60+
61+
#[macro_export]
62+
macro_rules! empty_struct {
63+
($i:ident) => {
64+
struct S {}
65+
};
66+
}

tests/ui/empty_structs_with_brackets.fixed

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//@aux-build:macro_rules.rs
12
#![warn(clippy::empty_structs_with_brackets)]
23
#![allow(dead_code)]
34

@@ -23,4 +24,6 @@ struct MyTupleStruct(usize, String); // should not trigger lint
2324
struct MySingleTupleStruct(usize); // should not trigger lint
2425
struct MyUnitLikeStruct; // should not trigger lint
2526

27+
macro_rules::empty_struct!(FromMacro);
28+
2629
fn main() {}

tests/ui/empty_structs_with_brackets.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//@aux-build:macro_rules.rs
12
#![warn(clippy::empty_structs_with_brackets)]
23
#![allow(dead_code)]
34

@@ -23,4 +24,6 @@ struct MyTupleStruct(usize, String); // should not trigger lint
2324
struct MySingleTupleStruct(usize); // should not trigger lint
2425
struct MyUnitLikeStruct; // should not trigger lint
2526

27+
macro_rules::empty_struct!(FromMacro);
28+
2629
fn main() {}

tests/ui/empty_structs_with_brackets.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: found empty brackets on struct declaration
2-
--> tests/ui/empty_structs_with_brackets.rs:4:25
2+
--> tests/ui/empty_structs_with_brackets.rs:5:25
33
|
44
LL | pub struct MyEmptyStruct {} // should trigger lint
55
| ^^^
@@ -9,7 +9,7 @@ LL | pub struct MyEmptyStruct {} // should trigger lint
99
= help: remove the brackets
1010

1111
error: found empty brackets on struct declaration
12-
--> tests/ui/empty_structs_with_brackets.rs:6:26
12+
--> tests/ui/empty_structs_with_brackets.rs:7:26
1313
|
1414
LL | struct MyEmptyTupleStruct(); // should trigger lint
1515
| ^^^

0 commit comments

Comments
 (0)