Skip to content

Commit 58e01ea

Browse files
committed
Auto merge of #4519 - rust-lang:vec-diagnostic-item, r=@oli-obk
Use diagnostic item for `Vec` This adds a new `is_type_diagnostic_item` to check items without lookup via `match_type`. changelog: none
2 parents d4a48ed + e3c4ffd commit 58e01ea

File tree

7 files changed

+34
-24
lines changed

7 files changed

+34
-24
lines changed

clippy_lints/src/loops.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc::{declare_lint_pass, declare_tool_lint};
1111
// use rustc::middle::region::CodeExtent;
1212
use crate::consts::{constant, Constant};
1313
use crate::utils::usage::mutated_variables;
14-
use crate::utils::{sext, sugg};
14+
use crate::utils::{sext, sugg, is_type_diagnostic_item};
1515
use rustc::middle::expr_use_visitor::*;
1616
use rustc::middle::mem_categorization::cmt_;
1717
use rustc::middle::mem_categorization::Categorization;
@@ -23,7 +23,7 @@ use std::iter::{once, Iterator};
2323
use std::mem;
2424
use syntax::ast;
2525
use syntax::source_map::Span;
26-
use syntax_pos::BytePos;
26+
use syntax_pos::{BytePos, Symbol};
2727

2828
use crate::utils::paths;
2929
use crate::utils::{
@@ -795,7 +795,7 @@ fn is_slice_like<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, ty: Ty<'_>) -> bool {
795795
_ => false,
796796
};
797797

798-
is_slice || match_type(cx, ty, &paths::VEC) || match_type(cx, ty, &paths::VEC_DEQUE)
798+
is_slice || is_type_diagnostic_item(cx, ty, Symbol::intern("vec_type")) || match_type(cx, ty, &paths::VEC_DEQUE)
799799
}
800800

801801
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 {
19501950
// will allow further borrows afterwards
19511951
let ty = cx.tables.expr_ty(e);
19521952
is_iterable_array(ty, cx) ||
1953-
match_type(cx, ty, &paths::VEC) ||
1953+
is_type_diagnostic_item(cx, ty, Symbol::intern("vec_type")) ||
19541954
match_type(cx, ty, &paths::LINKED_LIST) ||
19551955
match_type(cx, ty, &paths::HASHMAP) ||
19561956
match_type(cx, ty, &paths::HASHSET) ||

clippy_lints/src/methods/mod.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,17 @@ use rustc::{declare_lint_pass, declare_tool_lint};
1616
use rustc_errors::Applicability;
1717
use syntax::ast;
1818
use syntax::source_map::Span;
19-
use syntax::symbol::{sym, LocalInternedString};
19+
use syntax::symbol::{sym, Symbol, LocalInternedString};
2020

21-
use crate::utils::sugg;
2221
use crate::utils::usage::mutated_variables;
2322
use crate::utils::{
2423
get_arg_name, get_parent_expr, get_trait_def_id, has_iter_method, implements_trait, in_macro, is_copy,
25-
is_ctor_function, is_expn_of, iter_input_pats, last_path_segment, match_def_path, match_qpath, match_trait_method,
26-
match_type, match_var, method_calls, method_chain_args, remove_blocks, return_ty, same_tys, single_segment_path,
27-
snippet, snippet_with_applicability, snippet_with_macro_callsite, span_lint, span_lint_and_sugg,
28-
span_lint_and_then, span_note_and_lint, walk_ptrs_ty, walk_ptrs_ty_depth, SpanlessEq,
24+
is_ctor_function, is_expn_of, is_type_diagnostic_item, iter_input_pats, last_path_segment, match_def_path,
25+
match_qpath, match_trait_method, match_type, match_var, method_calls, method_chain_args, remove_blocks,
26+
return_ty, same_tys, single_segment_path, snippet, snippet_with_applicability, snippet_with_macro_callsite,
27+
span_lint, span_lint_and_sugg, span_lint_and_then, span_note_and_lint, walk_ptrs_ty, walk_ptrs_ty_depth,
28+
SpanlessEq, sugg, paths, span_help_and_lint
2929
};
30-
use crate::utils::{paths, span_help_and_lint};
3130

3231
declare_clippy_lint! {
3332
/// **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:
17651764

17661765
fn lint_iter_cloned_collect<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &hir::Expr, iter_args: &'tcx [hir::Expr]) {
17671766
if_chain! {
1768-
if match_type(cx, cx.tables.expr_ty(expr), &paths::VEC);
1767+
if is_type_diagnostic_item(cx, cx.tables.expr_ty(expr), Symbol::intern("vec_type"));
17691768
if let Some(slice) = derefs_to_slice(cx, &iter_args[0], cx.tables.expr_ty(&iter_args[0]));
17701769
if let Some(to_replace) = expr.span.trim_start(slice.span.source_callsite());
17711770

@@ -1875,7 +1874,7 @@ fn lint_iter_nth<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &hir::Expr, iter_ar
18751874
let mut_str = if is_mut { "_mut" } else { "" };
18761875
let caller_type = if derefs_to_slice(cx, &iter_args[0], cx.tables.expr_ty(&iter_args[0])).is_some() {
18771876
"slice"
1878-
} else if match_type(cx, cx.tables.expr_ty(&iter_args[0]), &paths::VEC) {
1877+
} else if is_type_diagnostic_item(cx, cx.tables.expr_ty(&iter_args[0]), Symbol::intern("vec_type")) {
18791878
"Vec"
18801879
} else if match_type(cx, cx.tables.expr_ty(&iter_args[0]), &paths::VEC_DEQUE) {
18811880
"VecDeque"
@@ -1908,7 +1907,7 @@ fn lint_get_unwrap<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &hir::Expr, get_a
19081907
let caller_type = if derefs_to_slice(cx, &get_args[0], expr_ty).is_some() {
19091908
needs_ref = get_args_str.parse::<usize>().is_ok();
19101909
"slice"
1911-
} else if match_type(cx, expr_ty, &paths::VEC) {
1910+
} else if is_type_diagnostic_item(cx, expr_ty, Symbol::intern("vec_type")) {
19121911
needs_ref = get_args_str.parse::<usize>().is_ok();
19131912
"Vec"
19141913
} else if match_type(cx, expr_ty, &paths::VEC_DEQUE) {

clippy_lints/src/needless_pass_by_value.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::utils::ptr::get_spans;
22
use crate::utils::{
33
get_trait_def_id, implements_trait, is_copy, is_self, match_type, multispan_sugg, paths, snippet, snippet_opt,
4-
span_lint_and_then,
4+
span_lint_and_then, is_type_diagnostic_item,
55
};
66
use if_chain::if_chain;
77
use matches::matches;
@@ -19,7 +19,7 @@ use rustc_target::spec::abi::Abi;
1919
use std::borrow::Cow;
2020
use syntax::ast::Attribute;
2121
use syntax::errors::DiagnosticBuilder;
22-
use syntax_pos::Span;
22+
use syntax_pos::{Span, Symbol};
2323

2424
declare_clippy_lint! {
2525
/// **What it does:** Checks for functions taking arguments by value, but not
@@ -221,7 +221,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessPassByValue {
221221

222222
let deref_span = spans_need_deref.get(&canonical_id);
223223
if_chain! {
224-
if match_type(cx, ty, &paths::VEC);
224+
if is_type_diagnostic_item(cx, ty, Symbol::intern("vec_type"));
225225
if let Some(clone_spans) =
226226
get_spans(cx, Some(body.id()), idx, &[("clone", ".to_owned()")]);
227227
if let TyKind::Path(QPath::Resolved(_, ref path)) = input.node;

clippy_lints/src/ptr.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
//! Checks for usage of `&Vec[_]` and `&String`.
22
33
use crate::utils::ptr::get_spans;
4-
use crate::utils::{match_qpath, match_type, paths, snippet_opt, span_lint, span_lint_and_then, walk_ptrs_hir_ty};
4+
use crate::utils::{match_qpath, is_type_diagnostic_item, match_type, paths, snippet_opt,
5+
span_lint, span_lint_and_then, walk_ptrs_hir_ty};
56
use if_chain::if_chain;
67
use rustc::hir::QPath;
78
use rustc::hir::*;
@@ -11,7 +12,7 @@ use rustc::{declare_lint_pass, declare_tool_lint};
1112
use rustc_errors::Applicability;
1213
use std::borrow::Cow;
1314
use syntax::source_map::Span;
14-
use syntax_pos::MultiSpan;
15+
use syntax_pos::{MultiSpan, Symbol};
1516

1617
declare_clippy_lint! {
1718
/// **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:
148149

149150
for (idx, (arg, ty)) in decl.inputs.iter().zip(fn_ty.inputs()).enumerate() {
150151
if let ty::Ref(_, ty, MutImmutable) = ty.sty {
151-
if match_type(cx, ty, &paths::VEC) {
152+
if is_type_diagnostic_item(cx, ty, Symbol::intern("vec_type")) {
152153
let mut ty_snippet = None;
153154
if_chain! {
154155
if let TyKind::Path(QPath::Resolved(_, ref path)) = walk_ptrs_hir_ty(arg).node;

clippy_lints/src/swap.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::utils::sugg::Sugg;
22
use crate::utils::{
3-
differing_macro_contexts, match_type, paths, snippet, span_lint_and_then, walk_ptrs_ty, SpanlessEq,
3+
differing_macro_contexts, match_type, is_type_diagnostic_item, paths, snippet,
4+
span_lint_and_then, walk_ptrs_ty, SpanlessEq,
45
};
56
use if_chain::if_chain;
67
use matches::matches;
@@ -9,6 +10,7 @@ use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
910
use rustc::ty;
1011
use rustc::{declare_lint_pass, declare_tool_lint};
1112
use rustc_errors::Applicability;
13+
use syntax_pos::Symbol;
1214

1315
declare_clippy_lint! {
1416
/// **What it does:** Checks for manual swapping.
@@ -107,7 +109,7 @@ fn check_manual_swap(cx: &LateContext<'_, '_>, block: &Block) {
107109

108110
if matches!(ty.sty, ty::Slice(_)) ||
109111
matches!(ty.sty, ty::Array(_, _)) ||
110-
match_type(cx, ty, &paths::VEC) ||
112+
is_type_diagnostic_item(cx, ty, Symbol::intern("vec_type")) ||
111113
match_type(cx, ty, &paths::VEC_DEQUE) {
112114
return Some((lhs1, idx1, idx2));
113115
}

clippy_lints/src/types.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_typeck::hir_ty_to_ty;
1818
use syntax::ast::{FloatTy, IntTy, LitIntType, LitKind, UintTy};
1919
use syntax::errors::DiagnosticBuilder;
2020
use syntax::source_map::Span;
21-
use syntax::symbol::sym;
21+
use syntax::symbol::{sym, Symbol};
2222

2323
use crate::consts::{constant, Constant};
2424
use crate::utils::paths;
@@ -253,7 +253,7 @@ fn check_ty(cx: &LateContext<'_, '_>, hir_ty: &hir::Ty, is_local: bool) {
253253
);
254254
return; // don't recurse into the type
255255
}
256-
} else if match_def_path(cx, def_id, &paths::VEC) {
256+
} else if cx.tcx.is_diagnostic_item(Symbol::intern("vec_type"), def_id) {
257257
if_chain! {
258258
// Get the _ part of Vec<_>
259259
if let Some(ref last) = last_path_segment(qpath).args;

clippy_lints/src/utils/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@ pub fn match_type(cx: &LateContext<'_, '_>, ty: Ty<'_>, path: &[&str]) -> bool {
130130
}
131131
}
132132

133+
/// Checks if the type is equal to a diagnostic item
134+
pub fn is_type_diagnostic_item(cx: &LateContext<'_, '_>, ty: Ty<'_>, diag_item: Symbol) -> bool {
135+
match ty.sty {
136+
ty::Adt(adt, _) => cx.tcx.is_diagnostic_item(diag_item, adt.did),
137+
_ => false,
138+
}
139+
}
140+
133141
/// Checks if the method call given in `expr` belongs to the given trait.
134142
pub fn match_trait_method(cx: &LateContext<'_, '_>, expr: &Expr, path: &[&str]) -> bool {
135143
let def_id = cx.tables.type_dependent_def_id(expr.hir_id).unwrap();

0 commit comments

Comments
 (0)