From 16acf7d618e13c8446a359fa6728878d375ae26e Mon Sep 17 00:00:00 2001 From: flip1995 Date: Thu, 28 Feb 2019 17:34:01 +0100 Subject: [PATCH] use check_path instead of check_expr --- src/librustc/lint/internal.rs | 76 +++++++++++++---------------------- 1 file changed, 27 insertions(+), 49 deletions(-) diff --git a/src/librustc/lint/internal.rs b/src/librustc/lint/internal.rs index 22386b1c7a5..8314bf7ae9d 100644 --- a/src/librustc/lint/internal.rs +++ b/src/librustc/lint/internal.rs @@ -1,7 +1,7 @@ //! Some lints that are only useful in the compiler or crates that use compiler internals, such as //! Clippy. -use crate::hir::{Expr, ExprKind, PatKind, Path, QPath, Ty, TyKind}; +use crate::hir::{HirId, Path, QPath, Ty, TyKind}; use crate::lint::{ EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintArray, LintContext, LintPass, }; @@ -81,56 +81,34 @@ impl LintPass for TyKindUsage { } impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TyKindUsage { - fn check_expr(&mut self, cx: &LateContext<'_, '_>, expr: &'tcx Expr) { - let qpaths = match &expr.node { - ExprKind::Match(_, arms, _) => { - let mut qpaths = vec![]; - for arm in arms { - for pat in &arm.pats { - match &pat.node { - PatKind::Path(qpath) | PatKind::TupleStruct(qpath, ..) => { - qpaths.push(qpath) - } - _ => (), - } - } - } - qpaths - } - ExprKind::Path(qpath) => vec![qpath], - _ => vec![], - }; - for qpath in qpaths { - if let QPath::Resolved(_, path) = qpath { - let segments_iter = path.segments.iter().rev().skip(1).rev(); + fn check_path(&mut self, cx: &LateContext<'_, '_>, path: &'tcx Path, _: HirId) { + let segments_iter = path.segments.iter().rev().skip(1).rev(); - if let Some(last) = segments_iter.clone().last() { - if last.ident.as_str() == "TyKind" { - let path = Path { - span: path.span.with_hi(last.ident.span.hi()), - def: path.def, - segments: segments_iter.cloned().collect(), - }; + if let Some(last) = segments_iter.clone().last() { + if last.ident.as_str() == "TyKind" { + let path = Path { + span: path.span.with_hi(last.ident.span.hi()), + def: path.def, + segments: segments_iter.cloned().collect(), + }; - if let Some(def) = last.def { - if def - .def_id() - .match_path(cx.tcx, &["rustc", "ty", "sty", "TyKind"]) - { - cx.struct_span_lint( - USAGE_OF_TY_TYKIND, - path.span, - "usage of `ty::TyKind::`", - ) - .span_suggestion( - path.span, - "try using ty:: directly", - "ty".to_string(), - Applicability::MaybeIncorrect, // ty maybe needs an import - ) - .emit(); - } - } + if let Some(def) = last.def { + if def + .def_id() + .match_path(cx.tcx, &["rustc", "ty", "sty", "TyKind"]) + { + cx.struct_span_lint( + USAGE_OF_TY_TYKIND, + path.span, + "usage of `ty::TyKind::`", + ) + .span_suggestion( + path.span, + "try using ty:: directly", + "ty".to_string(), + Applicability::MaybeIncorrect, // ty maybe needs an import + ) + .emit(); } } }