Skip to content

Commit d3d894d

Browse files
committed
Skip use_self inside macro expansion of impl Self items
1 parent 7d3f9c8 commit d3d894d

File tree

3 files changed

+17
-23
lines changed

3 files changed

+17
-23
lines changed

clippy_lints/src/use_self.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
8787
// skip over `ItemKind::OpaqueTy` in order to lint `foo() -> impl <..>`
8888
return;
8989
}
90+
9091
// We push the self types of `impl`s on a stack here. Only the top type on the stack is
9192
// relevant for linting, since this is the self type of the `impl` we're currently in. To
9293
// avoid linting on nested items, we push `StackItem::NoCheck` on the stack to signal, that
@@ -134,6 +135,14 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
134135
}
135136

136137
fn check_impl_item(&mut self, cx: &LateContext<'_>, impl_item: &hir::ImplItem<'_>) {
138+
// Checking items of `impl Self` blocks in which macro expands into.
139+
// HACK: Nested macros sometimes clears `macro_backtrace` of types in `check_ty`.
140+
// To work around this problem, we check if this impl_item is from macro expansions.
141+
// Just remember to remove this state again in `check_impl_item_post`.
142+
if impl_item.span.from_expansion() {
143+
self.stack.push(StackItem::NoCheck);
144+
return;
145+
}
137146
// We want to skip types in trait `impl`s that aren't declared as `Self` in the trait
138147
// declaration. The collection of those types is all this method implementation does.
139148
if let ImplItemKind::Fn(FnSig { decl, .. }, ..) = impl_item.kind
@@ -189,6 +198,11 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
189198
}
190199
}
191200

201+
fn check_impl_item_post(&mut self, _: &LateContext<'_>, _: &hir::ImplItem<'_>) {
202+
if let Some(StackItem::NoCheck) = self.stack.last() {
203+
self.stack.pop();
204+
}
205+
}
192206

193207
fn check_ty(&mut self, cx: &LateContext<'tcx>, hir_ty: &Ty<'tcx>) {
194208
if !hir_ty.span.from_expansion()

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)