Skip to content

Commit 2ac2188

Browse files
committed
Use diagnostic items for BinaryHeap, BTreeMap, BTreeSet, HashMap, HashSet, Borrow, Default
1 parent f07feca commit 2ac2188

File tree

12 files changed

+34
-31
lines changed

12 files changed

+34
-31
lines changed

clippy_lints/src/implicit_hasher.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@ use rustc_typeck::hir_ty_to_ty;
1919
use if_chain::if_chain;
2020

2121
use clippy_utils::diagnostics::{multispan_sugg, span_lint_and_then};
22-
use clippy_utils::paths;
22+
use clippy_utils::differing_macro_contexts;
2323
use clippy_utils::source::{snippet, snippet_opt};
2424
use clippy_utils::ty::is_type_diagnostic_item;
25-
use clippy_utils::{differing_macro_contexts, match_def_path};
2625

2726
declare_clippy_lint! {
2827
/// **What it does:** Checks for public `impl` or `fn` missing generalization
@@ -339,7 +338,7 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for ImplicitHasherConstructorVisitor<'a, 'b, 't
339338
return;
340339
}
341340

342-
if match_def_path(self.cx, ty_did, &paths::HASHMAP) {
341+
if self.cx.tcx.is_diagnostic_item(sym::hashmap_type, ty_did) {
343342
if method.ident.name == sym::new {
344343
self.suggestions
345344
.insert(e.span, "HashMap::default()".to_string());
@@ -352,7 +351,7 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for ImplicitHasherConstructorVisitor<'a, 'b, 't
352351
),
353352
);
354353
}
355-
} else if match_def_path(self.cx, ty_did, &paths::HASHSET) {
354+
} else if self.cx.tcx.is_diagnostic_item(sym::hashset_type, ty_did) {
356355
if method.ident.name == sym::new {
357356
self.suggestions
358357
.insert(e.span, "HashSet::default()".to_string());

clippy_lints/src/loops/explicit_iter_loop.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ fn is_ref_iterable_type(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
5959
is_type_diagnostic_item(cx, ty, sym::hashmap_type) ||
6060
is_type_diagnostic_item(cx, ty, sym::hashset_type) ||
6161
is_type_diagnostic_item(cx, ty, sym::vecdeque_type) ||
62-
match_type(cx, ty, &paths::BINARY_HEAP) ||
63-
match_type(cx, ty, &paths::BTREEMAP) ||
64-
match_type(cx, ty, &paths::BTREESET)
62+
is_type_diagnostic_item(cx, ty, sym::BinaryHeap) ||
63+
is_type_diagnostic_item(cx, ty, sym::BTreeMap) ||
64+
is_type_diagnostic_item(cx, ty, sym::BTreeSet)
6565
}
6666

6767
fn is_iterable_array<'tcx>(ty: Ty<'tcx>, cx: &LateContext<'tcx>) -> bool {

clippy_lints/src/loops/for_kv_map.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use super::FOR_KV_MAP;
22
use clippy_utils::diagnostics::{multispan_sugg, span_lint_and_then};
33
use clippy_utils::source::snippet;
4-
use clippy_utils::ty::{is_type_diagnostic_item, match_type};
4+
use clippy_utils::sugg;
5+
use clippy_utils::ty::is_type_diagnostic_item;
56
use clippy_utils::visitors::LocalUsedVisitor;
6-
use clippy_utils::{paths, sugg};
77
use rustc_hir::{BorrowKind, Expr, ExprKind, Mutability, Pat, PatKind};
88
use rustc_lint::LateContext;
99
use rustc_middle::ty;
@@ -39,7 +39,7 @@ pub(super) fn check<'tcx>(
3939
_ => arg,
4040
};
4141

42-
if is_type_diagnostic_item(cx, ty, sym::hashmap_type) || match_type(cx, ty, &paths::BTREEMAP) {
42+
if is_type_diagnostic_item(cx, ty, sym::hashmap_type) || is_type_diagnostic_item(cx, ty, sym::BTreeMap) {
4343
span_lint_and_then(
4444
cx,
4545
FOR_KV_MAP,

clippy_lints/src/matches.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1793,7 +1793,7 @@ mod redundant_pattern_match {
17931793
|| is_type_diagnostic_item(cx, ty, sym::Rc)
17941794
|| is_type_diagnostic_item(cx, ty, sym::Arc)
17951795
|| is_type_diagnostic_item(cx, ty, sym::cstring_type)
1796-
|| match_type(cx, ty, &paths::BTREEMAP)
1796+
|| is_type_diagnostic_item(cx, ty, sym::BTreeMap)
17971797
|| match_type(cx, ty, &paths::LINKED_LIST)
17981798
|| match_type(cx, ty, &paths::WEAK_RC)
17991799
|| match_type(cx, ty, &paths::WEAK_ARC)

clippy_lints/src/methods/get_unwrap.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use super::utils::derefs_to_slice;
22
use clippy_utils::diagnostics::span_lint_and_sugg;
3+
use clippy_utils::get_parent_expr;
34
use clippy_utils::source::snippet_with_applicability;
4-
use clippy_utils::ty::{is_type_diagnostic_item, match_type};
5-
use clippy_utils::{get_parent_expr, paths};
5+
use clippy_utils::ty::is_type_diagnostic_item;
66
use if_chain::if_chain;
77
use rustc_errors::Applicability;
88
use rustc_hir as hir;
@@ -36,7 +36,7 @@ pub(super) fn check<'tcx>(
3636
} else if !is_mut && is_type_diagnostic_item(cx, expr_ty, sym::hashmap_type) {
3737
needs_ref = true;
3838
"HashMap"
39-
} else if !is_mut && match_type(cx, expr_ty, &paths::BTREEMAP) {
39+
} else if !is_mut && is_type_diagnostic_item(cx, expr_ty, sym::BTreeMap) {
4040
needs_ref = true;
4141
"BTreeMap"
4242
} else {

clippy_lints/src/methods/iter_count.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ pub(crate) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, recv: &'tcx E
2222
"HashSet"
2323
} else if is_type_diagnostic_item(cx, ty, sym::hashmap_type) {
2424
"HashMap"
25-
} else if match_type(cx, ty, &paths::BTREEMAP) {
25+
} else if is_type_diagnostic_item(cx, ty, sym::BTreeMap) {
2626
"BTreeMap"
27-
} else if match_type(cx, ty, &paths::BTREESET) {
27+
} else if is_type_diagnostic_item(cx, ty, sym::BTreeSet) {
2828
"BTreeSet"
2929
} else if match_type(cx, ty, &paths::LINKED_LIST) {
3030
"LinkedList"
31-
} else if match_type(cx, ty, &paths::BINARY_HEAP) {
31+
} else if is_type_diagnostic_item(cx, ty, sym::BinaryHeap) {
3232
"BinaryHeap"
3333
} else {
3434
return;

clippy_lints/src/methods/or_fun_call.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::eager_or_lazy::is_lazyness_candidate;
33
use clippy_utils::source::{snippet, snippet_with_applicability, snippet_with_macro_callsite};
44
use clippy_utils::ty::{implements_trait, is_type_diagnostic_item, match_type};
5-
use clippy_utils::{contains_return, get_trait_def_id, last_path_segment, paths};
5+
use clippy_utils::{contains_return, last_path_segment, paths};
66
use if_chain::if_chain;
77
use rustc_errors::Applicability;
88
use rustc_hir as hir;
@@ -41,7 +41,7 @@ pub(super) fn check<'tcx>(
4141
let path = last_path_segment(qpath).ident.name;
4242
if matches!(path, kw::Default | sym::new);
4343
let arg_ty = cx.typeck_results().expr_ty(arg);
44-
if let Some(default_trait_id) = get_trait_def_id(cx, &paths::DEFAULT_TRAIT);
44+
if let Some(default_trait_id) = cx.tcx.get_diagnostic_item(sym::Default);
4545
if implements_trait(cx, arg_ty, default_trait_id, &[]);
4646

4747
then {

clippy_lints/src/mut_key.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use clippy_utils::diagnostics::span_lint;
2-
use clippy_utils::{match_def_path, paths, trait_ref_of_method};
2+
use clippy_utils::trait_ref_of_method;
33
use rustc_hir as hir;
44
use rustc_lint::{LateContext, LateLintPass};
55
use rustc_middle::ty::TypeFoldable;
66
use rustc_middle::ty::{Adt, Array, RawPtr, Ref, Slice, Tuple, Ty, TypeAndMut};
77
use rustc_session::{declare_lint_pass, declare_tool_lint};
88
use rustc_span::source_map::Span;
9+
use rustc_span::symbol::sym;
910
use std::iter;
1011

1112
declare_clippy_lint! {
@@ -99,9 +100,9 @@ fn check_sig<'tcx>(cx: &LateContext<'tcx>, item_hir_id: hir::HirId, decl: &hir::
99100
fn check_ty<'tcx>(cx: &LateContext<'tcx>, span: Span, ty: Ty<'tcx>) {
100101
let ty = ty.peel_refs();
101102
if let Adt(def, substs) = ty.kind() {
102-
if [&paths::HASHMAP, &paths::BTREEMAP, &paths::HASHSET, &paths::BTREESET]
103+
if [sym::hashmap_type, sym::BTreeMap, sym::hashset_type, sym::BTreeMap]
103104
.iter()
104-
.any(|path| match_def_path(cx, def.did, &**path))
105+
.any(|diag_item| cx.tcx.is_diagnostic_item(*diag_item, def.did))
105106
&& is_mutable_type(cx, substs.type_at(0), span)
106107
{
107108
span_lint(cx, MUTABLE_KEY_TYPE, span, "mutable key type");

clippy_lints/src/needless_pass_by_value.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
103103
}
104104

105105
// Allow `Borrow` or functions to be taken by value
106-
let borrow_trait = need!(get_trait_def_id(cx, &paths::BORROW_TRAIT));
107106
let allowed_traits = [
108107
need!(cx.tcx.lang_items().fn_trait()),
109108
need!(cx.tcx.lang_items().fn_once_trait()),
@@ -167,7 +166,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
167166
let preds = preds.iter().filter(|t| t.self_ty() == ty).collect::<Vec<_>>();
168167

169168
(
170-
preds.iter().any(|t| t.def_id() == borrow_trait),
169+
preds.iter().any(|t| cx.tcx.is_diagnostic_item(sym::Borrow, t.def_id())),
171170
!preds.is_empty() && {
172171
let ty_empty_region = cx.tcx.mk_imm_ref(cx.tcx.lifetimes.re_root_empty, ty);
173172
preds.iter().all(|t| {

clippy_lints/src/new_without_default.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_hir_and_then;
2-
use clippy_utils::paths;
2+
use clippy_utils::return_ty;
33
use clippy_utils::source::snippet;
44
use clippy_utils::sugg::DiagnosticBuilderExt;
5-
use clippy_utils::{get_trait_def_id, return_ty};
65
use if_chain::if_chain;
76
use rustc_errors::Applicability;
87
use rustc_hir as hir;
@@ -104,7 +103,7 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
104103
let self_def_id = cx.tcx.hir().local_def_id(cx.tcx.hir().get_parent_item(id));
105104
let self_ty = cx.tcx.type_of(self_def_id);
106105
if TyS::same_type(self_ty, return_ty(cx, id));
107-
if let Some(default_trait_id) = get_trait_def_id(cx, &paths::DEFAULT_TRAIT);
106+
if let Some(default_trait_id) = cx.tcx.get_diagnostic_item(sym::Default);
108107
then {
109108
if self.impling_types.is_none() {
110109
let mut impls = HirIdSet::default();

clippy_lints/src/zero_sized_map_values.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
2-
use clippy_utils::paths;
3-
use clippy_utils::ty::{is_normalizable, is_type_diagnostic_item, match_type};
2+
use clippy_utils::ty::{is_normalizable, is_type_diagnostic_item};
43
use if_chain::if_chain;
54
use rustc_hir::{self as hir, HirId, ItemKind, Node};
65
use rustc_lint::{LateContext, LateLintPass};
@@ -49,7 +48,7 @@ impl LateLintPass<'_> for ZeroSizedMapValues {
4948
if !hir_ty.span.from_expansion();
5049
if !in_trait_impl(cx, hir_ty.hir_id);
5150
let ty = ty_from_hir_ty(cx, hir_ty);
52-
if is_type_diagnostic_item(cx, ty, sym::hashmap_type) || match_type(cx, ty, &paths::BTREEMAP);
51+
if is_type_diagnostic_item(cx, ty, sym::hashmap_type) || is_type_diagnostic_item(cx, ty, sym::BTreeMap);
5352
if let Adt(_, substs) = ty.kind();
5453
let ty = substs.type_at(1);
5554
// Do this to prevent `layout_of` crashing, being unable to fully normalize `ty`.

clippy_utils/src/paths.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,25 @@ pub const ASMUT_TRAIT: [&str; 3] = ["core", "convert", "AsMut"];
2121
pub const ASREF_TRAIT: [&str; 3] = ["core", "convert", "AsRef"];
2222
pub(super) const BEGIN_PANIC: [&str; 3] = ["std", "panicking", "begin_panic"];
2323
pub(super) const BEGIN_PANIC_FMT: [&str; 3] = ["std", "panicking", "begin_panic_fmt"];
24+
/// Preferably use the diagnostic item `sym::BinaryHeap` where possible
2425
pub const BINARY_HEAP: [&str; 4] = ["alloc", "collections", "binary_heap", "BinaryHeap"];
26+
/// Preferably use the diagnostic item `sym::Borrow` where possible
2527
pub const BORROW_TRAIT: [&str; 3] = ["core", "borrow", "Borrow"];
28+
/// Preferably use the diagnostic item `sym::BTreeMap` where possible
2629
pub const BTREEMAP: [&str; 5] = ["alloc", "collections", "btree", "map", "BTreeMap"];
2730
pub const BTREEMAP_CONTAINS_KEY: [&str; 6] = ["alloc", "collections", "btree", "map", "BTreeMap", "contains_key"];
2831
pub const BTREEMAP_ENTRY: [&str; 6] = ["alloc", "collections", "btree", "map", "entry", "Entry"];
2932
pub const BTREEMAP_INSERT: [&str; 6] = ["alloc", "collections", "btree", "map", "BTreeMap", "insert"];
33+
/// Preferably use the diagnostic item `sym::BTreeSet` where possible
3034
pub const BTREESET: [&str; 5] = ["alloc", "collections", "btree", "set", "BTreeSet"];
3135
pub const CLONE_TRAIT_METHOD: [&str; 4] = ["core", "clone", "Clone", "clone"];
3236
pub const CMP_MAX: [&str; 3] = ["core", "cmp", "max"];
3337
pub const CMP_MIN: [&str; 3] = ["core", "cmp", "min"];
3438
pub const COW: [&str; 3] = ["alloc", "borrow", "Cow"];
3539
pub const CSTRING_AS_C_STR: [&str; 5] = ["std", "ffi", "c_str", "CString", "as_c_str"];
36-
pub const DEFAULT_TRAIT: [&str; 3] = ["core", "default", "Default"];
3740
pub const DEFAULT_TRAIT_METHOD: [&str; 4] = ["core", "default", "Default", "default"];
3841
pub const DEREF_MUT_TRAIT_METHOD: [&str; 5] = ["core", "ops", "deref", "DerefMut", "deref_mut"];
42+
/// Preferably use the diagnostic item `sym::deref_method` where possible
3943
pub const DEREF_TRAIT_METHOD: [&str; 5] = ["core", "ops", "deref", "Deref", "deref"];
4044
pub const DIR_BUILDER: [&str; 3] = ["std", "fs", "DirBuilder"];
4145
pub const DISPLAY_TRAIT: [&str; 3] = ["core", "fmt", "Display"];
@@ -55,10 +59,12 @@ pub const FROM_ITERATOR_METHOD: [&str; 6] = ["core", "iter", "traits", "collect"
5559
pub const FROM_STR_METHOD: [&str; 5] = ["core", "str", "traits", "FromStr", "from_str"];
5660
pub const FUTURE_FROM_GENERATOR: [&str; 3] = ["core", "future", "from_generator"];
5761
pub const HASH: [&str; 3] = ["core", "hash", "Hash"];
62+
/// Preferably use the diagnostic item `sym::hashmap_type` where possible
5863
pub const HASHMAP: [&str; 5] = ["std", "collections", "hash", "map", "HashMap"];
5964
pub const HASHMAP_CONTAINS_KEY: [&str; 6] = ["std", "collections", "hash", "map", "HashMap", "contains_key"];
6065
pub const HASHMAP_ENTRY: [&str; 5] = ["std", "collections", "hash", "map", "Entry"];
6166
pub const HASHMAP_INSERT: [&str; 6] = ["std", "collections", "hash", "map", "HashMap", "insert"];
67+
/// Preferably use the diagnostic item `sym::hashset_type` where possible
6268
pub const HASHSET: [&str; 5] = ["std", "collections", "hash", "set", "HashSet"];
6369
#[cfg(feature = "internal-lints")]
6470
pub const IDENT: [&str; 3] = ["rustc_span", "symbol", "Ident"];

0 commit comments

Comments
 (0)