Skip to content

Commit

Permalink
use_self: Fix false positive in macro expansion
Browse files Browse the repository at this point in the history
  • Loading branch information
Y-Nak committed Mar 23, 2021
1 parent b80903b commit 6edb655
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
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_recusively(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_recusively(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_recusively(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
}
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,
}
}

0 comments on commit 6edb655

Please sign in to comment.