// 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;
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::{
_ => 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> {
// 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) ||
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.
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());
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"
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) {
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;
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
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;
//! 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::*;
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`
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;
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;
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.
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));
}
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;
);
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;
}
}
+/// 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();