about summary refs log tree commit diff
diff options
context:
space:
mode:
authoryanglsh <yanglsh@shanghaitech.edu.cn>2025-06-18 20:33:11 +0800
committeryanglsh <yanglsh@shanghaitech.edu.cn>2025-06-30 15:11:41 +0800
commite0d9cfbe199bd2cd4adf80d5d2c47107ced07405 (patch)
tree140b2319f5e07199ca7001bd1de7222808ec0552
parent5f49cf8cf5c4c10e6c9dd54ba76847192cdb0d90 (diff)
downloadrust-e0d9cfbe199bd2cd4adf80d5d2c47107ced07405.tar.gz
rust-e0d9cfbe199bd2cd4adf80d5d2c47107ced07405.zip
Remove `unused_trait_names`'s support inside macros
-rw-r--r--clippy_lints/src/unused_trait_names.rs5
-rw-r--r--tests/ui/unused_trait_names.fixed23
-rw-r--r--tests/ui/unused_trait_names.rs21
-rw-r--r--tests/ui/unused_trait_names.stderr13
4 files changed, 6 insertions, 56 deletions
diff --git a/clippy_lints/src/unused_trait_names.rs b/clippy_lints/src/unused_trait_names.rs
index bc15c1376c0..947ca37f32d 100644
--- a/clippy_lints/src/unused_trait_names.rs
+++ b/clippy_lints/src/unused_trait_names.rs
@@ -6,7 +6,7 @@ use clippy_utils::source::snippet_opt;
 use rustc_errors::Applicability;
 use rustc_hir::def::{DefKind, Res};
 use rustc_hir::{Item, ItemKind, UseKind};
-use rustc_lint::{LateContext, LateLintPass, LintContext as _};
+use rustc_lint::{LateContext, LateLintPass};
 use rustc_middle::ty::Visibility;
 use rustc_session::impl_lint_pass;
 use rustc_span::symbol::kw;
@@ -59,7 +59,7 @@ impl_lint_pass!(UnusedTraitNames => [UNUSED_TRAIT_NAMES]);
 
 impl<'tcx> LateLintPass<'tcx> for UnusedTraitNames {
     fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
-        if !item.span.in_external_macro(cx.sess().source_map())
+        if !item.span.from_expansion()
             && let ItemKind::Use(path, UseKind::Single(ident)) = item.kind
             // Ignore imports that already use Underscore
             && ident.name != kw::Underscore
@@ -73,7 +73,6 @@ 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 60e69b470c1..6abbed01bb0 100644
--- a/tests/ui/unused_trait_names.fixed
+++ b/tests/ui/unused_trait_names.fixed
@@ -200,11 +200,11 @@ fn msrv_1_33() {
     MyStruct.do_things();
 }
 
+// Linting inside macro expansion is no longer supported
 mod lint_inside_macro_expansion_bad {
     macro_rules! foo {
         () => {
-            use std::any::Any as _;
-            //~^ unused_trait_names
+            use std::any::Any;
             fn bar() {
                 "bar".type_id();
             }
@@ -294,22 +294,3 @@ 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 6bef8f9070e..4a06f062dc3 100644
--- a/tests/ui/unused_trait_names.rs
+++ b/tests/ui/unused_trait_names.rs
@@ -200,11 +200,11 @@ fn msrv_1_33() {
     MyStruct.do_things();
 }
 
+// Linting inside macro expansion is no longer supported
 mod lint_inside_macro_expansion_bad {
     macro_rules! foo {
         () => {
             use std::any::Any;
-            //~^ unused_trait_names
             fn bar() {
                 "bar".type_id();
             }
@@ -294,22 +294,3 @@ 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.stderr b/tests/ui/unused_trait_names.stderr
index 3183289d853..28067e17414 100644
--- a/tests/ui/unused_trait_names.stderr
+++ b/tests/ui/unused_trait_names.stderr
@@ -58,16 +58,5 @@ error: importing trait that is only used anonymously
 LL |     use simple_trait::{MyStruct, MyTrait};
    |                                  ^^^^^^^ help: use: `MyTrait as _`
 
-error: importing trait that is only used anonymously
-  --> tests/ui/unused_trait_names.rs:206:27
-   |
-LL |             use std::any::Any;
-   |                           ^^^ help: use: `Any as _`
-...
-LL |     foo!();
-   |     ------ in this macro invocation
-   |
-   = note: this error originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info)
-
-error: aborting due to 10 previous errors
+error: aborting due to 9 previous errors