From 31435cd7f521a3d48511d7cc1ffc61255eda5c70 Mon Sep 17 00:00:00 2001 From: flip1995 Date: Fri, 15 Mar 2019 23:41:30 +0100 Subject: [PATCH 01/20] 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 02/20] 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 03/20] 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 04/20] 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 05/20] 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 06/20] 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 07/20] 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 08/20] 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 09/20] 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 = !; From 9b7ff501a65f132ee4bfdea0aac4807e5a7cc877 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Miku=C5=82a?= Date: Mon, 18 Mar 2019 12:43:10 +0100 Subject: [PATCH 10/20] Replace most of ty:Ty with Ty --- clippy_lints/src/consts.rs | 6 +++--- clippy_lints/src/eta_reduction.rs | 8 ++++---- clippy_lints/src/fallible_impl_from.rs | 4 ++-- clippy_lints/src/map_unit_fn.rs | 4 ++-- clippy_lints/src/methods/mod.rs | 4 ++-- clippy_lints/src/non_copy_const.rs | 4 ++-- clippy_lints/src/redundant_clone.rs | 4 ++-- clippy_lints/src/utils/mod.rs | 2 +- 8 files changed, 18 insertions(+), 18 deletions(-) diff --git a/clippy_lints/src/consts.rs b/clippy_lints/src/consts.rs index 8d71eb542777..61bfc78b3de8 100644 --- a/clippy_lints/src/consts.rs +++ b/clippy_lints/src/consts.rs @@ -117,7 +117,7 @@ impl Hash for Constant { } impl Constant { - pub fn partial_cmp(tcx: TyCtxt<'_, '_, '_>, cmp_type: ty::Ty<'_>, left: &Self, right: &Self) -> Option { + pub fn partial_cmp(tcx: TyCtxt<'_, '_, '_>, cmp_type: Ty<'_>, left: &Self, right: &Self) -> Option { match (left, right) { (&Constant::Str(ref ls), &Constant::Str(ref rs)) => Some(ls.cmp(rs)), (&Constant::Char(ref l), &Constant::Char(ref r)) => Some(l.cmp(r)), @@ -268,7 +268,7 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> { } #[allow(clippy::cast_possible_wrap)] - fn constant_not(&self, o: &Constant, ty: ty::Ty<'_>) -> Option { + fn constant_not(&self, o: &Constant, ty: Ty<'_>) -> Option { use self::Constant::*; match *o { Bool(b) => Some(Bool(!b)), @@ -284,7 +284,7 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> { } } - fn constant_negate(&self, o: &Constant, ty: ty::Ty<'_>) -> Option { + fn constant_negate(&self, o: &Constant, ty: Ty<'_>) -> Option { use self::Constant::*; match *o { Int(value) => { diff --git a/clippy_lints/src/eta_reduction.rs b/clippy_lints/src/eta_reduction.rs index 30436716ff95..e0fa9e5e929b 100644 --- a/clippy_lints/src/eta_reduction.rs +++ b/clippy_lints/src/eta_reduction.rs @@ -1,7 +1,7 @@ use if_chain::if_chain; use rustc::hir::*; use rustc::lint::{in_external_macro, LateContext, LateLintPass, LintArray, LintContext, LintPass}; -use rustc::ty; +use rustc::ty::{self, Ty}; use rustc::{declare_tool_lint, lint_array}; use rustc_errors::Applicability; @@ -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<'_>, rhs: 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<'_>, rhs: 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<'_>) -> 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/fallible_impl_from.rs b/clippy_lints/src/fallible_impl_from.rs index f8158bd16f73..bed5964fb32f 100644 --- a/clippy_lints/src/fallible_impl_from.rs +++ b/clippy_lints/src/fallible_impl_from.rs @@ -3,7 +3,7 @@ use crate::utils::{is_expn_of, match_def_path, method_chain_args, span_lint_and_ use if_chain::if_chain; use rustc::hir; use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; -use rustc::ty; +use rustc::ty::{self, Ty}; use rustc::{declare_tool_lint, lint_array}; use syntax_pos::Span; @@ -132,7 +132,7 @@ fn lint_impl_body<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, impl_span: Span, impl_it } } -fn match_type<'a, 'tcx>(tcx: ty::TyCtxt<'a, 'tcx, 'tcx>, ty: ty::Ty<'_>, path: &[&str]) -> bool { +fn match_type<'a, 'tcx>(tcx: ty::TyCtxt<'a, 'tcx, 'tcx>, 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/map_unit_fn.rs b/clippy_lints/src/map_unit_fn.rs index f96418201da7..22c336b8c478 100644 --- a/clippy_lints/src/map_unit_fn.rs +++ b/clippy_lints/src/map_unit_fn.rs @@ -3,7 +3,7 @@ use crate::utils::{in_macro, iter_input_pats, match_type, method_chain_args, sni use if_chain::if_chain; use rustc::hir; use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; -use rustc::ty; +use rustc::ty::{self, Ty}; use rustc::{declare_tool_lint, lint_array}; use rustc_errors::Applicability; use syntax::source_map::Span; @@ -87,7 +87,7 @@ impl LintPass for Pass { } } -fn is_unit_type(ty: ty::Ty<'_>) -> bool { +fn is_unit_type(ty: Ty<'_>) -> bool { match ty.sty { ty::Tuple(slice) => slice.is_empty(), ty::Never => true, diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index d7ab35057655..3e7403a3fe28 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -2196,7 +2196,7 @@ fn lint_asref(cx: &LateContext<'_, '_>, expr: &hir::Expr, call_name: &str, as_re fn ty_has_iter_method( cx: &LateContext<'_, '_>, - self_ref_ty: ty::Ty<'_>, + self_ref_ty: Ty<'_>, ) -> Option<(&'static Lint, &'static str, &'static str)> { if let Some(ty_name) = has_iter_method(cx, self_ref_ty) { let lint = match ty_name { @@ -2217,7 +2217,7 @@ fn ty_has_iter_method( } } -fn lint_into_iter(cx: &LateContext<'_, '_>, expr: &hir::Expr, self_ref_ty: ty::Ty<'_>, method_span: Span) { +fn lint_into_iter(cx: &LateContext<'_, '_>, expr: &hir::Expr, self_ref_ty: Ty<'_>, method_span: Span) { if !match_trait_method(cx, expr, &paths::INTO_ITERATOR) { return; } diff --git a/clippy_lints/src/non_copy_const.rs b/clippy_lints/src/non_copy_const.rs index 9c184e8c54b2..8b7b6b6c42c6 100644 --- a/clippy_lints/src/non_copy_const.rs +++ b/clippy_lints/src/non_copy_const.rs @@ -8,7 +8,7 @@ use rustc::hir::def::Def; use rustc::hir::*; use rustc::lint::{LateContext, LateLintPass, Lint, LintArray, LintPass}; use rustc::ty::adjustment::Adjust; -use rustc::ty::{self, TypeFlags}; +use rustc::ty::{Ty, TypeFlags}; use rustc::{declare_tool_lint, lint_array}; use rustc_errors::Applicability; use rustc_typeck::hir_ty_to_ty; @@ -108,7 +108,7 @@ impl Source { } } -fn verify_ty_bound<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, ty: ty::Ty<'tcx>, source: Source) { +fn verify_ty_bound<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, ty: Ty<'tcx>, source: Source) { if ty.is_freeze(cx.tcx, cx.param_env, DUMMY_SP) || is_copy(cx, ty) { // An `UnsafeCell` is `!Copy`, and an `UnsafeCell` is also the only type which // is `!Freeze`, thus if our type is `Copy` we can be sure it must be `Freeze` diff --git a/clippy_lints/src/redundant_clone.rs b/clippy_lints/src/redundant_clone.rs index 9a17e593fdfa..086c76c20b63 100644 --- a/clippy_lints/src/redundant_clone.rs +++ b/clippy_lints/src/redundant_clone.rs @@ -12,7 +12,7 @@ use rustc::mir::{ visit::{MutatingUseContext, PlaceContext, Visitor}, TerminatorKind, }; -use rustc::ty; +use rustc::ty::{self, Ty}; use rustc::{declare_tool_lint, lint_array}; use rustc_errors::Applicability; use std::convert::TryFrom; @@ -225,7 +225,7 @@ fn is_call_with_ref_arg<'tcx>( cx: &LateContext<'_, 'tcx>, mir: &'tcx mir::Mir<'tcx>, kind: &'tcx mir::TerminatorKind<'tcx>, -) -> Option<(def_id::DefId, mir::Local, ty::Ty<'tcx>, Option<&'tcx mir::Place<'tcx>>)> { +) -> Option<(def_id::DefId, mir::Local, Ty<'tcx>, Option<&'tcx mir::Place<'tcx>>)> { if_chain! { if let TerminatorKind::Call { func, args, destination, .. } = kind; if args.len() == 1; diff --git a/clippy_lints/src/utils/mod.rs b/clippy_lints/src/utils/mod.rs index c91065e5c1c8..7ffe226f5fc5 100644 --- a/clippy_lints/src/utils/mod.rs +++ b/clippy_lints/src/utils/mod.rs @@ -1080,7 +1080,7 @@ pub fn any_parent_is_automatically_derived(tcx: TyCtxt<'_, '_, '_>, node: HirId) } /// Returns true if ty has `iter` or `iter_mut` methods -pub fn has_iter_method(cx: &LateContext<'_, '_>, probably_ref_ty: ty::Ty<'_>) -> Option<&'static str> { +pub fn has_iter_method(cx: &LateContext<'_, '_>, probably_ref_ty: Ty<'_>) -> Option<&'static str> { // FIXME: instead of this hard-coded list, we should check if `::iter` // exists and has the desired signature. Unfortunately FnCtxt is not exported // so we can't use its `lookup_method` method. From cb3aa7480c5da6d581720c3e9a6c9da5590aa553 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Miku=C5=82a?= Date: Mon, 18 Mar 2019 12:49:12 +0100 Subject: [PATCH 11/20] Fix path_qualified As suggested by eddyb --- clippy_lints/src/utils/mod.rs | 6 ++++++ clippy_lints/src/utils/paths.rs | 22 +++++++++++----------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/clippy_lints/src/utils/mod.rs b/clippy_lints/src/utils/mod.rs index 7ffe226f5fc5..586a33384e0b 100644 --- a/clippy_lints/src/utils/mod.rs +++ b/clippy_lints/src/utils/mod.rs @@ -141,6 +141,12 @@ impl<'tcx> Printer<'tcx, 'tcx> for AbsolutePathPrinter<'_, 'tcx> { 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(), diff --git a/clippy_lints/src/utils/paths.rs b/clippy_lints/src/utils/paths.rs index 34796fa20f48..9ee25aaea5ea 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; 2] = ["", "new"]; +pub const CSTRING_NEW: [&str; 5] = ["std", "ffi", "c_str", "CString", "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; 2] = [">", "new_v1"]; -pub const FMT_ARGUMENTS_NEWV1FORMATTED: [&str; 2] = [">", "new_v1_formatted"]; +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 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; 2] = ["", "to_os_string"]; +pub const OS_STR_TO_OS_STRING: [&str; 5] = ["std", "ffi", "os_str", "OsStr", "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; 2] = ["", "to_path_buf"]; +pub const PATH_TO_PATH_BUF: [&str; 4] = ["std", "path", "Path", "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; 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 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 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 cf71caa71dd9a330749419f9936c78d610bef3e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Miku=C5=82a?= Date: Mon, 18 Mar 2019 12:52:24 +0100 Subject: [PATCH 12/20] Drop redundant & --- clippy_lints/src/eta_reduction.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clippy_lints/src/eta_reduction.rs b/clippy_lints/src/eta_reduction.rs index e0fa9e5e929b..425f1e671477 100644 --- a/clippy_lints/src/eta_reduction.rs +++ b/clippy_lints/src/eta_reduction.rs @@ -166,7 +166,7 @@ fn match_types(lhs: Ty<'_>, rhs: Ty<'_>) -> 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, &t2), + | (ty::Slice(t1), ty::Slice(t2)) => match_types(t1, t2), (ty::Adt(def1, _), ty::Adt(def2, _)) => def1 == def2, (_, _) => false, } From e63a685412e763a0eb04cf8a5daccdbe090d364e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Miku=C5=82a?= Date: Mon, 18 Mar 2019 11:59:09 +0100 Subject: [PATCH 13/20] NestedMetaItemKind -> NestedMetaItem --- clippy_lints/src/attrs.rs | 6 +++--- clippy_lints/src/utils/conf.rs | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/clippy_lints/src/attrs.rs b/clippy_lints/src/attrs.rs index 0c12bab957c3..8025832fdcc5 100644 --- a/clippy_lints/src/attrs.rs +++ b/clippy_lints/src/attrs.rs @@ -14,7 +14,7 @@ use rustc::ty::{self, TyCtxt}; use rustc::{declare_tool_lint, lint_array}; use rustc_errors::Applicability; use semver::Version; -use syntax::ast::{AttrStyle, Attribute, Lit, LitKind, MetaItemKind, NestedMetaItem, NestedMetaItemKind}; +use syntax::ast::{AttrStyle, Attribute, Lit, LitKind, MetaItemKind, NestedMetaItem}; use syntax::source_map::Span; declare_clippy_lint! { @@ -219,7 +219,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AttrPass { } for item in items { if_chain! { - if let NestedMetaItemKind::MetaItem(mi) = &item.node; + if let NestedMetaItem::MetaItem(mi) = &item; if let MetaItemKind::NameValue(lit) = &mi.node; if mi.name() == "since"; then { @@ -476,7 +476,7 @@ fn check_semver(cx: &LateContext<'_, '_>, span: Span, lit: &Lit) { } fn is_word(nmi: &NestedMetaItem, expected: &str) -> bool { - if let NestedMetaItemKind::MetaItem(mi) = &nmi.node { + if let NestedMetaItem::MetaItem(mi) = &nmi { mi.is_word() && mi.name() == expected } else { false diff --git a/clippy_lints/src/utils/conf.rs b/clippy_lints/src/utils/conf.rs index 64243203b7ff..ca860a5d30d8 100644 --- a/clippy_lints/src/utils/conf.rs +++ b/clippy_lints/src/utils/conf.rs @@ -12,9 +12,9 @@ use toml; /// Gets the configuration file from arguments. pub fn file_from_args( - args: &[source_map::Spanned], + args: &[ast::NestedMetaItem], ) -> Result, (&'static str, source_map::Span)> { - for arg in args.iter().filter_map(syntax::source_map::Spanned::meta_item) { + for arg in args.iter().filter_map(syntax::ast::NestedMetaItem::meta_item) { if arg.name() == "conf_file" { return match arg.node { ast::MetaItemKind::Word | ast::MetaItemKind::List(_) => { From b25564fc8abb5cac1738d19235efbb5ec2cc3a70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Miku=C5=82a?= Date: Mon, 18 Mar 2019 12:04:06 +0100 Subject: [PATCH 14/20] name -> ident_str --- clippy_lints/src/attrs.rs | 8 ++++++-- clippy_lints/src/missing_doc.rs | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/clippy_lints/src/attrs.rs b/clippy_lints/src/attrs.rs index 8025832fdcc5..66a1d72ec2d3 100644 --- a/clippy_lints/src/attrs.rs +++ b/clippy_lints/src/attrs.rs @@ -208,7 +208,8 @@ impl LintPass for AttrPass { impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AttrPass { fn check_attribute(&mut self, cx: &LateContext<'a, 'tcx>, attr: &'tcx Attribute) { if let Some(items) = &attr.meta_item_list() { - match &*attr.name().as_str() { + if let Some(ident) = attr.ident_str() { + match ident { "allow" | "warn" | "deny" | "forbid" => { check_clippy_lint_names(cx, items); }, @@ -229,6 +230,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AttrPass { } } } + } fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) { if is_relevant_item(cx.tcx, item) { @@ -240,7 +242,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AttrPass { for attr in &item.attrs { if let Some(lint_list) = &attr.meta_item_list() { - match &*attr.name().as_str() { + if let Some(ident) = attr.ident_str() { + match ident { "allow" | "warn" | "deny" | "forbid" => { // whitelist `unused_imports` and `deprecated` for `use` items // and `unused_imports` for `extern crate` items with `macro_use` @@ -288,6 +291,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AttrPass { } } } + } }, _ => {}, } diff --git a/clippy_lints/src/missing_doc.rs b/clippy_lints/src/missing_doc.rs index f5dab6d1feda..1cff51d19486 100644 --- a/clippy_lints/src/missing_doc.rs +++ b/clippy_lints/src/missing_doc.rs @@ -58,7 +58,7 @@ impl MissingDoc { if let Some(meta) = meta; if let MetaItemKind::List(list) = meta.node; if let Some(meta) = list.get(0); - if let Some(name) = meta.name(); + if let Some(name) = meta.ident_str(); then { name == "include" } else { From 4bb6c87b08aef5705644711b5c6bec831b158ffe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Miku=C5=82a?= Date: Mon, 18 Mar 2019 12:05:20 +0100 Subject: [PATCH 15/20] span -> span() --- clippy_lints/src/attrs.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/clippy_lints/src/attrs.rs b/clippy_lints/src/attrs.rs index 66a1d72ec2d3..3f5f1da424a5 100644 --- a/clippy_lints/src/attrs.rs +++ b/clippy_lints/src/attrs.rs @@ -224,7 +224,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AttrPass { if let MetaItemKind::NameValue(lit) = &mi.node; if mi.name() == "since"; then { - check_semver(cx, item.span, lit); + check_semver(cx, item.span(), lit); } } } @@ -327,7 +327,7 @@ fn check_clippy_lint_names(cx: &LateContext<'_, '_>, items: &[NestedMetaItem]) { span_lint_and_then( cx, UNKNOWN_CLIPPY_LINTS, - lint.span, + lint.span(), &format!("unknown clippy lint: clippy::{}", name), |db| { if name.as_str().chars().any(char::is_uppercase) { @@ -341,7 +341,7 @@ fn check_clippy_lint_names(cx: &LateContext<'_, '_>, items: &[NestedMetaItem]) { CheckLintNameResult::NoLint(None) => (), _ => { db.span_suggestion( - lint.span, + lint.span(), "lowercase the lint name", name_lower, Applicability::MaybeIncorrect, From 0ea5e38a9e188304e5ad7819139a9f2988483446 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Miku=C5=82a?= Date: Mon, 18 Mar 2019 12:15:23 +0100 Subject: [PATCH 16/20] name -> check_name --- clippy_lints/src/attrs.rs | 16 ++++++++-------- clippy_lints/src/doc.rs | 2 +- clippy_lints/src/inline_fn_without_body.rs | 2 +- clippy_lints/src/missing_doc.rs | 2 +- clippy_lints/src/missing_inline.rs | 2 +- clippy_lints/src/needless_pass_by_value.rs | 2 +- clippy_lints/src/returns.rs | 2 +- clippy_lints/src/trivially_copy_pass_by_ref.rs | 2 +- clippy_lints/src/utils/conf.rs | 2 +- 9 files changed, 16 insertions(+), 16 deletions(-) diff --git a/clippy_lints/src/attrs.rs b/clippy_lints/src/attrs.rs index 3f5f1da424a5..e7810152dc17 100644 --- a/clippy_lints/src/attrs.rs +++ b/clippy_lints/src/attrs.rs @@ -215,14 +215,14 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AttrPass { }, _ => {}, } - if items.is_empty() || attr.name() != "deprecated" { + if items.is_empty() || !attr.check_name("deprecated") { return; } for item in items { if_chain! { if let NestedMetaItem::MetaItem(mi) = &item; if let MetaItemKind::NameValue(lit) = &mi.node; - if mi.name() == "since"; + if mi.check_name("since"); then { check_semver(cx, item.span(), lit); } @@ -238,7 +238,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AttrPass { } match item.node { ItemKind::ExternCrate(..) | ItemKind::Use(..) => { - let skip_unused_imports = item.attrs.iter().any(|attr| attr.name() == "macro_use"); + let skip_unused_imports = item.attrs.iter().any(|attr| attr.check_name("macro_use")); for attr in &item.attrs { if let Some(lint_list) = &attr.meta_item_list() { @@ -447,7 +447,7 @@ fn check_attrs(cx: &LateContext<'_, '_>, span: Span, name: Name, attrs: &[Attrib } if let Some(values) = attr.meta_item_list() { - if values.len() != 1 || attr.name() != "inline" { + if values.len() != 1 || !attr.check_name("inline") { continue; } if is_word(&values[0], "always") { @@ -481,7 +481,7 @@ fn check_semver(cx: &LateContext<'_, '_>, span: Span, lit: &Lit) { fn is_word(nmi: &NestedMetaItem, expected: &str) -> bool { if let NestedMetaItem::MetaItem(mi) = &nmi { - mi.is_word() && mi.name() == expected + mi.is_word() && mi.check_name(expected) } else { false } @@ -518,15 +518,15 @@ impl EarlyLintPass for CfgAttrPass { fn check_attribute(&mut self, cx: &EarlyContext<'_>, attr: &Attribute) { if_chain! { // check cfg_attr - if attr.name() == "cfg_attr"; + if attr.check_name("cfg_attr"); if let Some(items) = attr.meta_item_list(); if items.len() == 2; // check for `rustfmt` if let Some(feature_item) = items[0].meta_item(); - if feature_item.name() == "rustfmt"; + if feature_item.check_name("rustfmt"); // check for `rustfmt_skip` and `rustfmt::skip` if let Some(skip_item) = &items[1].meta_item(); - if skip_item.name() == "rustfmt_skip" || skip_item.name() == "skip"; + if skip_item.check_name("rustfmt_skip") || skip_item.check_name("skip"); // Only lint outer attributes, because custom inner attributes are unstable // Tracking issue: /~https://github.com/rust-lang/rust/issues/54726 if let AttrStyle::Outer = attr.style; diff --git a/clippy_lints/src/doc.rs b/clippy_lints/src/doc.rs index 0e8d47384f1a..413645a091ff 100644 --- a/clippy_lints/src/doc.rs +++ b/clippy_lints/src/doc.rs @@ -152,7 +152,7 @@ pub fn check_attrs<'a>(cx: &EarlyContext<'_>, valid_idents: &FxHashSet, spans.extend_from_slice(¤t_spans); doc.push_str(¤t); } - } else if attr.name() == "doc" { + } else if attr.check_name("doc") { // ignore mix of sugared and non-sugared doc return; } diff --git a/clippy_lints/src/inline_fn_without_body.rs b/clippy_lints/src/inline_fn_without_body.rs index cced334f43a8..3e13d0073fe8 100644 --- a/clippy_lints/src/inline_fn_without_body.rs +++ b/clippy_lints/src/inline_fn_without_body.rs @@ -51,7 +51,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass { fn check_attrs(cx: &LateContext<'_, '_>, name: Name, attrs: &[Attribute]) { for attr in attrs { - if attr.name() != "inline" { + if !attr.check_name("inline") { continue; } diff --git a/clippy_lints/src/missing_doc.rs b/clippy_lints/src/missing_doc.rs index 1cff51d19486..ed984f16db5c 100644 --- a/clippy_lints/src/missing_doc.rs +++ b/clippy_lints/src/missing_doc.rs @@ -91,7 +91,7 @@ impl MissingDoc { let has_doc = attrs .iter() - .any(|a| a.name() == "doc" && (a.is_value_str() || Self::has_include(a.meta()))); + .any(|a| a.check_name("doc") && (a.is_value_str() || Self::has_include(a.meta()))); if !has_doc { span_lint( cx, diff --git a/clippy_lints/src/missing_inline.rs b/clippy_lints/src/missing_inline.rs index 87bf3b5485fb..d6711900093e 100644 --- a/clippy_lints/src/missing_inline.rs +++ b/clippy_lints/src/missing_inline.rs @@ -59,7 +59,7 @@ declare_clippy_lint! { pub struct MissingInline; fn check_missing_inline_attrs(cx: &LateContext<'_, '_>, attrs: &[ast::Attribute], sp: Span, desc: &'static str) { - let has_inline = attrs.iter().any(|a| a.name() == "inline"); + let has_inline = attrs.iter().any(|a| a.check_name("inline")); if !has_inline { span_lint( cx, diff --git a/clippy_lints/src/needless_pass_by_value.rs b/clippy_lints/src/needless_pass_by_value.rs index a07b8f4942b7..2300456d4c38 100644 --- a/clippy_lints/src/needless_pass_by_value.rs +++ b/clippy_lints/src/needless_pass_by_value.rs @@ -324,7 +324,7 @@ fn requires_exact_signature(attrs: &[Attribute]) -> bool { attrs.iter().any(|attr| { ["proc_macro", "proc_macro_attribute", "proc_macro_derive"] .iter() - .any(|&allow| attr.name() == allow) + .any(|&allow| attr.check_name(allow)) }) } diff --git a/clippy_lints/src/returns.rs b/clippy_lints/src/returns.rs index 5458fa51c7c0..3995d9f2819c 100644 --- a/clippy_lints/src/returns.rs +++ b/clippy_lints/src/returns.rs @@ -263,7 +263,7 @@ impl EarlyLintPass for ReturnPass { } fn attr_is_cfg(attr: &ast::Attribute) -> bool { - attr.meta_item_list().is_some() && attr.name() == "cfg" + attr.meta_item_list().is_some() && attr.check_name("cfg") } // get the def site diff --git a/clippy_lints/src/trivially_copy_pass_by_ref.rs b/clippy_lints/src/trivially_copy_pass_by_ref.rs index d246a7911662..e0f19a146541 100644 --- a/clippy_lints/src/trivially_copy_pass_by_ref.rs +++ b/clippy_lints/src/trivially_copy_pass_by_ref.rs @@ -176,7 +176,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TriviallyCopyPassByRef { return; } for a in attrs { - if a.meta_item_list().is_some() && a.name() == "proc_macro_derive" { + if a.meta_item_list().is_some() && a.check_name("proc_macro_derive") { return; } } diff --git a/clippy_lints/src/utils/conf.rs b/clippy_lints/src/utils/conf.rs index ca860a5d30d8..ce382da6e02b 100644 --- a/clippy_lints/src/utils/conf.rs +++ b/clippy_lints/src/utils/conf.rs @@ -15,7 +15,7 @@ pub fn file_from_args( args: &[ast::NestedMetaItem], ) -> Result, (&'static str, source_map::Span)> { for arg in args.iter().filter_map(syntax::ast::NestedMetaItem::meta_item) { - if arg.name() == "conf_file" { + if arg.check_name("conf_file") { return match arg.node { ast::MetaItemKind::Word | ast::MetaItemKind::List(_) => { Err(("`conf_file` must be a named value", arg.span)) From fb7699d9e5fc7bcf0c1cbebd0f847bce5ee89f88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Miku=C5=82a?= Date: Mon, 18 Mar 2019 12:29:09 +0100 Subject: [PATCH 17/20] Rework clippy detection in attribute lint --- clippy_lints/src/attrs.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/clippy_lints/src/attrs.rs b/clippy_lints/src/attrs.rs index e7810152dc17..b461b1364eff 100644 --- a/clippy_lints/src/attrs.rs +++ b/clippy_lints/src/attrs.rs @@ -315,10 +315,11 @@ fn check_clippy_lint_names(cx: &LateContext<'_, '_>, items: &[NestedMetaItem]) { let lint_store = cx.lints(); for lint in items { if_chain! { - if let Some(word) = lint.word(); - if let Some(tool_name) = word.is_scoped(); + if let Some(meta_item) = lint.meta_item(); + if meta_item.path.segments.len() > 1; + if let tool_name = meta_item.path.segments[0].ident; if tool_name.as_str() == "clippy"; - let name = word.name(); + let name = meta_item.path.segments.last().unwrap().ident.name; if let CheckLintNameResult::Tool(Err((None, _))) = lint_store.check_lint_name( &name.as_str(), Some(tool_name.as_str()), From 8feb2c6b2f7a6d32ce526678c068e7a5bfee4669 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Miku=C5=82a?= Date: Mon, 18 Mar 2019 12:29:56 +0100 Subject: [PATCH 18/20] Fix rustfmt::skip detection --- clippy_lints/src/attrs.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/clippy_lints/src/attrs.rs b/clippy_lints/src/attrs.rs index b461b1364eff..71e07b330a9f 100644 --- a/clippy_lints/src/attrs.rs +++ b/clippy_lints/src/attrs.rs @@ -527,7 +527,8 @@ impl EarlyLintPass for CfgAttrPass { if feature_item.check_name("rustfmt"); // check for `rustfmt_skip` and `rustfmt::skip` if let Some(skip_item) = &items[1].meta_item(); - if skip_item.check_name("rustfmt_skip") || skip_item.check_name("skip"); + if skip_item.check_name("rustfmt_skip") || + skip_item.path.segments.last().expect("empty path in attribute").ident.name == "skip"; // Only lint outer attributes, because custom inner attributes are unstable // Tracking issue: /~https://github.com/rust-lang/rust/issues/54726 if let AttrStyle::Outer = attr.style; From 664391c5f862c905a0753f7fe6de588981f10482 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Miku=C5=82a?= Date: Mon, 18 Mar 2019 12:30:22 +0100 Subject: [PATCH 19/20] Drop range_contains feature --- clippy_lints/src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 1943373eb4a3..bf98aa7e2b58 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -5,7 +5,6 @@ #![feature(rustc_private)] #![feature(slice_patterns)] #![feature(stmt_expr_attributes)] -#![feature(range_contains)] #![allow(clippy::missing_docs_in_private_items)] #![recursion_limit = "256"] #![warn(rust_2018_idioms, trivial_casts, trivial_numeric_casts)] From 6cb06056000dcf38cb6c50f7da1dd622c312a1d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Miku=C5=82a?= Date: Mon, 18 Mar 2019 12:31:49 +0100 Subject: [PATCH 20/20] Cargo fmt --- clippy_lints/src/attrs.rs | 112 ++++++++++++++++----------------- clippy_lints/src/utils/conf.rs | 4 +- clippy_lints/src/utils/mod.rs | 2 +- 3 files changed, 58 insertions(+), 60 deletions(-) diff --git a/clippy_lints/src/attrs.rs b/clippy_lints/src/attrs.rs index 71e07b330a9f..b4e4d46a33a6 100644 --- a/clippy_lints/src/attrs.rs +++ b/clippy_lints/src/attrs.rs @@ -210,27 +210,27 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AttrPass { if let Some(items) = &attr.meta_item_list() { if let Some(ident) = attr.ident_str() { match ident { - "allow" | "warn" | "deny" | "forbid" => { - check_clippy_lint_names(cx, items); - }, - _ => {}, - } + "allow" | "warn" | "deny" | "forbid" => { + check_clippy_lint_names(cx, items); + }, + _ => {}, + } if items.is_empty() || !attr.check_name("deprecated") { - return; - } - for item in items { - if_chain! { + return; + } + for item in items { + if_chain! { if let NestedMetaItem::MetaItem(mi) = &item; - if let MetaItemKind::NameValue(lit) = &mi.node; + if let MetaItemKind::NameValue(lit) = &mi.node; if mi.check_name("since"); - then { + then { check_semver(cx, item.span(), lit); + } } } } } } - } fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) { if is_relevant_item(cx.tcx, item) { @@ -244,54 +244,54 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AttrPass { if let Some(lint_list) = &attr.meta_item_list() { if let Some(ident) = attr.ident_str() { match ident { - "allow" | "warn" | "deny" | "forbid" => { - // whitelist `unused_imports` and `deprecated` for `use` items - // and `unused_imports` for `extern crate` items with `macro_use` - for lint in lint_list { - match item.node { - ItemKind::Use(..) => { - if is_word(lint, "unused_imports") || is_word(lint, "deprecated") { - return; - } - }, - ItemKind::ExternCrate(..) => { - if is_word(lint, "unused_imports") && skip_unused_imports { - return; - } - if is_word(lint, "unused_extern_crates") { - return; - } - }, - _ => {}, - } - } - let line_span = last_line_of_span(cx, attr.span); - - if let Some(mut sugg) = snippet_opt(cx, line_span) { - if sugg.contains("#[") { - span_lint_and_then( - cx, - USELESS_ATTRIBUTE, - line_span, - "useless lint attribute", - |db| { - sugg = sugg.replacen("#[", "#![", 1); - db.span_suggestion( - line_span, - "if you just forgot a `!`, use", - sugg, - Applicability::MachineApplicable, - ); + "allow" | "warn" | "deny" | "forbid" => { + // whitelist `unused_imports` and `deprecated` for `use` items + // and `unused_imports` for `extern crate` items with `macro_use` + for lint in lint_list { + match item.node { + ItemKind::Use(..) => { + if is_word(lint, "unused_imports") || is_word(lint, "deprecated") { + return; + } }, - ); + ItemKind::ExternCrate(..) => { + if is_word(lint, "unused_imports") && skip_unused_imports { + return; + } + if is_word(lint, "unused_extern_crates") { + return; + } + }, + _ => {}, + } } - } - }, - _ => {}, + let line_span = last_line_of_span(cx, attr.span); + + if let Some(mut sugg) = snippet_opt(cx, line_span) { + if sugg.contains("#[") { + span_lint_and_then( + cx, + USELESS_ATTRIBUTE, + line_span, + "useless lint attribute", + |db| { + sugg = sugg.replacen("#[", "#![", 1); + db.span_suggestion( + line_span, + "if you just forgot a `!`, use", + sugg, + Applicability::MachineApplicable, + ); + }, + ); + } + } + }, + _ => {}, + } } } } - } }, _ => {}, } @@ -527,7 +527,7 @@ impl EarlyLintPass for CfgAttrPass { if feature_item.check_name("rustfmt"); // check for `rustfmt_skip` and `rustfmt::skip` if let Some(skip_item) = &items[1].meta_item(); - if skip_item.check_name("rustfmt_skip") || + if skip_item.check_name("rustfmt_skip") || skip_item.path.segments.last().expect("empty path in attribute").ident.name == "skip"; // Only lint outer attributes, because custom inner attributes are unstable // Tracking issue: /~https://github.com/rust-lang/rust/issues/54726 diff --git a/clippy_lints/src/utils/conf.rs b/clippy_lints/src/utils/conf.rs index ce382da6e02b..2c96d9a8b5aa 100644 --- a/clippy_lints/src/utils/conf.rs +++ b/clippy_lints/src/utils/conf.rs @@ -11,9 +11,7 @@ use syntax::{ast, source_map}; use toml; /// Gets the configuration file from arguments. -pub fn file_from_args( - args: &[ast::NestedMetaItem], -) -> Result, (&'static str, source_map::Span)> { +pub fn file_from_args(args: &[ast::NestedMetaItem]) -> Result, (&'static str, source_map::Span)> { for arg in args.iter().filter_map(syntax::ast::NestedMetaItem::meta_item) { if arg.check_name("conf_file") { return match arg.node { diff --git a/clippy_lints/src/utils/mod.rs b/clippy_lints/src/utils/mod.rs index 586a33384e0b..2cf638b939cc 100644 --- a/clippy_lints/src/utils/mod.rs +++ b/clippy_lints/src/utils/mod.rs @@ -146,7 +146,7 @@ impl<'tcx> Printer<'tcx, 'tcx> for AbsolutePathPrinter<'_, 'tcx> { 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(),