Skip to content

Commit

Permalink
Skip use_self inside macro expansion of impl Self items
Browse files Browse the repository at this point in the history
  • Loading branch information
tesuji committed Feb 6, 2025
1 parent e3e6e6e commit bcfd0d1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 24 deletions.
14 changes: 13 additions & 1 deletion clippy_lints/src/use_self.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
}

fn check_impl_item(&mut self, cx: &LateContext<'_>, impl_item: &hir::ImplItem<'_>) {
// Checking items of `impl Self` blocks in which macro expands into.
if impl_item.span.from_expansion() {
self.stack.push(StackItem::NoCheck);
return;
}
// We want to skip types in trait `impl`s that aren't declared as `Self` in the trait
// declaration. The collection of those types is all this method implementation does.
if let ImplItemKind::Fn(FnSig { decl, .. }, ..) = impl_item.kind
Expand Down Expand Up @@ -183,6 +188,13 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
}
}

fn check_impl_item_post(&mut self, _: &LateContext<'_>, impl_item: &hir::ImplItem<'_>) {
if impl_item.span.from_expansion()
&& let Some(StackItem::NoCheck) = self.stack.last()
{
self.stack.pop();
}
}

fn check_ty(&mut self, cx: &LateContext<'tcx>, hir_ty: &Ty<'tcx, AmbigArg>) {
if !hir_ty.span.from_expansion()
Expand All @@ -197,7 +209,7 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
Res::SelfTyParam { .. } | Res::SelfTyAlias { .. } | Res::Def(DefKind::TyParam, _)
)
&& !types_to_skip.contains(&hir_ty.hir_id)
&& let ty = ty_from_hir_ty(cx, hir_ty)
&& let ty = ty_from_hir_ty(cx, hir_ty.as_unambig_ty())
&& let impl_ty = cx.tcx.type_of(impl_id).instantiate_identity()
&& same_type_and_consts(ty, impl_ty)
// Ensure the type we encounter and the one from the impl have the same lifetime parameters. It may be that
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/use_self.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -682,12 +682,12 @@ mod issue_13092 {
struct MyStruct;

impl MyStruct {
macro_inner_item!(Self);
macro_inner_item!(MyStruct);
}

impl MyStruct {
thread_local! {
static SPECIAL: RefCell<Self> = RefCell::default();
static SPECIAL: RefCell<MyStruct> = RefCell::default();
}
}
}
22 changes: 1 addition & 21 deletions tests/ui/use_self.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -259,25 +259,5 @@ error: unnecessary structure name repetition
LL | E::A => {},
| ^ help: use the applicable keyword: `Self`

error: unnecessary structure name repetition
--> tests/ui/use_self.rs:685:27
|
LL | macro_inner_item!(MyStruct);
| ^^^^^^^^ help: use the applicable keyword: `Self`

error: unnecessary structure name repetition
--> tests/ui/use_self.rs:690:37
|
LL | static SPECIAL: RefCell<MyStruct> = RefCell::default();
| ^^^^^^^^ help: use the applicable keyword: `Self`

error: unnecessary structure name repetition
--> tests/ui/use_self.rs:690:37
|
LL | static SPECIAL: RefCell<MyStruct> = RefCell::default();
| ^^^^^^^^ help: use the applicable keyword: `Self`
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

error: aborting due to 46 previous errors
error: aborting due to 43 previous errors

0 comments on commit bcfd0d1

Please sign in to comment.