Skip to content

Commit 6030428

Browse files
committed
Use diagnostic items for Into, IntoIterator, LinkedList, ptr::null, prt::null_mut
1 parent 2ac2188 commit 6030428

11 files changed

+45
-26
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/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

+4-4
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,7 +55,7 @@ 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) ||

clippy_lints/src/matches.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1794,7 +1794,7 @@ mod redundant_pattern_match {
17941794
|| is_type_diagnostic_item(cx, ty, sym::Arc)
17951795
|| is_type_diagnostic_item(cx, ty, sym::cstring_type)
17961796
|| is_type_diagnostic_item(cx, ty, sym::BTreeMap)
1797-
|| match_type(cx, ty, &paths::LINKED_LIST)
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/iter_count.rs

+2-3
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;
@@ -26,7 +25,7 @@ pub(crate) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, recv: &'tcx E
2625
"BTreeMap"
2726
} 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"
3130
} else if is_type_diagnostic_item(cx, ty, sym::BinaryHeap) {
3231
"BinaryHeap"

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/transmuting_null.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
use clippy_utils::consts::{constant_context, Constant};
22
use clippy_utils::diagnostics::span_lint;
3-
use clippy_utils::{is_expr_path_def_path, paths};
3+
use clippy_utils::{is_expr_diagnostic_item, is_expr_path_def_path, paths};
44
use if_chain::if_chain;
55
use rustc_ast::LitKind;
66
use rustc_hir::{Expr, ExprKind};
77
use rustc_lint::{LateContext, LateLintPass, LintContext};
88
use rustc_middle::lint::in_external_macro;
99
use rustc_session::{declare_lint_pass, declare_tool_lint};
10+
use rustc_span::symbol::sym;
1011

1112
declare_clippy_lint! {
1213
/// **What it does:** Checks for transmute calls which would receive a null pointer.
@@ -67,7 +68,7 @@ impl<'tcx> LateLintPass<'tcx> for TransmutingNull {
6768
// `std::mem::transmute(std::ptr::null::<i32>())`
6869
if_chain! {
6970
if let ExprKind::Call(func1, []) = arg.kind;
70-
if is_expr_path_def_path(cx, func1, &paths::PTR_NULL);
71+
if is_expr_diagnostic_item(cx, func1, sym::ptr_null);
7172
then {
7273
span_lint(cx, TRANSMUTING_NULL, expr.span, LINT_MSG)
7374
}

clippy_lints/src/types/linked_list.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
2-
use clippy_utils::{match_def_path, paths};
32
use rustc_hir::{self as hir, def_id::DefId};
43
use rustc_lint::LateContext;
4+
use rustc_span::symbol::sym;
55

66
use super::LINKEDLIST;
77

88
pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, def_id: DefId) -> bool {
9-
if match_def_path(cx, def_id, &paths::LINKED_LIST) {
9+
if cx.tcx.is_diagnostic_item(sym::LinkedList, def_id) {
1010
span_lint_and_help(
1111
cx,
1212
LINKEDLIST,

clippy_lints/src/useless_conversion.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg};
22
use clippy_utils::source::{snippet, snippet_with_macro_callsite};
33
use clippy_utils::sugg::Sugg;
44
use clippy_utils::ty::{is_type_diagnostic_item, same_type_and_consts};
5-
use clippy_utils::{get_parent_expr, match_def_path, match_trait_method, paths};
5+
use clippy_utils::{get_parent_expr, is_trait_method, match_def_path, match_trait_method, paths};
66
use if_chain::if_chain;
77
use rustc_errors::Applicability;
88
use rustc_hir::{Expr, ExprKind, HirId, MatchSource};
@@ -64,7 +64,7 @@ impl<'tcx> LateLintPass<'tcx> for UselessConversion {
6464
},
6565

6666
ExprKind::MethodCall(name, .., args, _) => {
67-
if match_trait_method(cx, e, &paths::INTO) && &*name.ident.as_str() == "into" {
67+
if is_trait_method(cx, e, sym::into_trait) && &*name.ident.as_str() == "into" {
6868
let a = cx.typeck_results().expr_ty(e);
6969
let b = cx.typeck_results().expr_ty(&args[0]);
7070
if same_type_and_consts(a, b) {
@@ -80,7 +80,7 @@ impl<'tcx> LateLintPass<'tcx> for UselessConversion {
8080
);
8181
}
8282
}
83-
if match_trait_method(cx, e, &paths::INTO_ITERATOR) && name.ident.name == sym::into_iter {
83+
if is_trait_method(cx, e, sym::IntoIterator) && name.ident.name == sym::into_iter {
8484
if let Some(parent_expr) = get_parent_expr(cx, e) {
8585
if let ExprKind::MethodCall(parent_name, ..) = parent_expr.kind {
8686
if parent_name.ident.name != sym::into_iter {

clippy_utils/src/lib.rs

+20
Original file line numberDiff line numberDiff line change
@@ -411,12 +411,22 @@ pub fn is_qpath_def_path(cx: &LateContext<'_>, path: &QPath<'_>, hir_id: HirId,
411411
}
412412

413413
/// If the expression is a path, resolves it to a `DefId` and checks if it matches the given path.
414+
///
415+
/// Please use `is_expr_diagnostic_item` if the target is a diagnostic item.
414416
pub fn is_expr_path_def_path(cx: &LateContext<'_>, expr: &Expr<'_>, segments: &[&str]) -> bool {
415417
expr_path_res(cx, expr)
416418
.opt_def_id()
417419
.map_or(false, |id| match_def_path(cx, id, segments))
418420
}
419421

422+
/// If the expression is a path, resolves it to a `DefId` and checks if it matches the given
423+
/// diagnostic item.
424+
pub fn is_expr_diagnostic_item(cx: &LateContext<'_>, expr: &Expr<'_>, diag_item: Symbol) -> bool {
425+
expr_path_res(cx, expr)
426+
.opt_def_id()
427+
.map_or(false, |id| cx.tcx.is_diagnostic_item(diag_item, id))
428+
}
429+
420430
/// THIS METHOD IS DEPRECATED and will eventually be removed since it does not match against the
421431
/// entire path or resolved `DefId`. Prefer using `match_def_path`. Consider getting a `DefId` from
422432
/// `QPath::Resolved.1.res.opt_def_id()`.
@@ -1249,13 +1259,23 @@ pub fn match_function_call<'tcx>(
12491259

12501260
/// Checks if the given `DefId` matches any of the paths. Returns the index of matching path, if
12511261
/// any.
1262+
///
1263+
/// Please use `match_any_diagnostic_items` if the targets are all diagnostic items.
12521264
pub fn match_any_def_paths(cx: &LateContext<'_>, did: DefId, paths: &[&[&str]]) -> Option<usize> {
12531265
let search_path = cx.get_def_path(did);
12541266
paths
12551267
.iter()
12561268
.position(|p| p.iter().map(|x| Symbol::intern(x)).eq(search_path.iter().copied()))
12571269
}
12581270

1271+
/// Checks if the given `DefId` matches any of provided diagnostic items. Returns the index of
1272+
/// matching path, if any.
1273+
pub fn match_any_diagnostic_items(cx: &LateContext<'_>, def_id: DefId, diag_items: &[Symbol]) -> Option<usize> {
1274+
diag_items
1275+
.iter()
1276+
.position(|item| cx.tcx.is_diagnostic_item(*item, def_id))
1277+
}
1278+
12591279
/// Checks if the given `DefId` matches the path.
12601280
pub fn match_def_path<'tcx>(cx: &LateContext<'tcx>, did: DefId, syms: &[&str]) -> bool {
12611281
// We should probably move to Symbols in Clippy as well rather than interning every time.

clippy_utils/src/paths.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,6 @@ pub const IDENT_AS_STR: [&str; 4] = ["rustc_span", "symbol", "Ident", "as_str"];
7373
pub const INDEX: [&str; 3] = ["core", "ops", "Index"];
7474
pub const INDEX_MUT: [&str; 3] = ["core", "ops", "IndexMut"];
7575
pub const INSERT_STR: [&str; 4] = ["alloc", "string", "String", "insert_str"];
76-
pub const INTO: [&str; 3] = ["core", "convert", "Into"];
77-
pub const INTO_ITERATOR: [&str; 5] = ["core", "iter", "traits", "collect", "IntoIterator"];
7876
pub const IO_READ: [&str; 3] = ["std", "io", "Read"];
7977
pub const IO_WRITE: [&str; 3] = ["std", "io", "Write"];
8078
pub const IPADDR_V4: [&str; 5] = ["std", "net", "ip", "IpAddr", "V4"];
@@ -85,6 +83,7 @@ pub const KW_MODULE: [&str; 3] = ["rustc_span", "symbol", "kw"];
8583
#[cfg(feature = "internal-lints")]
8684
pub const LATE_CONTEXT: [&str; 2] = ["rustc_lint", "LateContext"];
8785
pub const LIBC_STRLEN: [&str; 2] = ["libc", "strlen"];
86+
/// Preferably use the diagnostic item `sym::LinkedList` where possible
8887
pub const LINKED_LIST: [&str; 4] = ["alloc", "collections", "linked_list", "LinkedList"];
8988
#[cfg(any(feature = "internal-lints", feature = "metadata-collector-lint"))]
9089
pub const LINT: [&str; 2] = ["rustc_lint_defs", "Lint"];
@@ -99,6 +98,7 @@ pub const MEM_SIZE_OF_VAL: [&str; 3] = ["core", "mem", "size_of_val"];
9998
pub const MUTEX_GUARD: [&str; 4] = ["std", "sync", "mutex", "MutexGuard"];
10099
pub const OPEN_OPTIONS: [&str; 3] = ["std", "fs", "OpenOptions"];
101100
pub const OPS_MODULE: [&str; 2] = ["core", "ops"];
101+
/// Preferably use the diagnostic item `sym::option_type` where possible
102102
pub const OPTION: [&str; 3] = ["core", "option", "Option"];
103103
pub const OPTION_NONE: [&str; 4] = ["core", "option", "Option", "None"];
104104
pub const OPTION_SOME: [&str; 4] = ["core", "option", "Option", "Some"];
@@ -122,8 +122,6 @@ pub const POLL_READY: [&str; 5] = ["core", "task", "poll", "Poll", "Ready"];
122122
pub const PTR_COPY: [&str; 3] = ["core", "intrinsics", "copy"];
123123
pub const PTR_COPY_NONOVERLAPPING: [&str; 3] = ["core", "intrinsics", "copy_nonoverlapping"];
124124
pub const PTR_EQ: [&str; 3] = ["core", "ptr", "eq"];
125-
pub const PTR_NULL: [&str; 3] = ["core", "ptr", "null"];
126-
pub const PTR_NULL_MUT: [&str; 3] = ["core", "ptr", "null_mut"];
127125
pub const PTR_SLICE_FROM_RAW_PARTS: [&str; 3] = ["core", "ptr", "slice_from_raw_parts"];
128126
pub const PTR_SLICE_FROM_RAW_PARTS_MUT: [&str; 3] = ["core", "ptr", "slice_from_raw_parts_mut"];
129127
pub const PTR_SWAP_NONOVERLAPPING: [&str; 3] = ["core", "ptr", "swap_nonoverlapping"];

0 commit comments

Comments
 (0)