From 31435cd7f521a3d48511d7cc1ffc61255eda5c70 Mon Sep 17 00:00:00 2001 From: flip1995 Date: Fri, 15 Mar 2019 23:41:30 +0100 Subject: [PATCH 1/9] Reimplement AbsolutePathBuffer --- clippy_lints/src/attrs.rs | 10 +-- clippy_lints/src/consts.rs | 2 +- clippy_lints/src/fallible_impl_from.rs | 2 +- clippy_lints/src/lib.rs | 1 + clippy_lints/src/types.rs | 10 +-- clippy_lints/src/utils/mod.rs | 116 ++++++++++++++++++++----- 6 files changed, 107 insertions(+), 34 deletions(-) diff --git a/clippy_lints/src/attrs.rs b/clippy_lints/src/attrs.rs index ab3b7e471ac0..0c12bab957c3 100644 --- a/clippy_lints/src/attrs.rs +++ b/clippy_lints/src/attrs.rs @@ -352,7 +352,7 @@ fn check_clippy_lint_names(cx: &LateContext<'_, '_>, items: &[NestedMetaItem]) { } } -fn is_relevant_item(tcx: TyCtxt<'_, '_, '_>, item: &Item) -> bool { +fn is_relevant_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, item: &Item) -> bool { if let ItemKind::Fn(_, _, _, eid) = item.node { is_relevant_expr(tcx, tcx.body_tables(eid), &tcx.hir().body(eid).value) } else { @@ -360,14 +360,14 @@ fn is_relevant_item(tcx: TyCtxt<'_, '_, '_>, item: &Item) -> bool { } } -fn is_relevant_impl(tcx: TyCtxt<'_, '_, '_>, item: &ImplItem) -> bool { +fn is_relevant_impl<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, item: &ImplItem) -> bool { match item.node { ImplItemKind::Method(_, eid) => is_relevant_expr(tcx, tcx.body_tables(eid), &tcx.hir().body(eid).value), _ => false, } } -fn is_relevant_trait(tcx: TyCtxt<'_, '_, '_>, item: &TraitItem) -> bool { +fn is_relevant_trait<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, item: &TraitItem) -> bool { match item.node { TraitItemKind::Method(_, TraitMethod::Required(_)) => true, TraitItemKind::Method(_, TraitMethod::Provided(eid)) => { @@ -377,7 +377,7 @@ fn is_relevant_trait(tcx: TyCtxt<'_, '_, '_>, item: &TraitItem) -> bool { } } -fn is_relevant_block(tcx: TyCtxt<'_, '_, '_>, tables: &ty::TypeckTables<'_>, block: &Block) -> bool { +fn is_relevant_block<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, tables: &ty::TypeckTables<'_>, block: &Block) -> bool { if let Some(stmt) = block.stmts.first() { match &stmt.node { StmtKind::Local(_) => true, @@ -389,7 +389,7 @@ fn is_relevant_block(tcx: TyCtxt<'_, '_, '_>, tables: &ty::TypeckTables<'_>, blo } } -fn is_relevant_expr(tcx: TyCtxt<'_, '_, '_>, tables: &ty::TypeckTables<'_>, expr: &Expr) -> bool { +fn is_relevant_expr<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, tables: &ty::TypeckTables<'_>, expr: &Expr) -> bool { match &expr.node { ExprKind::Block(block, _) => is_relevant_block(tcx, tables, block), ExprKind::Ret(Some(e)) => is_relevant_expr(tcx, tables, e), diff --git a/clippy_lints/src/consts.rs b/clippy_lints/src/consts.rs index 8d71eb542777..3d6fa273c3e3 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[..]; + if let &["core", "num", impl_ty, "max_value"] = &def_path.iter().map(|s| s.as_str()).collect::>()[..]; then { let value = match impl_ty { "" => i8::max_value() as u128, diff --git a/clippy_lints/src/fallible_impl_from.rs b/clippy_lints/src/fallible_impl_from.rs index c59a5fc20a67..f8158bd16f73 100644 --- a/clippy_lints/src/fallible_impl_from.rs +++ b/clippy_lints/src/fallible_impl_from.rs @@ -132,7 +132,7 @@ fn lint_impl_body<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, impl_span: Span, impl_it } } -fn match_type(tcx: ty::TyCtxt<'_, '_, '_>, ty: ty::Ty<'_>, path: &[&str]) -> bool { +fn match_type<'a, 'tcx>(tcx: ty::TyCtxt<'a, 'tcx, 'tcx>, ty: ty::Ty<'_>, path: &[&str]) -> bool { match ty.sty { ty::Adt(adt, _) => match_def_path(tcx, adt.did, path), _ => false, diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 4a7f23bc8587..1943373eb4a3 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -1,6 +1,7 @@ // error-pattern:cargo-clippy #![feature(box_syntax)] +#![feature(never_type)] #![feature(rustc_private)] #![feature(slice_patterns)] #![feature(stmt_expr_attributes)] diff --git a/clippy_lints/src/types.rs b/clippy_lints/src/types.rs index 4ea341822945..d4c4756e4581 100644 --- a/clippy_lints/src/types.rs +++ b/clippy_lints/src/types.rs @@ -12,6 +12,7 @@ use rustc::lint::{in_external_macro, LateContext, LateLintPass, LintArray, LintC use rustc::ty::layout::LayoutOf; use rustc::ty::{self, InferTy, Ty, TyCtxt, TypeckTables}; use rustc::{declare_tool_lint, lint_array}; +use rustc::ty::print::Printer; use rustc_errors::Applicability; use rustc_target::spec::abi::Abi; use rustc_typeck::hir_ty_to_ty; @@ -1135,15 +1136,14 @@ impl LintPass for CastPass { // Check if the given type is either `core::ffi::c_void` or // one of the platform specific `libc::::c_void` of libc. -fn is_c_void(tcx: TyCtxt<'_, '_, '_>, ty: Ty<'_>) -> bool { +fn is_c_void<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, ty: Ty<'_>) -> bool { if let ty::Adt(adt, _) = ty.sty { - let mut apb = AbsolutePathBuffer { names: vec![] }; - tcx.push_item_path(&mut apb, adt.did, false); + let names = AbsolutePathBuffer { tcx }.print_def_path(adt.did, &[]).unwrap(); - if apb.names.is_empty() { + if names.is_empty() { return false; } - if apb.names[0] == "libc" || apb.names[0] == "core" && *apb.names.last().unwrap() == "c_void" { + if names[0] == "libc" || names[0] == "core" && *names.last().unwrap() == "c_void" { return true; } } diff --git a/clippy_lints/src/utils/mod.rs b/clippy_lints/src/utils/mod.rs index bf94c8a08681..c9c11ef44e49 100644 --- a/clippy_lints/src/utils/mod.rs +++ b/clippy_lints/src/utils/mod.rs @@ -24,6 +24,8 @@ use if_chain::if_chain; use matches::matches; use rustc::hir; use rustc::hir::def::Def; +use rustc::hir::map::DisambiguatedDefPathData; +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::Node; @@ -41,7 +43,6 @@ use rustc_errors::Applicability; use syntax::ast::{self, LitKind}; use syntax::attr; use syntax::source_map::{Span, DUMMY_SP}; -use syntax::symbol; use syntax::symbol::{keywords, Symbol}; use crate::reexport::*; @@ -97,19 +98,97 @@ pub fn in_macro(span: Span) -> bool { /// Used to store the absolute path to a type. /// /// See `match_def_path` for usage. -#[derive(Debug)] -pub struct AbsolutePathBuffer { - pub names: Vec, +pub struct AbsolutePathBuffer<'a, 'tcx> { + pub tcx: TyCtxt<'a, 'tcx, 'tcx>, } -impl ty::item_path::ItemPathBuffer for AbsolutePathBuffer { - fn root_mode(&self) -> &ty::item_path::RootMode { - const ABSOLUTE: &ty::item_path::RootMode = &ty::item_path::RootMode::Absolute; - ABSOLUTE +use rustc::ty::print::Printer; + +impl<'tcx> Printer<'tcx, 'tcx> for AbsolutePathBuffer<'_, '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 push(&mut self, text: &str) { - self.names.push(symbol::Symbol::intern(text).as_str()); + fn path_crate( + self, + cnum: CrateNum, + ) -> Result { + Ok(vec![self.tcx.original_crate_name(cnum).to_string()]) + } + fn path_qualified( + self, + self_ty: Ty<'tcx>, + trait_ref: Option>, + ) -> 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), + }]) + } + + 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) => { + format!("", trait_ref, self_ty) + } + None => format!("", self_ty), + }); + + 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()); + Ok(path) + } + fn path_generic_args( + self, + print_prefix: impl FnOnce(Self) -> Result, + _args: &[Kind<'tcx>], + ) -> Result { + print_prefix(self) } } @@ -121,12 +200,10 @@ impl ty::item_path::ItemPathBuffer for AbsolutePathBuffer { /// ``` /// /// See also the `paths` module. -pub fn match_def_path(tcx: TyCtxt<'_, '_, '_>, def_id: DefId, path: &[&str]) -> bool { - let mut apb = AbsolutePathBuffer { names: vec![] }; +pub fn match_def_path<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId, path: &[&str]) -> bool { + let names = AbsolutePathBuffer { tcx }.print_def_path(def_id, &[]).unwrap(); - tcx.push_item_path(&mut apb, def_id, false); - - apb.names.len() == path.len() && apb.names.into_iter().zip(path.iter()).all(|(a, &b)| *a == *b) + 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`. @@ -138,13 +215,8 @@ pub fn match_def_path(tcx: TyCtxt<'_, '_, '_>, def_id: DefId, path: &[&str]) -> /// // The given `def_id` is that of an `Option` type /// }; /// ``` -pub fn get_def_path(tcx: TyCtxt<'_, '_, '_>, def_id: DefId) -> Vec<&'static str> { - let mut apb = AbsolutePathBuffer { names: vec![] }; - tcx.push_item_path(&mut apb, def_id, false); - apb.names - .iter() - .map(syntax_pos::symbol::LocalInternedString::get) - .collect() +pub fn get_def_path<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> Vec { + AbsolutePathBuffer { tcx }.print_def_path(def_id, &[]).unwrap() } /// Checks if type is struct, enum or union type with the given def path. From dcbd3aefa2f56f097cdbc912d288b29bc7864ea3 Mon Sep 17 00:00:00 2001 From: flip1995 Date: Fri, 15 Mar 2019 23:43:42 +0100 Subject: [PATCH 2/9] item_path_str -> def_path_str --- clippy_lints/src/eta_reduction.rs | 4 ++-- clippy_lints/src/matches.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/clippy_lints/src/eta_reduction.rs b/clippy_lints/src/eta_reduction.rs index 1329e945b370..7998a266285b 100644 --- a/clippy_lints/src/eta_reduction.rs +++ b/clippy_lints/src/eta_reduction.rs @@ -134,7 +134,7 @@ fn get_ufcs_type_name( if let Some(trait_id) = cx.tcx.trait_of_item(method_def_id) { if match_borrow_depth(expected_type_of_self, actual_type_of_self) { - return Some(cx.tcx.item_path_str(trait_id)); + return Some(cx.tcx.def_path_str(trait_id)); } } @@ -174,7 +174,7 @@ fn match_types(lhs: &ty::TyKind<'_>, rhs: &ty::TyKind<'_>) -> bool { fn get_type_name(cx: &LateContext<'_, '_>, kind: &ty::TyKind<'_>) -> String { match kind { - ty::Adt(t, _) => cx.tcx.item_path_str(t.did), + ty::Adt(t, _) => cx.tcx.def_path_str(t.did), ty::Ref(_, r, _) => get_type_name(cx, &r.sty), _ => kind.to_string(), } diff --git a/clippy_lints/src/matches.rs b/clippy_lints/src/matches.rs index 7bd3fd7f4c41..74a85f1f8174 100644 --- a/clippy_lints/src/matches.rs +++ b/clippy_lints/src/matches.rs @@ -539,7 +539,7 @@ fn check_wild_enum_match(cx: &LateContext<'_, '_>, ex: &Expr, arms: &[Arm]) { String::new() }; // This path assumes that the enum type is imported into scope. - format!("{}{}{}", ident_str, cx.tcx.item_path_str(v.did), suffix) + format!("{}{}{}", ident_str, cx.tcx.def_path_str(v.did), suffix) }) .collect(); From 8eadbfd35bf8c6728c72c26b680eb23759be8c09 Mon Sep 17 00:00:00 2001 From: flip1995 Date: Fri, 15 Mar 2019 23:44:04 +0100 Subject: [PATCH 3/9] parent_def_id -> parent --- clippy_lints/src/use_self.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/clippy_lints/src/use_self.rs b/clippy_lints/src/use_self.rs index 903e0a81b993..8fc4457aad2f 100644 --- a/clippy_lints/src/use_self.rs +++ b/clippy_lints/src/use_self.rs @@ -5,6 +5,7 @@ use rustc::hir::*; use rustc::lint::{in_external_macro, LateContext, LateLintPass, LintArray, LintContext, LintPass}; use rustc::ty; use rustc::{declare_tool_lint, lint_array}; +use rustc::ty::DefIdTree; use rustc_errors::Applicability; use syntax_pos::symbol::keywords::SelfUpper; @@ -233,7 +234,7 @@ impl<'a, 'tcx> Visitor<'tcx> for UseSelfVisitor<'a, 'tcx> { if self.item_path.def == path.def { span_use_self_lint(self.cx, path); } else if let Def::StructCtor(ctor_did, CtorKind::Fn) = path.def { - if self.item_path.def.opt_def_id() == self.cx.tcx.parent_def_id(ctor_did) { + if self.item_path.def.opt_def_id() == self.cx.tcx.parent(ctor_did) { span_use_self_lint(self.cx, path); } } From 2d8618e95cf46f9d975f38eee06a1a94d5d4cd7e Mon Sep 17 00:00:00 2001 From: flip1995 Date: Fri, 15 Mar 2019 23:44:42 +0100 Subject: [PATCH 4/9] Remove ty::TyKind from eta_reduction and replace it with ty::Ty --- clippy_lints/src/eta_reduction.rs | 28 ++++++++++++++-------------- clippy_lints/src/transmute.rs | 2 +- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/clippy_lints/src/eta_reduction.rs b/clippy_lints/src/eta_reduction.rs index 7998a266285b..2b68277ac366 100644 --- a/clippy_lints/src/eta_reduction.rs +++ b/clippy_lints/src/eta_reduction.rs @@ -129,27 +129,27 @@ fn get_ufcs_type_name( method_def_id: def_id::DefId, self_arg: &Expr, ) -> std::option::Option { - let expected_type_of_self = &cx.tcx.fn_sig(method_def_id).inputs_and_output().skip_binder()[0].sty; - let actual_type_of_self = &cx.tables.node_type(self_arg.hir_id).sty; + let expected_type_of_self = &cx.tcx.fn_sig(method_def_id).inputs_and_output().skip_binder()[0]; + let actual_type_of_self = &cx.tables.node_type(self_arg.hir_id); if let Some(trait_id) = cx.tcx.trait_of_item(method_def_id) { - if match_borrow_depth(expected_type_of_self, actual_type_of_self) { + if match_borrow_depth(expected_type_of_self, &actual_type_of_self) { return Some(cx.tcx.def_path_str(trait_id)); } } cx.tcx.impl_of_method(method_def_id).and_then(|_| { //a type may implicitly implement other type's methods (e.g. Deref) - if match_types(expected_type_of_self, actual_type_of_self) { + if match_types(expected_type_of_self, &actual_type_of_self) { return Some(get_type_name(cx, &actual_type_of_self)); } None }) } -fn match_borrow_depth(lhs: &ty::TyKind<'_>, rhs: &ty::TyKind<'_>) -> bool { - match (lhs, rhs) { - (ty::Ref(_, t1, _), ty::Ref(_, t2, _)) => match_borrow_depth(&t1.sty, &t2.sty), +fn match_borrow_depth(lhs: &ty::Ty<'_>, rhs: &ty::Ty<'_>) -> bool { + match (&lhs.sty, &rhs.sty) { + (ty::Ref(_, t1, _), ty::Ref(_, t2, _)) => match_borrow_depth(&t1, &t2), (l, r) => match (l, r) { (ty::Ref(_, _, _), _) | (_, ty::Ref(_, _, _)) => false, (_, _) => true, @@ -157,8 +157,8 @@ fn match_borrow_depth(lhs: &ty::TyKind<'_>, rhs: &ty::TyKind<'_>) -> bool { } } -fn match_types(lhs: &ty::TyKind<'_>, rhs: &ty::TyKind<'_>) -> bool { - match (lhs, rhs) { +fn match_types(lhs: &ty::Ty<'_>, rhs: &ty::Ty<'_>) -> bool { + match (&lhs.sty, &rhs.sty) { (ty::Bool, ty::Bool) | (ty::Char, ty::Char) | (ty::Int(_), ty::Int(_)) @@ -166,17 +166,17 @@ fn match_types(lhs: &ty::TyKind<'_>, rhs: &ty::TyKind<'_>) -> bool { | (ty::Str, ty::Str) => true, (ty::Ref(_, t1, _), ty::Ref(_, t2, _)) | (ty::Array(t1, _), ty::Array(t2, _)) - | (ty::Slice(t1), ty::Slice(t2)) => match_types(&t1.sty, &t2.sty), + | (ty::Slice(t1), ty::Slice(t2)) => match_types(&t1, &t2), (ty::Adt(def1, _), ty::Adt(def2, _)) => def1 == def2, (_, _) => false, } } -fn get_type_name(cx: &LateContext<'_, '_>, kind: &ty::TyKind<'_>) -> String { - match kind { +fn get_type_name(cx: &LateContext<'_, '_>, ty: &ty::Ty<'_>) -> String { + match ty.sty { ty::Adt(t, _) => cx.tcx.def_path_str(t.did), - ty::Ref(_, r, _) => get_type_name(cx, &r.sty), - _ => kind.to_string(), + ty::Ref(_, r, _) => get_type_name(cx, &r), + _ => ty.to_string(), } } diff --git a/clippy_lints/src/transmute.rs b/clippy_lints/src/transmute.rs index 2c4bfe7358d6..e4eb1bb0b740 100644 --- a/clippy_lints/src/transmute.rs +++ b/clippy_lints/src/transmute.rs @@ -347,7 +347,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Transmute { |db| { let arg = sugg::Sugg::hir(cx, &args[0], ".."); let arg = if let ty::Int(_) = from_ty.sty { - arg.as_ty(ty::Uint(ast::UintTy::U32)) + arg.as_ty(ast::UintTy::U32) } else { arg }; From dae5c9c685b8b7e936b48bedf41ce4f2e6890629 Mon Sep 17 00:00:00 2001 From: flip1995 Date: Sat, 16 Mar 2019 11:17:36 +0100 Subject: [PATCH 5/9] AbsolutePathBuffer -> AbsolutePathPrinter --- clippy_lints/src/types.rs | 4 ++-- clippy_lints/src/utils/mod.rs | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/clippy_lints/src/types.rs b/clippy_lints/src/types.rs index d4c4756e4581..4433acf8efd1 100644 --- a/clippy_lints/src/types.rs +++ b/clippy_lints/src/types.rs @@ -25,7 +25,7 @@ use crate::utils::paths; use crate::utils::{ clip, comparisons, differing_macro_contexts, higher, in_constant, in_macro, int_bits, last_path_segment, match_def_path, match_path, multispan_sugg, same_tys, sext, snippet, snippet_opt, snippet_with_applicability, - span_help_and_lint, span_lint, span_lint_and_sugg, span_lint_and_then, unsext, AbsolutePathBuffer, + span_help_and_lint, span_lint, span_lint_and_sugg, span_lint_and_then, unsext, AbsolutePathPrinter, }; /// Handles all the linting of funky types @@ -1138,7 +1138,7 @@ impl LintPass for CastPass { // one of the platform specific `libc::::c_void` of libc. fn is_c_void<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, ty: Ty<'_>) -> bool { if let ty::Adt(adt, _) = ty.sty { - let names = AbsolutePathBuffer { tcx }.print_def_path(adt.did, &[]).unwrap(); + let names = AbsolutePathPrinter { tcx }.print_def_path(adt.did, &[]).unwrap(); if names.is_empty() { return false; diff --git a/clippy_lints/src/utils/mod.rs b/clippy_lints/src/utils/mod.rs index c9c11ef44e49..b46237b57f6e 100644 --- a/clippy_lints/src/utils/mod.rs +++ b/clippy_lints/src/utils/mod.rs @@ -98,13 +98,13 @@ pub fn in_macro(span: Span) -> bool { /// Used to store the absolute path to a type. /// /// See `match_def_path` for usage. -pub struct AbsolutePathBuffer<'a, 'tcx> { +pub struct AbsolutePathPrinter<'a, 'tcx> { pub tcx: TyCtxt<'a, 'tcx, 'tcx>, } use rustc::ty::print::Printer; -impl<'tcx> Printer<'tcx, 'tcx> for AbsolutePathBuffer<'_, 'tcx> { +impl<'tcx> Printer<'tcx, 'tcx> for AbsolutePathPrinter<'_, 'tcx> { type Error = !; type Path = Vec; @@ -201,7 +201,7 @@ impl<'tcx> Printer<'tcx, 'tcx> for AbsolutePathBuffer<'_, 'tcx> { /// /// 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 = AbsolutePathBuffer { tcx }.print_def_path(def_id, &[]).unwrap(); + let names = AbsolutePathPrinter { tcx }.print_def_path(def_id, &[]).unwrap(); names.len() == path.len() && names.into_iter().zip(path.iter()).all(|(a, &b)| *a == *b) } @@ -216,7 +216,7 @@ pub fn match_def_path<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId, path /// }; /// ``` pub fn get_def_path<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> Vec { - AbsolutePathBuffer { tcx }.print_def_path(def_id, &[]).unwrap() + AbsolutePathPrinter { tcx }.print_def_path(def_id, &[]).unwrap() } /// Checks if type is struct, enum or union type with the given def path. From 254fad95de3aeed62d8d7c44398112c8f5ace25d Mon Sep 17 00:00:00 2001 From: flip1995 Date: Sat, 16 Mar 2019 11:45:29 +0100 Subject: [PATCH 6/9] 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. From 3c4616d3034d4fd4c5d737261b14900e247e9b9c Mon Sep 17 00:00:00 2001 From: flip1995 Date: Sat, 16 Mar 2019 11:51:57 +0100 Subject: [PATCH 7/9] cargo fmt --- clippy_lints/src/types.rs | 2 +- clippy_lints/src/use_self.rs | 2 +- clippy_lints/src/utils/mod.rs | 40 +++++++++++++++-------------------- 3 files changed, 19 insertions(+), 25 deletions(-) diff --git a/clippy_lints/src/types.rs b/clippy_lints/src/types.rs index 4433acf8efd1..2e50222c8a9f 100644 --- a/clippy_lints/src/types.rs +++ b/clippy_lints/src/types.rs @@ -10,9 +10,9 @@ use rustc::hir::intravisit::{walk_body, walk_expr, walk_ty, FnKind, NestedVisito use rustc::hir::*; use rustc::lint::{in_external_macro, LateContext, LateLintPass, LintArray, LintContext, LintPass}; use rustc::ty::layout::LayoutOf; +use rustc::ty::print::Printer; use rustc::ty::{self, InferTy, Ty, TyCtxt, TypeckTables}; use rustc::{declare_tool_lint, lint_array}; -use rustc::ty::print::Printer; use rustc_errors::Applicability; use rustc_target::spec::abi::Abi; use rustc_typeck::hir_ty_to_ty; diff --git a/clippy_lints/src/use_self.rs b/clippy_lints/src/use_self.rs index 8fc4457aad2f..ac901ab062f3 100644 --- a/clippy_lints/src/use_self.rs +++ b/clippy_lints/src/use_self.rs @@ -4,8 +4,8 @@ use rustc::hir::intravisit::{walk_item, walk_path, walk_ty, NestedVisitorMap, Vi use rustc::hir::*; use rustc::lint::{in_external_macro, LateContext, LateLintPass, LintArray, LintContext, LintPass}; use rustc::ty; -use rustc::{declare_tool_lint, lint_array}; use rustc::ty::DefIdTree; +use rustc::{declare_tool_lint, lint_array}; use rustc_errors::Applicability; use syntax_pos::symbol::keywords::SelfUpper; diff --git a/clippy_lints/src/utils/mod.rs b/clippy_lints/src/utils/mod.rs index 632e96abb99c..aef53ea626b2 100644 --- a/clippy_lints/src/utils/mod.rs +++ b/clippy_lints/src/utils/mod.rs @@ -24,10 +24,10 @@ use if_chain::if_chain; use matches::matches; use rustc::hir; use rustc::hir::def::Def; -use rustc::hir::map::DisambiguatedDefPathData; 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::DisambiguatedDefPathData; use rustc::hir::Node; use rustc::hir::*; use rustc::lint::{LateContext, Level, Lint, LintContext}; @@ -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, LocalInternedString}; +use syntax::symbol::{keywords, LocalInternedString, Symbol}; use crate::reexport::*; @@ -116,31 +116,22 @@ impl<'tcx> Printer<'tcx, 'tcx> for AbsolutePathPrinter<'_, 'tcx> { self.tcx } - fn print_region( - self, - _region: ty::Region<'_>, - ) -> Result { + fn print_region(self, _region: ty::Region<'_>) -> Result { Ok(()) } - fn print_type( - self, - _ty: Ty<'tcx>, - ) -> Result { + fn print_type(self, _ty: Ty<'tcx>) -> Result { Ok(()) } fn print_dyn_existential( self, _predicates: &'tcx ty::List>, - ) -> Result { + ) -> Result { Ok(()) } - fn path_crate( - self, - cnum: CrateNum, - ) -> Result { + fn path_crate(self, cnum: CrateNum) -> Result { Ok(vec![self.tcx.original_crate_name(cnum).as_str()]) } @@ -148,7 +139,7 @@ impl<'tcx> Printer<'tcx, 'tcx> for AbsolutePathPrinter<'_, 'tcx> { self, self_ty: Ty<'tcx>, trait_ref: Option>, - ) -> Result { + ) -> Result { // 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(), @@ -162,14 +153,12 @@ impl<'tcx> Printer<'tcx, 'tcx> for AbsolutePathPrinter<'_, 'tcx> { _disambiguated_data: &DisambiguatedDefPathData, self_ty: Ty<'tcx>, trait_ref: Option>, - ) -> Result { + ) -> 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() - } + Some(trait_ref) => Symbol::intern(&format!("", trait_ref, self_ty)).as_str(), None => Symbol::intern(&format!("", self_ty)).as_str(), }); @@ -180,7 +169,7 @@ impl<'tcx> Printer<'tcx, 'tcx> for AbsolutePathPrinter<'_, 'tcx> { self, print_prefix: impl FnOnce(Self) -> Result, disambiguated_data: &DisambiguatedDefPathData, - ) -> Result { + ) -> Result { let mut path = print_prefix(self)?; path.push(disambiguated_data.data.as_interned_str().as_str()); Ok(path) @@ -190,7 +179,7 @@ impl<'tcx> Printer<'tcx, 'tcx> for AbsolutePathPrinter<'_, 'tcx> { self, print_prefix: impl FnOnce(Self) -> Result, _args: &[Kind<'tcx>], - ) -> Result { + ) -> Result { print_prefix(self) } } @@ -219,7 +208,12 @@ pub fn match_def_path<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId, path /// }; /// ``` 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() + 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. From 180e07e99e408239072cad0463fc1ce649082c63 Mon Sep 17 00:00:00 2001 From: flip1995 Date: Sat, 16 Mar 2019 15:22:31 +0100 Subject: [PATCH 8/9] Adapt paths to new formatting --- clippy_lints/src/utils/mod.rs | 2 +- clippy_lints/src/utils/paths.rs | 22 +++++++++++----------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/clippy_lints/src/utils/mod.rs b/clippy_lints/src/utils/mod.rs index aef53ea626b2..0b2e47fc86a2 100644 --- a/clippy_lints/src/utils/mod.rs +++ b/clippy_lints/src/utils/mod.rs @@ -193,7 +193,7 @@ impl<'tcx> Printer<'tcx, 'tcx> for AbsolutePathPrinter<'_, 'tcx> { /// /// 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 = AbsolutePathPrinter { tcx }.print_def_path(def_id, &[]).unwrap(); + let names = get_def_path(tcx, def_id); names.len() == path.len() && names.into_iter().zip(path.iter()).all(|(a, &b)| *a == *b) } diff --git a/clippy_lints/src/utils/paths.rs b/clippy_lints/src/utils/paths.rs index 9ee25aaea5ea..34796fa20f48 100644 --- a/clippy_lints/src/utils/paths.rs +++ b/clippy_lints/src/utils/paths.rs @@ -17,7 +17,7 @@ pub const CLONE_TRAIT_METHOD: [&str; 4] = ["core", "clone", "Clone", "clone"]; pub const CMP_MAX: [&str; 3] = ["core", "cmp", "max"]; pub const CMP_MIN: [&str; 3] = ["core", "cmp", "min"]; pub const COW: [&str; 3] = ["alloc", "borrow", "Cow"]; -pub const CSTRING_NEW: [&str; 5] = ["std", "ffi", "c_str", "CString", "new"]; +pub const CSTRING_NEW: [&str; 2] = ["", "new"]; pub const DEFAULT_TRAIT: [&str; 3] = ["core", "default", "Default"]; pub const DEFAULT_TRAIT_METHOD: [&str; 4] = ["core", "default", "Default", "default"]; pub const DEREF_TRAIT_METHOD: [&str; 5] = ["core", "ops", "deref", "Deref", "deref"]; @@ -27,8 +27,8 @@ pub const DROP: [&str; 3] = ["core", "mem", "drop"]; pub const DROP_TRAIT: [&str; 4] = ["core", "ops", "drop", "Drop"]; pub const DURATION: [&str; 3] = ["core", "time", "Duration"]; pub const EARLY_CONTEXT: [&str; 4] = ["rustc", "lint", "context", "EarlyContext"]; -pub const FMT_ARGUMENTS_NEWV1: [&str; 4] = ["core", "fmt", "Arguments", "new_v1"]; -pub const FMT_ARGUMENTS_NEWV1FORMATTED: [&str; 4] = ["core", "fmt", "Arguments", "new_v1_formatted"]; +pub const FMT_ARGUMENTS_NEWV1: [&str; 2] = [">", "new_v1"]; +pub const FMT_ARGUMENTS_NEWV1FORMATTED: [&str; 2] = [">", "new_v1_formatted"]; pub const FROM_FROM: [&str; 4] = ["core", "convert", "From", "from"]; pub const FROM_TRAIT: [&str; 3] = ["core", "convert", "From"]; pub const HASH: [&str; 2] = ["hash", "Hash"]; @@ -60,11 +60,11 @@ pub const OPTION_NONE: [&str; 4] = ["core", "option", "Option", "None"]; pub const OPTION_SOME: [&str; 4] = ["core", "option", "Option", "Some"]; pub const ORD: [&str; 3] = ["core", "cmp", "Ord"]; pub const OS_STRING: [&str; 4] = ["std", "ffi", "os_str", "OsString"]; -pub const OS_STR_TO_OS_STRING: [&str; 5] = ["std", "ffi", "os_str", "OsStr", "to_os_string"]; +pub const OS_STR_TO_OS_STRING: [&str; 2] = ["", "to_os_string"]; pub const PARTIAL_ORD: [&str; 3] = ["core", "cmp", "PartialOrd"]; pub const PATH: [&str; 3] = ["std", "path", "Path"]; pub const PATH_BUF: [&str; 3] = ["std", "path", "PathBuf"]; -pub const PATH_TO_PATH_BUF: [&str; 4] = ["std", "path", "Path", "to_path_buf"]; +pub const PATH_TO_PATH_BUF: [&str; 2] = ["", "to_path_buf"]; pub const PTR_NULL: [&str; 2] = ["ptr", "null"]; pub const PTR_NULL_MUT: [&str; 2] = ["ptr", "null_mut"]; pub const RANGE: [&str; 3] = ["core", "ops", "Range"]; @@ -83,12 +83,12 @@ pub const RANGE_TO_STD: [&str; 3] = ["std", "ops", "RangeTo"]; pub const RC: [&str; 3] = ["alloc", "rc", "Rc"]; pub const RECEIVER: [&str; 4] = ["std", "sync", "mpsc", "Receiver"]; pub const REGEX: [&str; 3] = ["regex", "re_unicode", "Regex"]; -pub const REGEX_BUILDER_NEW: [&str; 5] = ["regex", "re_builder", "unicode", "RegexBuilder", "new"]; -pub const REGEX_BYTES_BUILDER_NEW: [&str; 5] = ["regex", "re_builder", "bytes", "RegexBuilder", "new"]; -pub const REGEX_BYTES_NEW: [&str; 4] = ["regex", "re_bytes", "Regex", "new"]; -pub const REGEX_BYTES_SET_NEW: [&str; 5] = ["regex", "re_set", "bytes", "RegexSet", "new"]; -pub const REGEX_NEW: [&str; 4] = ["regex", "re_unicode", "Regex", "new"]; -pub const REGEX_SET_NEW: [&str; 5] = ["regex", "re_set", "unicode", "RegexSet", "new"]; +pub const REGEX_BUILDER_NEW: [&str; 2] = ["", "new"]; +pub const REGEX_BYTES_BUILDER_NEW: [&str; 2] = ["", "new"]; +pub const REGEX_BYTES_NEW: [&str; 2] = ["", "new"]; +pub const REGEX_BYTES_SET_NEW: [&str; 2] = ["", "new"]; +pub const REGEX_NEW: [&str; 2] = ["", "new"]; +pub const REGEX_SET_NEW: [&str; 2] = ["", "new"]; pub const REPEAT: [&str; 3] = ["core", "iter", "repeat"]; pub const RESULT: [&str; 3] = ["core", "result", "Result"]; pub const RESULT_ERR: [&str; 4] = ["core", "result", "Result", "Err"]; From 4832a853c782000799bf6b1986c18f1455fb11ce Mon Sep 17 00:00:00 2001 From: flip1995 Date: Sat, 16 Mar 2019 15:39:02 +0100 Subject: [PATCH 9/9] Feed dog --- clippy_lints/src/eta_reduction.rs | 6 +++--- clippy_lints/src/utils/mod.rs | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/clippy_lints/src/eta_reduction.rs b/clippy_lints/src/eta_reduction.rs index 2b68277ac366..30436716ff95 100644 --- a/clippy_lints/src/eta_reduction.rs +++ b/clippy_lints/src/eta_reduction.rs @@ -147,7 +147,7 @@ fn get_ufcs_type_name( }) } -fn match_borrow_depth(lhs: &ty::Ty<'_>, rhs: &ty::Ty<'_>) -> bool { +fn match_borrow_depth(lhs: ty::Ty<'_>, rhs: ty::Ty<'_>) -> bool { match (&lhs.sty, &rhs.sty) { (ty::Ref(_, t1, _), ty::Ref(_, t2, _)) => match_borrow_depth(&t1, &t2), (l, r) => match (l, r) { @@ -157,7 +157,7 @@ fn match_borrow_depth(lhs: &ty::Ty<'_>, rhs: &ty::Ty<'_>) -> bool { } } -fn match_types(lhs: &ty::Ty<'_>, rhs: &ty::Ty<'_>) -> bool { +fn match_types(lhs: ty::Ty<'_>, rhs: ty::Ty<'_>) -> bool { match (&lhs.sty, &rhs.sty) { (ty::Bool, ty::Bool) | (ty::Char, ty::Char) @@ -172,7 +172,7 @@ fn match_types(lhs: &ty::Ty<'_>, rhs: &ty::Ty<'_>) -> bool { } } -fn get_type_name(cx: &LateContext<'_, '_>, ty: &ty::Ty<'_>) -> String { +fn get_type_name(cx: &LateContext<'_, '_>, ty: ty::Ty<'_>) -> String { match ty.sty { ty::Adt(t, _) => cx.tcx.def_path_str(t.did), ty::Ref(_, r, _) => get_type_name(cx, &r), diff --git a/clippy_lints/src/utils/mod.rs b/clippy_lints/src/utils/mod.rs index 0b2e47fc86a2..c91065e5c1c8 100644 --- a/clippy_lints/src/utils/mod.rs +++ b/clippy_lints/src/utils/mod.rs @@ -104,6 +104,7 @@ pub struct AbsolutePathPrinter<'a, 'tcx> { use rustc::ty::print::Printer; +#[allow(clippy::diverging_sub_expression)] impl<'tcx> Printer<'tcx, 'tcx> for AbsolutePathPrinter<'_, 'tcx> { type Error = !;