about summary refs log tree commit diff
diff options
context:
space:
mode:
authoryanglsh <yanglsh@shanghaitech.edu.cn>2025-06-01 14:41:00 +0800
committeryanglsh <yanglsh@shanghaitech.edu.cn>2025-06-30 15:11:22 +0800
commit5f49cf8cf5c4c10e6c9dd54ba76847192cdb0d90 (patch)
tree80ec2590cebeb92053763436753c652ef43a903b
parent428208eb9b61f44e5bbab70feea42a8d55994230 (diff)
downloadrust-5f49cf8cf5c4c10e6c9dd54ba76847192cdb0d90.tar.gz
rust-5f49cf8cf5c4c10e6c9dd54ba76847192cdb0d90.zip
fix: `unused_trait_names` FP when `as` name is from macro input
-rw-r--r--clippy_lints/src/unused_trait_names.rs1
-rw-r--r--tests/ui/unused_trait_names.fixed19
-rw-r--r--tests/ui/unused_trait_names.rs19
3 files changed, 39 insertions, 0 deletions
diff --git a/clippy_lints/src/unused_trait_names.rs b/clippy_lints/src/unused_trait_names.rs
index 610cec7b8c8..bc15c1376c0 100644
--- a/clippy_lints/src/unused_trait_names.rs
+++ b/clippy_lints/src/unused_trait_names.rs
@@ -73,6 +73,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedTraitNames {
             && let Some(snip) = snippet_opt(cx, last_segment.ident.span)
             && self.msrv.meets(cx, msrvs::UNDERSCORE_IMPORTS)
             && !is_from_proc_macro(cx, &last_segment.ident)
+            && (!last_segment.ident.span.from_expansion() || ident.span.from_expansion())
         {
             let complete_span = last_segment.ident.span.to(ident.span);
             span_lint_and_sugg(
diff --git a/tests/ui/unused_trait_names.fixed b/tests/ui/unused_trait_names.fixed
index 17e32ddfd9d..60e69b470c1 100644
--- a/tests/ui/unused_trait_names.fixed
+++ b/tests/ui/unused_trait_names.fixed
@@ -294,3 +294,22 @@ mod allow_lint_import {
 //         "foo".type_id();
 //     }
 // }
+
+mod issue14924 {
+    mod m {
+        pub trait Tr {
+            fn method(&self) {}
+        }
+
+        impl Tr for u8 {}
+    }
+
+    macro gen_import($Br: ident) {
+        use m::Tr as $Br;
+    }
+    gen_import!(Br);
+
+    fn do_something() {
+        0u8.method();
+    }
+}
diff --git a/tests/ui/unused_trait_names.rs b/tests/ui/unused_trait_names.rs
index 3cf8597e535..6bef8f9070e 100644
--- a/tests/ui/unused_trait_names.rs
+++ b/tests/ui/unused_trait_names.rs
@@ -294,3 +294,22 @@ mod allow_lint_import {
 //         "foo".type_id();
 //     }
 // }
+
+mod issue14924 {
+    mod m {
+        pub trait Tr {
+            fn method(&self) {}
+        }
+
+        impl Tr for u8 {}
+    }
+
+    macro gen_import($Br: ident) {
+        use m::Tr as $Br;
+    }
+    gen_import!(Br);
+
+    fn do_something() {
+        0u8.method();
+    }
+}