Skip to content

use_self: Fix false positive in macro expansion #6954

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions clippy_lints/src/use_self.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,10 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
}

fn check_ty(&mut self, cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>) {
if in_macro(hir_ty.span) | in_impl(cx, hir_ty) | !meets_msrv(self.msrv.as_ref(), &USE_SELF_MSRV) {
if in_macro_recursively(cx, hir_ty.hir_id)
| in_impl(cx, hir_ty)
| !meets_msrv(self.msrv.as_ref(), &USE_SELF_MSRV)
{
return;
}

Expand Down Expand Up @@ -265,15 +268,12 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
// https://github.com/rust-lang/rust/blob/master/src/librustc_ast_lowering/path.rs#l142-l162
let hir = cx.tcx.hir();
let id = hir.get_parent_node(hir_ty.hir_id);

if !hir.opt_span(id).map_or(false, in_macro) {
match hir.find(id) {
Some(Node::Expr(Expr {
kind: ExprKind::Path(QPath::TypeRelative(_, segment)),
..
})) => span_lint_until_last_segment(cx, hir_ty.span, segment),
_ => span_lint(cx, hir_ty.span),
}
match hir.find(id) {
Some(Node::Expr(Expr {
kind: ExprKind::Path(QPath::TypeRelative(_, segment)),
..
})) => span_lint_until_last_segment(cx, hir_ty.span, segment),
_ => span_lint(cx, hir_ty.span),
}
}
}
Expand All @@ -288,7 +288,7 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
}
}

if in_macro(expr.span) | !meets_msrv(self.msrv.as_ref(), &USE_SELF_MSRV) {
if in_macro_recursively(cx, expr.hir_id) | !meets_msrv(self.msrv.as_ref(), &USE_SELF_MSRV) {
return;
}

Expand Down Expand Up @@ -467,3 +467,18 @@ fn should_lint_ty(hir_ty: &hir::Ty<'_>, ty: Ty<'_>, self_ty: Ty<'_>) -> bool {
}
}
}

fn in_macro_recursively(cx: &LateContext<'_>, hir_id: HirId) -> bool {
let map = cx.tcx.hir();
if map.opt_span(hir_id).map_or(false, in_macro) {
return true;
}

for (parent_id, _) in map.parent_iter(hir_id) {
if map.opt_span(parent_id).map_or(false, in_macro) {
return true;
}
}

false
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks expensive and doesn't seem like the right solution to me. We probably need to check in_macro somewhere while descending into the HIR rather than the other way around.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, we shouldn't have to traverse the HIR to determine if something comes from a macro expansion. Is this maybe a bug in rustc, not tracking the origin of this span correctly?

8 changes: 8 additions & 0 deletions tests/ui/use_self.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -461,3 +461,11 @@ mod issue6818 {
a: i32,
}
}

mod issue6902 {
#[derive(serde::Deserialize)]
#[serde(untagged)]
enum Direction {
North,
}
}
8 changes: 8 additions & 0 deletions tests/ui/use_self.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,3 +461,11 @@ mod issue6818 {
a: i32,
}
}

mod issue6902 {
#[derive(serde::Deserialize)]
#[serde(untagged)]
enum Direction {
North,
}
}