Skip to content

Commit bcfd0d1

Browse files
committed
Skip use_self inside macro expansion of impl Self items
1 parent e3e6e6e commit bcfd0d1

File tree

3 files changed

+16
-24
lines changed

3 files changed

+16
-24
lines changed

clippy_lints/src/use_self.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
128128
}
129129

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

191+
fn check_impl_item_post(&mut self, _: &LateContext<'_>, impl_item: &hir::ImplItem<'_>) {
192+
if impl_item.span.from_expansion()
193+
&& let Some(StackItem::NoCheck) = self.stack.last()
194+
{
195+
self.stack.pop();
196+
}
197+
}
186198

187199
fn check_ty(&mut self, cx: &LateContext<'tcx>, hir_ty: &Ty<'tcx, AmbigArg>) {
188200
if !hir_ty.span.from_expansion()
@@ -197,7 +209,7 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
197209
Res::SelfTyParam { .. } | Res::SelfTyAlias { .. } | Res::Def(DefKind::TyParam, _)
198210
)
199211
&& !types_to_skip.contains(&hir_ty.hir_id)
200-
&& let ty = ty_from_hir_ty(cx, hir_ty)
212+
&& let ty = ty_from_hir_ty(cx, hir_ty.as_unambig_ty())
201213
&& let impl_ty = cx.tcx.type_of(impl_id).instantiate_identity()
202214
&& same_type_and_consts(ty, impl_ty)
203215
// Ensure the type we encounter and the one from the impl have the same lifetime parameters. It may be that

tests/ui/use_self.fixed

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -682,12 +682,12 @@ mod issue_13092 {
682682
struct MyStruct;
683683

684684
impl MyStruct {
685-
macro_inner_item!(Self);
685+
macro_inner_item!(MyStruct);
686686
}
687687

688688
impl MyStruct {
689689
thread_local! {
690-
static SPECIAL: RefCell<Self> = RefCell::default();
690+
static SPECIAL: RefCell<MyStruct> = RefCell::default();
691691
}
692692
}
693693
}

tests/ui/use_self.stderr

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -259,25 +259,5 @@ error: unnecessary structure name repetition
259259
LL | E::A => {},
260260
| ^ help: use the applicable keyword: `Self`
261261

262-
error: unnecessary structure name repetition
263-
--> tests/ui/use_self.rs:685:27
264-
|
265-
LL | macro_inner_item!(MyStruct);
266-
| ^^^^^^^^ help: use the applicable keyword: `Self`
267-
268-
error: unnecessary structure name repetition
269-
--> tests/ui/use_self.rs:690:37
270-
|
271-
LL | static SPECIAL: RefCell<MyStruct> = RefCell::default();
272-
| ^^^^^^^^ help: use the applicable keyword: `Self`
273-
274-
error: unnecessary structure name repetition
275-
--> tests/ui/use_self.rs:690:37
276-
|
277-
LL | static SPECIAL: RefCell<MyStruct> = RefCell::default();
278-
| ^^^^^^^^ help: use the applicable keyword: `Self`
279-
|
280-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
281-
282-
error: aborting due to 46 previous errors
262+
error: aborting due to 43 previous errors
283263

0 commit comments

Comments
 (0)