Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 6030428

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

File tree

11 files changed

+45
-26
lines changed

11 files changed

+45
-26
lines changed

clippy_lints/src/from_over_into.rs

Lines changed: 3 additions & 3 deletions
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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 3 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 20 additions & 0 deletions
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.

0 commit comments

Comments
 (0)