diff options
| author | Lzu Tao <taolzu@gmail.com> | 2024-09-05 00:58:33 +0700 |
|---|---|---|
| committer | Lzu Tao <taolzu@gmail.com> | 2025-02-06 14:34:55 +0700 |
| commit | bcfd0d1aba7b63dd2b571771885375f9e1f66d1f (patch) | |
| tree | 3d126b8bf5c0f30fc4b36af61cb54f522a59f17e | |
| parent | e3e6e6ea4131db92ff23a0e32eb40a353f50f8f6 (diff) | |
| download | rust-bcfd0d1aba7b63dd2b571771885375f9e1f66d1f.tar.gz rust-bcfd0d1aba7b63dd2b571771885375f9e1f66d1f.zip | |
Skip `use_self` inside macro expansion of `impl Self` items
| -rw-r--r-- | clippy_lints/src/use_self.rs | 14 | ||||
| -rw-r--r-- | tests/ui/use_self.fixed | 4 | ||||
| -rw-r--r-- | tests/ui/use_self.stderr | 22 |
3 files changed, 16 insertions, 24 deletions
diff --git a/clippy_lints/src/use_self.rs b/clippy_lints/src/use_self.rs index 994ca9b6690..0d56cf5c2b2 100644 --- a/clippy_lints/src/use_self.rs +++ b/clippy_lints/src/use_self.rs @@ -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 @@ -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() @@ -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 diff --git a/tests/ui/use_self.fixed b/tests/ui/use_self.fixed index c6358a90f1e..572e919fb2d 100644 --- a/tests/ui/use_self.fixed +++ b/tests/ui/use_self.fixed @@ -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(); } } } diff --git a/tests/ui/use_self.stderr b/tests/ui/use_self.stderr index 06260a598be..bd5b685b45d 100644 --- a/tests/ui/use_self.stderr +++ b/tests/ui/use_self.stderr @@ -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 |
