Remove unused_trait_names's support inside macros

This commit is contained in:
yanglsh 2025-06-18 20:33:11 +08:00
parent 5f49cf8cf5
commit e0d9cfbe19
4 changed files with 6 additions and 56 deletions

View file

@ -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(

View file

@ -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();
}
}

View file

@ -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();
}
}

View file

@ -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