Skip to content

Commit ceb7a86

Browse files
committed
Auto merge of #7466 - xFrednet:5393-use-more-diagnostic-items, r=flip1995
Use diagnostic items where possible Clippy still uses a bunch of paths in places that could easily use already defined diagnostic items. This PR updates all references to such paths and also removes a bunch of them that are no longer needed after this cleanup. Some paths are also used to construct new paths and can therefore not be removed that easily. I've added a doc comment to those instances that recommends the use of the diagnostic item where possible. And that's it, cleaning crew signing off 🧹 🗑️ --- changelog: none (only internal improvements) cc: #5393
2 parents fd2b43d + ecf85f4 commit ceb7a86

25 files changed

+109
-98
lines changed

clippy_lints/src/from_over_into.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
2-
use clippy_utils::paths::INTO;
3-
use clippy_utils::{match_def_path, meets_msrv, msrvs};
2+
use clippy_utils::{meets_msrv, msrvs};
43
use if_chain::if_chain;
54
use rustc_hir as hir;
65
use rustc_lint::{LateContext, LateLintPass, LintContext};
76
use rustc_semver::RustcVersion;
87
use rustc_session::{declare_tool_lint, impl_lint_pass};
8+
use rustc_span::symbol::sym;
99

1010
declare_clippy_lint! {
1111
/// **What it does:** Searches for implementations of the `Into<..>` trait and suggests to implement `From<..>` instead.
@@ -62,7 +62,7 @@ impl LateLintPass<'_> for FromOverInto {
6262
if_chain! {
6363
if let hir::ItemKind::Impl{ .. } = &item.kind;
6464
if let Some(impl_trait_ref) = cx.tcx.impl_trait_ref(item.def_id);
65-
if match_def_path(cx, impl_trait_ref.def_id, &INTO);
65+
if cx.tcx.is_diagnostic_item(sym::into_trait, impl_trait_ref.def_id);
6666

6767
then {
6868
span_lint_and_help(

clippy_lints/src/implicit_hasher.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@ use rustc_typeck::hir_ty_to_ty;
1717
use if_chain::if_chain;
1818

1919
use clippy_utils::diagnostics::{multispan_sugg, span_lint_and_then};
20-
use clippy_utils::paths;
20+
use clippy_utils::differing_macro_contexts;
2121
use clippy_utils::source::{snippet, snippet_opt};
2222
use clippy_utils::ty::is_type_diagnostic_item;
23-
use clippy_utils::{differing_macro_contexts, match_def_path};
2423

2524
declare_clippy_lint! {
2625
/// **What it does:** Checks for public `impl` or `fn` missing generalization
@@ -337,7 +336,7 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for ImplicitHasherConstructorVisitor<'a, 'b, 't
337336
return;
338337
}
339338

340-
if match_def_path(self.cx, ty_did, &paths::HASHMAP) {
339+
if self.cx.tcx.is_diagnostic_item(sym::hashmap_type, ty_did) {
341340
if method.ident.name == sym::new {
342341
self.suggestions
343342
.insert(e.span, "HashMap::default()".to_string());
@@ -350,7 +349,7 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for ImplicitHasherConstructorVisitor<'a, 'b, 't
350349
),
351350
);
352351
}
353-
} else if match_def_path(self.cx, ty_did, &paths::HASHSET) {
352+
} else if self.cx.tcx.is_diagnostic_item(sym::hashset_type, ty_did) {
354353
if method.ident.name == sym::new {
355354
self.suggestions
356355
.insert(e.span, "HashSet::default()".to_string());

clippy_lints/src/infinite_iter.rs

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use clippy_utils::diagnostics::span_lint;
2-
use clippy_utils::ty::{implements_trait, match_type};
2+
use clippy_utils::ty::{implements_trait, is_type_diagnostic_item};
33
use clippy_utils::{get_trait_def_id, higher, is_qpath_def_path, paths};
44
use rustc_hir::{BorrowKind, Expr, ExprKind};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_session::{declare_lint_pass, declare_tool_lint};
7+
use rustc_span::symbol::{sym, Symbol};
78

89
declare_clippy_lint! {
910
/// **What it does:** Checks for iteration that is guaranteed to be infinite.
@@ -202,15 +203,15 @@ const COMPLETING_METHODS: [(&str, usize); 12] = [
202203
];
203204

204205
/// the paths of types that are known to be infinitely allocating
205-
const INFINITE_COLLECTORS: [&[&str]; 8] = [
206-
&paths::BINARY_HEAP,
207-
&paths::BTREEMAP,
208-
&paths::BTREESET,
209-
&paths::HASHMAP,
210-
&paths::HASHSET,
211-
&paths::LINKED_LIST,
212-
&paths::VEC,
213-
&paths::VEC_DEQUE,
206+
const INFINITE_COLLECTORS: &[Symbol] = &[
207+
sym::BinaryHeap,
208+
sym::BTreeMap,
209+
sym::BTreeSet,
210+
sym::hashmap_type,
211+
sym::hashset_type,
212+
sym::LinkedList,
213+
sym::vec_type,
214+
sym::vecdeque_type,
214215
];
215216

216217
fn complete_infinite_iter(cx: &LateContext<'_>, expr: &Expr<'_>) -> Finiteness {
@@ -235,7 +236,10 @@ fn complete_infinite_iter(cx: &LateContext<'_>, expr: &Expr<'_>) -> Finiteness {
235236
}
236237
} else if method.ident.name == sym!(collect) {
237238
let ty = cx.typeck_results().expr_ty(expr);
238-
if INFINITE_COLLECTORS.iter().any(|path| match_type(cx, ty, path)) {
239+
if INFINITE_COLLECTORS
240+
.iter()
241+
.any(|diag_item| is_type_diagnostic_item(cx, ty, *diag_item))
242+
{
239243
return is_infinite(cx, &args[0]);
240244
}
241245
}

clippy_lints/src/loops/explicit_into_iter_loop.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
use super::EXPLICIT_INTO_ITER_LOOP;
22
use clippy_utils::diagnostics::span_lint_and_sugg;
3+
use clippy_utils::is_trait_method;
34
use clippy_utils::source::snippet_with_applicability;
4-
use clippy_utils::{match_trait_method, paths};
55
use rustc_errors::Applicability;
66
use rustc_hir::Expr;
77
use rustc_lint::LateContext;
88
use rustc_middle::ty::TyS;
9+
use rustc_span::symbol::sym;
910

1011
pub(super) fn check(cx: &LateContext<'_>, self_arg: &'hir Expr<'hir>, call_expr: &Expr<'_>) {
1112
let self_ty = cx.typeck_results().expr_ty(self_arg);
1213
let self_ty_adjusted = cx.typeck_results().expr_ty_adjusted(self_arg);
13-
if !(TyS::same_type(self_ty, self_ty_adjusted) && match_trait_method(cx, call_expr, &paths::INTO_ITERATOR)) {
14+
if !(TyS::same_type(self_ty, self_ty_adjusted) && is_trait_method(cx, call_expr, sym::IntoIterator)) {
1415
return;
1516
}
1617

clippy_lints/src/loops/explicit_iter_loop.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use super::EXPLICIT_ITER_LOOP;
22
use clippy_utils::diagnostics::span_lint_and_sugg;
3+
use clippy_utils::is_trait_method;
34
use clippy_utils::source::snippet_with_applicability;
4-
use clippy_utils::ty::{is_type_diagnostic_item, match_type};
5-
use clippy_utils::{match_trait_method, paths};
5+
use clippy_utils::ty::is_type_diagnostic_item;
66
use rustc_errors::Applicability;
77
use rustc_hir::{Expr, Mutability};
88
use rustc_lint::LateContext;
@@ -12,7 +12,7 @@ use rustc_span::sym;
1212
pub(super) fn check(cx: &LateContext<'_>, self_arg: &Expr<'_>, arg: &Expr<'_>, method_name: &str) {
1313
let should_lint = match method_name {
1414
"iter" | "iter_mut" => is_ref_iterable_type(cx, self_arg),
15-
"into_iter" if match_trait_method(cx, arg, &paths::INTO_ITERATOR) => {
15+
"into_iter" if is_trait_method(cx, arg, sym::IntoIterator) => {
1616
let receiver_ty = cx.typeck_results().expr_ty(self_arg);
1717
let receiver_ty_adjusted = cx.typeck_results().expr_ty_adjusted(self_arg);
1818
let ref_receiver_ty = cx.tcx.mk_ref(
@@ -55,13 +55,13 @@ fn is_ref_iterable_type(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
5555
let ty = cx.typeck_results().expr_ty(e);
5656
is_iterable_array(ty, cx) ||
5757
is_type_diagnostic_item(cx, ty, sym::vec_type) ||
58-
match_type(cx, ty, &paths::LINKED_LIST) ||
58+
is_type_diagnostic_item(cx, ty, sym::LinkedList) ||
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

+3-3
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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1793,8 +1793,8 @@ 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)
1797-
|| match_type(cx, ty, &paths::LINKED_LIST)
1796+
|| is_type_diagnostic_item(cx, ty, sym::BTreeMap)
1797+
|| is_type_diagnostic_item(cx, ty, sym::LinkedList)
17981798
|| match_type(cx, ty, &paths::WEAK_RC)
17991799
|| match_type(cx, ty, &paths::WEAK_ARC)
18001800
{

clippy_lints/src/methods/get_unwrap.rs

+3-3
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

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use super::utils::derefs_to_slice;
22
use clippy_utils::diagnostics::span_lint_and_sugg;
3-
use clippy_utils::paths;
43
use clippy_utils::source::snippet_with_applicability;
5-
use clippy_utils::ty::{is_type_diagnostic_item, match_type};
4+
use clippy_utils::ty::is_type_diagnostic_item;
65
use rustc_errors::Applicability;
76
use rustc_hir::Expr;
87
use rustc_lint::LateContext;
@@ -22,13 +21,13 @@ pub(crate) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, recv: &'tcx E
2221
"HashSet"
2322
} else if is_type_diagnostic_item(cx, ty, sym::hashmap_type) {
2423
"HashMap"
25-
} else if match_type(cx, ty, &paths::BTREEMAP) {
24+
} else if is_type_diagnostic_item(cx, ty, sym::BTreeMap) {
2625
"BTreeMap"
27-
} else if match_type(cx, ty, &paths::BTREESET) {
26+
} else if is_type_diagnostic_item(cx, ty, sym::BTreeSet) {
2827
"BTreeSet"
29-
} else if match_type(cx, ty, &paths::LINKED_LIST) {
28+
} else if is_type_diagnostic_item(cx, ty, sym::LinkedList) {
3029
"LinkedList"
31-
} else if match_type(cx, ty, &paths::BINARY_HEAP) {
30+
} else if is_type_diagnostic_item(cx, ty, sym::BinaryHeap) {
3231
"BinaryHeap"
3332
} else {
3433
return;

clippy_lints/src/methods/or_fun_call.rs

+2-2
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

+4-3
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

+1-2
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

+2-3
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;
@@ -105,7 +104,7 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
105104
let self_def_id = cx.tcx.hir().local_def_id(cx.tcx.hir().get_parent_item(id));
106105
let self_ty = cx.tcx.type_of(self_def_id);
107106
if TyS::same_type(self_ty, return_ty(cx, id));
108-
if let Some(default_trait_id) = get_trait_def_id(cx, &paths::DEFAULT_TRAIT);
107+
if let Some(default_trait_id) = cx.tcx.get_diagnostic_item(sym::Default);
109108
then {
110109
if self.impling_types.is_none() {
111110
let mut impls = HirIdSet::default();

clippy_lints/src/ptr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg, span_lint_and_the
44
use clippy_utils::ptr::get_spans;
55
use clippy_utils::source::snippet_opt;
66
use clippy_utils::ty::{is_type_diagnostic_item, match_type, walk_ptrs_hir_ty};
7-
use clippy_utils::{expr_path_res, is_lint_allowed, match_any_def_paths, paths};
7+
use clippy_utils::{expr_path_res, is_lint_allowed, match_any_diagnostic_items, paths};
88
use if_chain::if_chain;
99
use rustc_errors::Applicability;
1010
use rustc_hir::{
@@ -419,7 +419,7 @@ fn get_rptr_lm<'tcx>(ty: &'tcx Ty<'tcx>) -> Option<(&'tcx Lifetime, Mutability,
419419
fn is_null_path(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
420420
if let ExprKind::Call(pathexp, []) = expr.kind {
421421
expr_path_res(cx, pathexp).opt_def_id().map_or(false, |id| {
422-
match_any_def_paths(cx, id, &[&paths::PTR_NULL, &paths::PTR_NULL_MUT]).is_some()
422+
match_any_diagnostic_items(cx, id, &[sym::ptr_null, sym::ptr_null_mut]).is_some()
423423
})
424424
} else {
425425
false

clippy_lints/src/size_of_in_element_count.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ fn get_pointee_ty_and_count_expr(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -
6767
const FUNCTIONS: [&[&str]; 8] = [
6868
&paths::PTR_COPY_NONOVERLAPPING,
6969
&paths::PTR_COPY,
70-
&paths::WRITE_BYTES,
70+
&paths::PTR_WRITE_BYTES,
7171
&paths::PTR_SWAP_NONOVERLAPPING,
7272
&paths::PTR_SLICE_FROM_RAW_PARTS,
7373
&paths::PTR_SLICE_FROM_RAW_PARTS_MUT,

clippy_lints/src/transmute/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ mod useless_transmute;
1212
mod utils;
1313
mod wrong_transmute;
1414

15-
use clippy_utils::{in_constant, match_def_path, paths};
15+
use clippy_utils::in_constant;
1616
use if_chain::if_chain;
1717
use rustc_hir::{Expr, ExprKind};
1818
use rustc_lint::{LateContext, LateLintPass};
1919
use rustc_session::{declare_lint_pass, declare_tool_lint};
20+
use rustc_span::symbol::sym;
2021

2122
declare_clippy_lint! {
2223
/// **What it does:** Checks for transmutes that can't ever be correct on any
@@ -328,7 +329,7 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
328329
if let ExprKind::Call(path_expr, args) = e.kind;
329330
if let ExprKind::Path(ref qpath) = path_expr.kind;
330331
if let Some(def_id) = cx.qpath_res(qpath, path_expr.hir_id).opt_def_id();
331-
if match_def_path(cx, def_id, &paths::TRANSMUTE);
332+
if cx.tcx.is_diagnostic_item(sym::transmute, def_id);
332333
then {
333334
// Avoid suggesting from/to bits and dereferencing raw pointers in const contexts.
334335
// See https://github.com/rust-lang/rust/issues/73736 for progress on making them `const fn`.

0 commit comments

Comments
 (0)