Skip to content

Commit e2dc8ca

Browse files
committed
Replace a few paths with diagnostic items
1 parent 5707491 commit e2dc8ca

File tree

6 files changed

+24
-31
lines changed

6 files changed

+24
-31
lines changed

clippy_lints/src/derive.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_note, span_lint_and_then};
22
use clippy_utils::paths;
33
use clippy_utils::ty::{implements_trait, is_copy};
4-
use clippy_utils::{get_trait_def_id, is_automatically_derived, is_lint_allowed, match_def_path};
4+
use clippy_utils::{is_automatically_derived, is_lint_allowed, match_def_path};
55
use if_chain::if_chain;
66
use rustc_hir::intravisit::{walk_expr, walk_fn, walk_item, FnKind, Visitor};
77
use rustc_hir::{
@@ -12,6 +12,7 @@ use rustc_middle::hir::nested_filter;
1212
use rustc_middle::ty::{self, Ty};
1313
use rustc_session::{declare_lint_pass, declare_tool_lint};
1414
use rustc_span::source_map::Span;
15+
use rustc_span::sym;
1516

1617
declare_clippy_lint! {
1718
/// ### What it does
@@ -196,7 +197,7 @@ fn check_hash_peq<'tcx>(
196197
if_chain! {
197198
if let Some(peq_trait_def_id) = cx.tcx.lang_items().eq_trait();
198199
if let Some(def_id) = trait_ref.trait_def_id();
199-
if match_def_path(cx, def_id, &paths::HASH);
200+
if cx.tcx.is_diagnostic_item(sym::Hash, def_id);
200201
then {
201202
// Look for the PartialEq implementations for `ty`
202203
cx.tcx.for_each_relevant_impl(peq_trait_def_id, ty, |impl_id| {
@@ -247,7 +248,7 @@ fn check_ord_partial_ord<'tcx>(
247248
ord_is_automatically_derived: bool,
248249
) {
249250
if_chain! {
250-
if let Some(ord_trait_def_id) = get_trait_def_id(cx, &paths::ORD);
251+
if let Some(ord_trait_def_id) = cx.tcx.get_diagnostic_item(sym::Ord);
251252
if let Some(partial_ord_trait_def_id) = cx.tcx.lang_items().partial_ord_trait();
252253
if let Some(def_id) = &trait_ref.trait_def_id();
253254
if *def_id == ord_trait_def_id;

clippy_lints/src/duration_subsec.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1+
use clippy_utils::consts::{constant, Constant};
2+
use clippy_utils::diagnostics::span_lint_and_sugg;
13
use clippy_utils::source::snippet_with_applicability;
2-
use clippy_utils::ty::match_type;
4+
use clippy_utils::ty::is_type_diagnostic_item;
35
use if_chain::if_chain;
46
use rustc_errors::Applicability;
57
use rustc_hir::{BinOpKind, Expr, ExprKind};
68
use rustc_lint::{LateContext, LateLintPass};
79
use rustc_session::{declare_lint_pass, declare_tool_lint};
810
use rustc_span::source_map::Spanned;
9-
10-
use clippy_utils::consts::{constant, Constant};
11-
use clippy_utils::diagnostics::span_lint_and_sugg;
12-
use clippy_utils::paths;
11+
use rustc_span::sym;
1312

1413
declare_clippy_lint! {
1514
/// ### What it does
@@ -46,7 +45,7 @@ impl<'tcx> LateLintPass<'tcx> for DurationSubsec {
4645
if_chain! {
4746
if let ExprKind::Binary(Spanned { node: BinOpKind::Div, .. }, left, right) = expr.kind;
4847
if let ExprKind::MethodCall(method_path, args, _) = left.kind;
49-
if match_type(cx, cx.typeck_results().expr_ty(&args[0]).peel_refs(), &paths::DURATION);
48+
if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&args[0]).peel_refs(), sym::Duration);
5049
if let Some((Constant::Int(divisor), _)) = constant(cx, cx.typeck_results(), right);
5150
then {
5251
let suggested_fn = match (method_path.ident.as_str(), divisor) {

clippy_lints/src/infinite_iter.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint;
22
use clippy_utils::ty::{implements_trait, is_type_diagnostic_item};
3-
use clippy_utils::{get_trait_def_id, higher, match_def_path, path_def_id, paths};
3+
use clippy_utils::{higher, match_def_path, path_def_id, paths};
44
use rustc_hir::{BorrowKind, Expr, ExprKind};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -229,9 +229,12 @@ fn complete_infinite_iter(cx: &LateContext<'_>, expr: &Expr<'_>) -> Finiteness {
229229
}
230230
}
231231
if method.ident.name == sym!(last) && args.len() == 1 {
232-
let not_double_ended = get_trait_def_id(cx, &paths::DOUBLE_ENDED_ITERATOR).map_or(false, |id| {
233-
!implements_trait(cx, cx.typeck_results().expr_ty(&args[0]), id, &[])
234-
});
232+
let not_double_ended = cx
233+
.tcx
234+
.get_diagnostic_item(sym::DoubleEndedIterator)
235+
.map_or(false, |id| {
236+
!implements_trait(cx, cx.typeck_results().expr_ty(&args[0]), id, &[])
237+
});
235238
if not_double_ended {
236239
return is_infinite(cx, &args[0]);
237240
}

clippy_lints/src/minmax.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use clippy_utils::consts::{constant_simple, Constant};
22
use clippy_utils::diagnostics::span_lint;
3-
use clippy_utils::{match_def_path, match_trait_method, paths};
3+
use clippy_utils::{match_trait_method, paths};
44
use if_chain::if_chain;
55
use rustc_hir::{Expr, ExprKind};
66
use rustc_lint::{LateContext, LateLintPass};
77
use rustc_session::{declare_lint_pass, declare_tool_lint};
8+
use rustc_span::sym;
89
use std::cmp::Ordering;
910

1011
declare_clippy_lint! {
@@ -73,14 +74,10 @@ fn min_max<'a>(cx: &LateContext<'_>, expr: &'a Expr<'a>) -> Option<(MinMax, Cons
7374
cx.typeck_results()
7475
.qpath_res(qpath, path.hir_id)
7576
.opt_def_id()
76-
.and_then(|def_id| {
77-
if match_def_path(cx, def_id, &paths::CMP_MIN) {
78-
fetch_const(cx, args, MinMax::Min)
79-
} else if match_def_path(cx, def_id, &paths::CMP_MAX) {
80-
fetch_const(cx, args, MinMax::Max)
81-
} else {
82-
None
83-
}
77+
.and_then(|def_id| match cx.tcx.get_diagnostic_name(def_id) {
78+
Some(sym::cmp_min) => fetch_const(cx, args, MinMax::Min),
79+
Some(sym::cmp_max) => fetch_const(cx, args, MinMax::Max),
80+
_ => None,
8481
})
8582
} else {
8683
None

clippy_lints/src/types/borrowed_box.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::source::snippet;
3-
use clippy_utils::{match_def_path, paths};
43
use if_chain::if_chain;
54
use rustc_errors::Applicability;
65
use rustc_hir::{self as hir, GenericArg, GenericBounds, GenericParamKind};
76
use rustc_hir::{HirId, Lifetime, MutTy, Mutability, Node, QPath, TyKind};
87
use rustc_lint::LateContext;
8+
use rustc_span::sym;
99

1010
use super::BORROWED_BOX;
1111

@@ -89,7 +89,7 @@ fn is_any_trait(cx: &LateContext<'_>, t: &hir::Ty<'_>) -> bool {
8989
if let Some(trait_did) = traits[0].trait_ref.trait_def_id();
9090
// Only Send/Sync can be used as additional traits, so it is enough to
9191
// check only the first trait.
92-
if match_def_path(cx, trait_did, &paths::ANY_TRAIT);
92+
if cx.tcx.is_diagnostic_item(sym::Any, trait_did);
9393
then {
9494
return true;
9595
}

clippy_utils/src/paths.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
//! Whenever possible, please consider diagnostic items over hardcoded paths.
55
//! See <https://github.com/rust-lang/rust-clippy/issues/5393> for more information.
66
7-
pub const ANY_TRAIT: [&str; 3] = ["core", "any", "Any"];
87
#[cfg(feature = "internal")]
98
pub const APPLICABILITY: [&str; 2] = ["rustc_lint_defs", "Applicability"];
109
#[cfg(feature = "internal")]
@@ -32,8 +31,6 @@ pub const BTREEMAP_CONTAINS_KEY: [&str; 6] = ["alloc", "collections", "btree", "
3231
pub const BTREEMAP_ENTRY: [&str; 6] = ["alloc", "collections", "btree", "map", "entry", "Entry"];
3332
pub const BTREEMAP_INSERT: [&str; 6] = ["alloc", "collections", "btree", "map", "BTreeMap", "insert"];
3433
pub const CLONE_TRAIT_METHOD: [&str; 4] = ["core", "clone", "Clone", "clone"];
35-
pub const CMP_MAX: [&str; 3] = ["core", "cmp", "max"];
36-
pub const CMP_MIN: [&str; 3] = ["core", "cmp", "min"];
3734
pub const COW: [&str; 3] = ["alloc", "borrow", "Cow"];
3835
pub const CSTRING_AS_C_STR: [&str; 5] = ["std", "ffi", "c_str", "CString", "as_c_str"];
3936
pub const DEFAULT_TRAIT_METHOD: [&str; 4] = ["core", "default", "Default", "default"];
@@ -42,9 +39,7 @@ pub const DEREF_MUT_TRAIT_METHOD: [&str; 5] = ["core", "ops", "deref", "DerefMut
4239
pub const DEREF_TRAIT_METHOD: [&str; 5] = ["core", "ops", "deref", "Deref", "deref"];
4340
pub const DIR_BUILDER: [&str; 3] = ["std", "fs", "DirBuilder"];
4441
pub const DISPLAY_TRAIT: [&str; 3] = ["core", "fmt", "Display"];
45-
pub const DOUBLE_ENDED_ITERATOR: [&str; 4] = ["core", "iter", "traits", "DoubleEndedIterator"];
4642
pub const DROP: [&str; 3] = ["core", "mem", "drop"];
47-
pub const DURATION: [&str; 3] = ["core", "time", "Duration"];
4843
#[cfg(feature = "internal")]
4944
pub const EARLY_CONTEXT: [&str; 2] = ["rustc_lint", "EarlyContext"];
5045
#[allow(clippy::invalid_paths)] // `check_path` does not seem to work for macros
@@ -59,15 +54,13 @@ pub const FILE_TYPE: [&str; 3] = ["std", "fs", "FileType"];
5954
#[allow(clippy::invalid_paths)] // `check_path` does not seem to work for macros
6055
pub const FORMAT_ARGS_MACRO: [&str; 4] = ["core", "macros", "builtin", "format_args"];
6156
pub const FROM_FROM: [&str; 4] = ["core", "convert", "From", "from"];
62-
pub const FROM_ITERATOR: [&str; 5] = ["core", "iter", "traits", "collect", "FromIterator"];
6357
pub const FROM_ITERATOR_METHOD: [&str; 6] = ["core", "iter", "traits", "collect", "FromIterator", "from_iter"];
6458
pub const FROM_STR_METHOD: [&str; 5] = ["core", "str", "traits", "FromStr", "from_str"];
6559
pub const FUTURE_FROM_GENERATOR: [&str; 3] = ["core", "future", "from_generator"];
6660
#[allow(clippy::invalid_paths)] // internal lints do not know about all external crates
6761
pub const FUTURES_IO_ASYNCREADEXT: [&str; 3] = ["futures_util", "io", "AsyncReadExt"];
6862
#[allow(clippy::invalid_paths)] // internal lints do not know about all external crates
6963
pub const FUTURES_IO_ASYNCWRITEEXT: [&str; 3] = ["futures_util", "io", "AsyncWriteExt"];
70-
pub const HASH: [&str; 3] = ["core", "hash", "Hash"];
7164
pub const HASHMAP_CONTAINS_KEY: [&str; 6] = ["std", "collections", "hash", "map", "HashMap", "contains_key"];
7265
pub const HASHMAP_ENTRY: [&str; 5] = ["std", "collections", "hash", "map", "Entry"];
7366
pub const HASHMAP_INSERT: [&str; 6] = ["std", "collections", "hash", "map", "HashMap", "insert"];

0 commit comments

Comments
 (0)