about summary refs log tree commit diff
diff options
context:
space:
mode:
authorTimo <30553356+y21@users.noreply.github.com>2025-05-17 10:43:54 +0000
committerGitHub <noreply@github.com>2025-05-17 10:43:54 +0000
commitda7b678992dd65dbd644bfe30ba61a9a0d2c695c (patch)
treec96bbfc1b21730ce029e2c8d49e915cb5292a3b1
parentdaeb6a1d180b040ed4f61485cc1294859a287f88 (diff)
parent4de5b2757df953e4f0198aca3cf89c720e6bcbdf (diff)
`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
-rw-r--r--clippy_lints/src/empty_with_brackets.rs1
-rw-r--r--tests/ui/empty_structs_with_brackets.fixed8
-rw-r--r--tests/ui/empty_structs_with_brackets.rs8
3 files changed, 17 insertions, 0 deletions
diff --git a/clippy_lints/src/empty_with_brackets.rs b/clippy_lints/src/empty_with_brackets.rs
index a38d6df89f2..8c12364883c 100644
--- a/clippy_lints/src/empty_with_brackets.rs
+++ b/clippy_lints/src/empty_with_brackets.rs
@@ -93,6 +93,7 @@ impl_lint_pass!(EmptyWithBrackets => [EMPTY_STRUCTS_WITH_BRACKETS, EMPTY_ENUM_VA
 impl LateLintPass<'_> for EmptyWithBrackets {
     fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
         if let ItemKind::Struct(ident, var_data, _) = &item.kind
+            && !item.span.from_expansion()
             && has_brackets(var_data)
             && let span_after_ident = item.span.with_lo(ident.span.hi())
             && has_no_fields(cx, var_data, span_after_ident)
diff --git a/tests/ui/empty_structs_with_brackets.fixed b/tests/ui/empty_structs_with_brackets.fixed
index b1600862a8f..419cf2354f8 100644
--- a/tests/ui/empty_structs_with_brackets.fixed
+++ b/tests/ui/empty_structs_with_brackets.fixed
@@ -23,4 +23,12 @@ struct MyTupleStruct(usize, String); // should not trigger lint
 struct MySingleTupleStruct(usize); // should not trigger lint
 struct MyUnitLikeStruct; // should not trigger lint
 
+macro_rules! empty_struct {
+    ($s:ident) => {
+        struct $s {}
+    };
+}
+
+empty_struct!(FromMacro);
+
 fn main() {}
diff --git a/tests/ui/empty_structs_with_brackets.rs b/tests/ui/empty_structs_with_brackets.rs
index 1f69c4be9ec..90c415c1220 100644
--- a/tests/ui/empty_structs_with_brackets.rs
+++ b/tests/ui/empty_structs_with_brackets.rs
@@ -23,4 +23,12 @@ struct MyTupleStruct(usize, String); // should not trigger lint
 struct MySingleTupleStruct(usize); // should not trigger lint
 struct MyUnitLikeStruct; // should not trigger lint
 
+macro_rules! empty_struct {
+    ($s:ident) => {
+        struct $s {}
+    };
+}
+
+empty_struct!(FromMacro);
+
 fn main() {}