From 5837f2d031af2c81664dd4b9c691ebac5b2ff572 Mon Sep 17 00:00:00 2001 From: flip1995 Date: Sun, 7 Apr 2019 19:40:46 +0200 Subject: [PATCH] Remove uplifted functions {get,match}_def_path from Clippy --- clippy_lints/src/utils/mod.rs | 138 +--------------------------------- 1 file changed, 1 insertion(+), 137 deletions(-) diff --git a/clippy_lints/src/utils/mod.rs b/clippy_lints/src/utils/mod.rs index 542a18635d32..b0b7231ddbfd 100644 --- a/clippy_lints/src/utils/mod.rs +++ b/clippy_lints/src/utils/mod.rs @@ -24,10 +24,8 @@ use if_chain::if_chain; use matches::matches; use rustc::hir; use rustc::hir::def::Def; -use rustc::hir::def_id::CrateNum; use rustc::hir::def_id::{DefId, CRATE_DEF_INDEX, LOCAL_CRATE}; use rustc::hir::intravisit::{NestedVisitorMap, Visitor}; -use rustc::hir::map::{DefPathData, DisambiguatedDefPathData}; use rustc::hir::Node; use rustc::hir::*; use rustc::lint::{LateContext, Level, Lint, LintContext}; @@ -43,7 +41,7 @@ use rustc_errors::Applicability; use syntax::ast::{self, LitKind}; use syntax::attr; use syntax::source_map::{Span, DUMMY_SP}; -use syntax::symbol::{keywords, LocalInternedString, Symbol}; +use syntax::symbol::{keywords, Symbol}; use crate::reexport::*; @@ -95,140 +93,6 @@ pub fn in_macro(span: Span) -> bool { span.ctxt().outer().expn_info().is_some() } -/// Used to store the absolute path to a type. -/// -/// See `match_def_path` for usage. -pub struct AbsolutePathPrinter<'a, 'tcx> { - pub tcx: TyCtxt<'a, 'tcx, 'tcx>, -} - -use rustc::ty::print::Printer; - -#[allow(clippy::diverging_sub_expression)] -impl<'tcx> Printer<'tcx, 'tcx> for AbsolutePathPrinter<'_, 'tcx> { - type Error = !; - - type Path = Vec; - type Region = (); - type Type = (); - type DynExistential = (); - - fn tcx<'a>(&'a self) -> TyCtxt<'a, 'tcx, 'tcx> { - self.tcx - } - - fn print_region(self, _region: ty::Region<'_>) -> Result { - Ok(()) - } - - fn print_type(self, _ty: Ty<'tcx>) -> Result { - Ok(()) - } - - fn print_dyn_existential( - self, - _predicates: &'tcx ty::List>, - ) -> Result { - Ok(()) - } - - fn path_crate(self, cnum: CrateNum) -> Result { - Ok(vec![self.tcx.original_crate_name(cnum).as_str()]) - } - - fn path_qualified( - self, - self_ty: Ty<'tcx>, - trait_ref: Option>, - ) -> Result { - if trait_ref.is_none() { - if let ty::Adt(def, substs) = self_ty.sty { - return self.print_def_path(def.did, substs); - } - } - - // This shouldn't ever be needed, but just in case: - Ok(vec![match trait_ref { - Some(trait_ref) => Symbol::intern(&format!("{:?}", trait_ref)).as_str(), - None => Symbol::intern(&format!("<{}>", self_ty)).as_str(), - }]) - } - - fn path_append_impl( - self, - print_prefix: impl FnOnce(Self) -> Result, - _disambiguated_data: &DisambiguatedDefPathData, - self_ty: Ty<'tcx>, - trait_ref: Option>, - ) -> Result { - let mut path = print_prefix(self)?; - - // This shouldn't ever be needed, but just in case: - path.push(match trait_ref { - Some(trait_ref) => Symbol::intern(&format!("", trait_ref, self_ty)).as_str(), - None => Symbol::intern(&format!("", self_ty)).as_str(), - }); - - Ok(path) - } - - fn path_append( - self, - print_prefix: impl FnOnce(Self) -> Result, - disambiguated_data: &DisambiguatedDefPathData, - ) -> Result { - let mut path = print_prefix(self)?; - - // Skip `::{{constructor}}` on tuple/unit structs. - if let DefPathData::Ctor = disambiguated_data.data { - return Ok(path); - } - - path.push(disambiguated_data.data.as_interned_str().as_str()); - Ok(path) - } - - fn path_generic_args( - self, - print_prefix: impl FnOnce(Self) -> Result, - _args: &[Kind<'tcx>], - ) -> Result { - print_prefix(self) - } -} - -/// Checks if a `DefId`'s path matches the given absolute type path usage. -/// -/// # Examples -/// ```rust,ignore -/// match_def_path(cx.tcx, id, &["core", "option", "Option"]) -/// ``` -/// -/// See also the `paths` module. -pub fn match_def_path<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId, path: &[&str]) -> bool { - let names = get_def_path(tcx, def_id); - - names.len() == path.len() && names.into_iter().zip(path.iter()).all(|(a, &b)| *a == *b) -} - -/// Gets the absolute path of `def_id` as a vector of `&str`. -/// -/// # Examples -/// ```rust,ignore -/// let def_path = get_def_path(tcx, def_id); -/// if let &["core", "option", "Option"] = &def_path[..] { -/// // The given `def_id` is that of an `Option` type -/// }; -/// ``` -pub fn get_def_path<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> Vec<&'static str> { - AbsolutePathPrinter { tcx } - .print_def_path(def_id, &[]) - .unwrap() - .iter() - .map(LocalInternedString::get) - .collect() -} - /// Checks if type is struct, enum or union type with the given def path. pub fn match_type(cx: &LateContext<'_, '_>, ty: Ty<'_>, path: &[&str]) -> bool { match ty.sty {