From e3c4ffd4aa9a4ec39547c4a5a9715173d4a5f43d Mon Sep 17 00:00:00 2001 From: Andre Bogus Date: Sat, 7 Sep 2019 12:21:52 +0200 Subject: [PATCH] Use diagnostic item for --- clippy_lints/src/loops.rs | 8 ++++---- clippy_lints/src/methods/mod.rs | 19 +++++++++---------- clippy_lints/src/needless_pass_by_value.rs | 6 +++--- clippy_lints/src/ptr.rs | 7 ++++--- clippy_lints/src/swap.rs | 6 ++++-- clippy_lints/src/types.rs | 4 ++-- clippy_lints/src/utils/mod.rs | 8 ++++++++ 7 files changed, 34 insertions(+), 24 deletions(-) diff --git a/clippy_lints/src/loops.rs b/clippy_lints/src/loops.rs index 9d58d98db..faf6a55fb 100644 --- a/clippy_lints/src/loops.rs +++ b/clippy_lints/src/loops.rs @@ -11,7 +11,7 @@ use rustc::{declare_lint_pass, declare_tool_lint}; // use rustc::middle::region::CodeExtent; use crate::consts::{constant, Constant}; use crate::utils::usage::mutated_variables; -use crate::utils::{sext, sugg}; +use crate::utils::{sext, sugg, is_type_diagnostic_item}; use rustc::middle::expr_use_visitor::*; use rustc::middle::mem_categorization::cmt_; use rustc::middle::mem_categorization::Categorization; @@ -23,7 +23,7 @@ use std::iter::{once, Iterator}; use std::mem; use syntax::ast; use syntax::source_map::Span; -use syntax_pos::BytePos; +use syntax_pos::{BytePos, Symbol}; use crate::utils::paths; use crate::utils::{ @@ -795,7 +795,7 @@ fn is_slice_like<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, ty: Ty<'_>) -> bool { _ => false, }; - is_slice || match_type(cx, ty, &paths::VEC) || match_type(cx, ty, &paths::VEC_DEQUE) + is_slice || is_type_diagnostic_item(cx, ty, Symbol::intern("vec_type")) || match_type(cx, ty, &paths::VEC_DEQUE) } fn get_fixed_offset_var<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &Expr, var: HirId) -> Option { @@ -1950,7 +1950,7 @@ fn is_ref_iterable_type(cx: &LateContext<'_, '_>, e: &Expr) -> bool { // will allow further borrows afterwards let ty = cx.tables.expr_ty(e); is_iterable_array(ty, cx) || - match_type(cx, ty, &paths::VEC) || + is_type_diagnostic_item(cx, ty, Symbol::intern("vec_type")) || match_type(cx, ty, &paths::LINKED_LIST) || match_type(cx, ty, &paths::HASHMAP) || match_type(cx, ty, &paths::HASHSET) || diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index 5578dd265..a4904483c 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -16,18 +16,17 @@ use rustc::{declare_lint_pass, declare_tool_lint}; use rustc_errors::Applicability; use syntax::ast; use syntax::source_map::Span; -use syntax::symbol::{sym, LocalInternedString}; +use syntax::symbol::{sym, Symbol, LocalInternedString}; -use crate::utils::sugg; use crate::utils::usage::mutated_variables; use crate::utils::{ get_arg_name, get_parent_expr, get_trait_def_id, has_iter_method, implements_trait, in_macro, is_copy, - is_ctor_function, is_expn_of, iter_input_pats, last_path_segment, match_def_path, match_qpath, match_trait_method, - match_type, match_var, method_calls, method_chain_args, remove_blocks, return_ty, same_tys, single_segment_path, - snippet, snippet_with_applicability, snippet_with_macro_callsite, span_lint, span_lint_and_sugg, - span_lint_and_then, span_note_and_lint, walk_ptrs_ty, walk_ptrs_ty_depth, SpanlessEq, + is_ctor_function, is_expn_of, is_type_diagnostic_item, iter_input_pats, last_path_segment, match_def_path, + match_qpath, match_trait_method, match_type, match_var, method_calls, method_chain_args, remove_blocks, + return_ty, same_tys, single_segment_path, snippet, snippet_with_applicability, snippet_with_macro_callsite, + span_lint, span_lint_and_sugg, span_lint_and_then, span_note_and_lint, walk_ptrs_ty, walk_ptrs_ty_depth, + SpanlessEq, sugg, paths, span_help_and_lint }; -use crate::utils::{paths, span_help_and_lint}; declare_clippy_lint! { /// **What it does:** Checks for `.unwrap()` calls on `Option`s. @@ -1765,7 +1764,7 @@ fn lint_cstring_as_ptr(cx: &LateContext<'_, '_>, expr: &hir::Expr, source: &hir: fn lint_iter_cloned_collect<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &hir::Expr, iter_args: &'tcx [hir::Expr]) { if_chain! { - if match_type(cx, cx.tables.expr_ty(expr), &paths::VEC); + if is_type_diagnostic_item(cx, cx.tables.expr_ty(expr), Symbol::intern("vec_type")); if let Some(slice) = derefs_to_slice(cx, &iter_args[0], cx.tables.expr_ty(&iter_args[0])); if let Some(to_replace) = expr.span.trim_start(slice.span.source_callsite()); @@ -1875,7 +1874,7 @@ fn lint_iter_nth<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &hir::Expr, iter_ar let mut_str = if is_mut { "_mut" } else { "" }; let caller_type = if derefs_to_slice(cx, &iter_args[0], cx.tables.expr_ty(&iter_args[0])).is_some() { "slice" - } else if match_type(cx, cx.tables.expr_ty(&iter_args[0]), &paths::VEC) { + } else if is_type_diagnostic_item(cx, cx.tables.expr_ty(&iter_args[0]), Symbol::intern("vec_type")) { "Vec" } else if match_type(cx, cx.tables.expr_ty(&iter_args[0]), &paths::VEC_DEQUE) { "VecDeque" @@ -1908,7 +1907,7 @@ fn lint_get_unwrap<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &hir::Expr, get_a let caller_type = if derefs_to_slice(cx, &get_args[0], expr_ty).is_some() { needs_ref = get_args_str.parse::().is_ok(); "slice" - } else if match_type(cx, expr_ty, &paths::VEC) { + } else if is_type_diagnostic_item(cx, expr_ty, Symbol::intern("vec_type")) { needs_ref = get_args_str.parse::().is_ok(); "Vec" } else if match_type(cx, expr_ty, &paths::VEC_DEQUE) { diff --git a/clippy_lints/src/needless_pass_by_value.rs b/clippy_lints/src/needless_pass_by_value.rs index d3ca5cb53..5fa160e2c 100644 --- a/clippy_lints/src/needless_pass_by_value.rs +++ b/clippy_lints/src/needless_pass_by_value.rs @@ -1,7 +1,7 @@ use crate::utils::ptr::get_spans; use crate::utils::{ get_trait_def_id, implements_trait, is_copy, is_self, match_type, multispan_sugg, paths, snippet, snippet_opt, - span_lint_and_then, + span_lint_and_then, is_type_diagnostic_item, }; use if_chain::if_chain; use matches::matches; @@ -19,7 +19,7 @@ use rustc_target::spec::abi::Abi; use std::borrow::Cow; use syntax::ast::Attribute; use syntax::errors::DiagnosticBuilder; -use syntax_pos::Span; +use syntax_pos::{Span, Symbol}; declare_clippy_lint! { /// **What it does:** Checks for functions taking arguments by value, but not @@ -221,7 +221,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessPassByValue { let deref_span = spans_need_deref.get(&canonical_id); if_chain! { - if match_type(cx, ty, &paths::VEC); + if is_type_diagnostic_item(cx, ty, Symbol::intern("vec_type")); if let Some(clone_spans) = get_spans(cx, Some(body.id()), idx, &[("clone", ".to_owned()")]); if let TyKind::Path(QPath::Resolved(_, ref path)) = input.node; diff --git a/clippy_lints/src/ptr.rs b/clippy_lints/src/ptr.rs index 854bb718e..1d60460b5 100644 --- a/clippy_lints/src/ptr.rs +++ b/clippy_lints/src/ptr.rs @@ -1,7 +1,8 @@ //! Checks for usage of `&Vec[_]` and `&String`. use crate::utils::ptr::get_spans; -use crate::utils::{match_qpath, match_type, paths, snippet_opt, span_lint, span_lint_and_then, walk_ptrs_hir_ty}; +use crate::utils::{match_qpath, is_type_diagnostic_item, match_type, paths, snippet_opt, + span_lint, span_lint_and_then, walk_ptrs_hir_ty}; use if_chain::if_chain; use rustc::hir::QPath; use rustc::hir::*; @@ -11,7 +12,7 @@ use rustc::{declare_lint_pass, declare_tool_lint}; use rustc_errors::Applicability; use std::borrow::Cow; use syntax::source_map::Span; -use syntax_pos::MultiSpan; +use syntax_pos::{MultiSpan, Symbol}; declare_clippy_lint! { /// **What it does:** This lint checks for function arguments of type `&String` @@ -148,7 +149,7 @@ fn check_fn(cx: &LateContext<'_, '_>, decl: &FnDecl, fn_id: HirId, opt_body_id: for (idx, (arg, ty)) in decl.inputs.iter().zip(fn_ty.inputs()).enumerate() { if let ty::Ref(_, ty, MutImmutable) = ty.sty { - if match_type(cx, ty, &paths::VEC) { + if is_type_diagnostic_item(cx, ty, Symbol::intern("vec_type")) { let mut ty_snippet = None; if_chain! { if let TyKind::Path(QPath::Resolved(_, ref path)) = walk_ptrs_hir_ty(arg).node; diff --git a/clippy_lints/src/swap.rs b/clippy_lints/src/swap.rs index 80c9a33c0..a2c2a1c11 100644 --- a/clippy_lints/src/swap.rs +++ b/clippy_lints/src/swap.rs @@ -1,6 +1,7 @@ use crate::utils::sugg::Sugg; use crate::utils::{ - differing_macro_contexts, match_type, paths, snippet, span_lint_and_then, walk_ptrs_ty, SpanlessEq, + differing_macro_contexts, match_type, is_type_diagnostic_item, paths, snippet, + span_lint_and_then, walk_ptrs_ty, SpanlessEq, }; use if_chain::if_chain; use matches::matches; @@ -9,6 +10,7 @@ use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; use rustc::ty; use rustc::{declare_lint_pass, declare_tool_lint}; use rustc_errors::Applicability; +use syntax_pos::Symbol; declare_clippy_lint! { /// **What it does:** Checks for manual swapping. @@ -107,7 +109,7 @@ fn check_manual_swap(cx: &LateContext<'_, '_>, block: &Block) { if matches!(ty.sty, ty::Slice(_)) || matches!(ty.sty, ty::Array(_, _)) || - match_type(cx, ty, &paths::VEC) || + is_type_diagnostic_item(cx, ty, Symbol::intern("vec_type")) || match_type(cx, ty, &paths::VEC_DEQUE) { return Some((lhs1, idx1, idx2)); } diff --git a/clippy_lints/src/types.rs b/clippy_lints/src/types.rs index c210cf82a..2e4128ccc 100644 --- a/clippy_lints/src/types.rs +++ b/clippy_lints/src/types.rs @@ -18,7 +18,7 @@ use rustc_typeck::hir_ty_to_ty; use syntax::ast::{FloatTy, IntTy, LitIntType, LitKind, UintTy}; use syntax::errors::DiagnosticBuilder; use syntax::source_map::Span; -use syntax::symbol::sym; +use syntax::symbol::{sym, Symbol}; use crate::consts::{constant, Constant}; use crate::utils::paths; @@ -253,7 +253,7 @@ fn check_ty(cx: &LateContext<'_, '_>, hir_ty: &hir::Ty, is_local: bool) { ); return; // don't recurse into the type } - } else if match_def_path(cx, def_id, &paths::VEC) { + } else if cx.tcx.is_diagnostic_item(Symbol::intern("vec_type"), def_id) { if_chain! { // Get the _ part of Vec<_> if let Some(ref last) = last_path_segment(qpath).args; diff --git a/clippy_lints/src/utils/mod.rs b/clippy_lints/src/utils/mod.rs index ab78272fc..bcb443158 100644 --- a/clippy_lints/src/utils/mod.rs +++ b/clippy_lints/src/utils/mod.rs @@ -130,6 +130,14 @@ pub fn match_type(cx: &LateContext<'_, '_>, ty: Ty<'_>, path: &[&str]) -> bool { } } +/// Checks if the type is equal to a diagnostic item +pub fn is_type_diagnostic_item(cx: &LateContext<'_, '_>, ty: Ty<'_>, diag_item: Symbol) -> bool { + match ty.sty { + ty::Adt(adt, _) => cx.tcx.is_diagnostic_item(diag_item, adt.did), + _ => false, + } +} + /// Checks if the method call given in `expr` belongs to the given trait. pub fn match_trait_method(cx: &LateContext<'_, '_>, expr: &Expr, path: &[&str]) -> bool { let def_id = cx.tables.type_dependent_def_id(expr.hir_id).unwrap();