From 254fad95de3aeed62d8d7c44398112c8f5ace25d Mon Sep 17 00:00:00 2001 From: flip1995 Date: Sat, 16 Mar 2019 11:45:29 +0100 Subject: [PATCH] Use LocalInternedString inside of AbsolutePathPrinter --- clippy_lints/src/consts.rs | 2 +- clippy_lints/src/utils/mod.rs | 23 +++++++++++++---------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/clippy_lints/src/consts.rs b/clippy_lints/src/consts.rs index 3d6fa273c3e3..8d71eb542777 100644 --- a/clippy_lints/src/consts.rs +++ b/clippy_lints/src/consts.rs @@ -245,7 +245,7 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> { let def = self.tables.qpath_def(qpath, callee.hir_id); if let Some(def_id) = def.opt_def_id(); let def_path = get_def_path(self.tcx, def_id); - if let &["core", "num", impl_ty, "max_value"] = &def_path.iter().map(|s| s.as_str()).collect::>()[..]; + if let &["core", "num", impl_ty, "max_value"] = &def_path[..]; then { let value = match impl_ty { "" => i8::max_value() as u128, diff --git a/clippy_lints/src/utils/mod.rs b/clippy_lints/src/utils/mod.rs index b46237b57f6e..632e96abb99c 100644 --- a/clippy_lints/src/utils/mod.rs +++ b/clippy_lints/src/utils/mod.rs @@ -43,7 +43,7 @@ use rustc_errors::Applicability; use syntax::ast::{self, LitKind}; use syntax::attr; use syntax::source_map::{Span, DUMMY_SP}; -use syntax::symbol::{keywords, Symbol}; +use syntax::symbol::{keywords, Symbol, LocalInternedString}; use crate::reexport::*; @@ -107,7 +107,7 @@ use rustc::ty::print::Printer; impl<'tcx> Printer<'tcx, 'tcx> for AbsolutePathPrinter<'_, 'tcx> { type Error = !; - type Path = Vec; + type Path = Vec; type Region = (); type Type = (); type DynExistential = (); @@ -141,8 +141,9 @@ impl<'tcx> Printer<'tcx, 'tcx> for AbsolutePathPrinter<'_, 'tcx> { self, cnum: CrateNum, ) -> Result { - Ok(vec![self.tcx.original_crate_name(cnum).to_string()]) + Ok(vec![self.tcx.original_crate_name(cnum).as_str()]) } + fn path_qualified( self, self_ty: Ty<'tcx>, @@ -150,8 +151,8 @@ impl<'tcx> Printer<'tcx, 'tcx> for AbsolutePathPrinter<'_, 'tcx> { ) -> Result { // This shouldn't ever be needed, but just in case: Ok(vec![match trait_ref { - Some(trait_ref) => format!("{:?}", trait_ref), - None => format!("<{}>", self_ty), + Some(trait_ref) => Symbol::intern(&format!("{:?}", trait_ref)).as_str(), + None => Symbol::intern(&format!("<{}>", self_ty)).as_str(), }]) } @@ -167,22 +168,24 @@ impl<'tcx> Printer<'tcx, 'tcx> for AbsolutePathPrinter<'_, 'tcx> { // This shouldn't ever be needed, but just in case: path.push(match trait_ref { Some(trait_ref) => { - format!("", trait_ref, self_ty) + Symbol::intern(&format!("", trait_ref, self_ty)).as_str() } - None => format!("", self_ty), + 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)?; - path.push(disambiguated_data.data.as_interned_str().to_string()); + path.push(disambiguated_data.data.as_interned_str().as_str()); Ok(path) } + fn path_generic_args( self, print_prefix: impl FnOnce(Self) -> Result, @@ -215,8 +218,8 @@ pub fn match_def_path<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId, 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 { - AbsolutePathPrinter { tcx }.print_def_path(def_id, &[]).unwrap() +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.