]> git.lizzy.rs Git - rust.git/commitdiff
Use diagnostic item for
authorAndre Bogus <bogusandre@gmail.com>
Sat, 7 Sep 2019 10:21:52 +0000 (12:21 +0200)
committerAndre Bogus <bogusandre@gmail.com>
Mon, 9 Sep 2019 03:43:39 +0000 (05:43 +0200)
clippy_lints/src/loops.rs
clippy_lints/src/methods/mod.rs
clippy_lints/src/needless_pass_by_value.rs
clippy_lints/src/ptr.rs
clippy_lints/src/swap.rs
clippy_lints/src/types.rs
clippy_lints/src/utils/mod.rs

index 9d58d98dbb1fb728c32323752d7049fb1fabb0fe..faf6a55fbb05f0c1505ee009ed757e61d98de619 100644 (file)
@@ -11,7 +11,7 @@
 // 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::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<FixedOffsetVar> {
@@ -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) ||
index 5578dd2654e299a3fc14bb55b433c3d117e21fa4..a4904483c24e9ff7adc1ed3df6d614f8ece29eff 100644 (file)
 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::<usize>().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::<usize>().is_ok();
         "Vec"
     } else if match_type(cx, expr_ty, &paths::VEC_DEQUE) {
index d3ca5cb539a1be08c58168b37af489bdbfdeca50..5fa160e2cd41be408e35750c58cf90dad128e79a 100644 (file)
@@ -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 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 @@ fn check_fn(
 
                         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;
index 854bb718e3efca947d5fd05d2fdf9bbd27c98ecd..1d60460b5a9fa1044d9a70916f9dff8aafa02283 100644 (file)
@@ -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_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;
index 80c9a33c06a807bdac2915c31a8a94350b9918db..a2c2a1c11860f35b8d8a81f2c261330fe1ac1e69 100644 (file)
@@ -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::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_for_slice<'a>(
 
                                 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));
                                 }
index c210cf82a300967aa0c70ddcbaf1d62513ff9e13..2e4128ccca632fed2e8d93cbbbc050007bd36161 100644 (file)
@@ -18,7 +18,7 @@
 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;
index ab78272fc3b84fd731a5441628eb36078ea0a48c..bcb443158397e2b36e7bc9b5f66c0a4e70541b33 100644 (file)
@@ -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();