Skip to content

Commit 9de3e6c

Browse files
committed
Add more diagnostic items for clippy
1 parent c5c6d70 commit 9de3e6c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+191
-271
lines changed

clippy_lints/src/await_holding_invalid.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,5 +287,5 @@ fn is_mutex_guard(cx: &LateContext<'_>, def_id: DefId) -> bool {
287287
}
288288

289289
fn is_refcell_ref(cx: &LateContext<'_>, def_id: DefId) -> bool {
290-
match_def_path(cx, def_id, &paths::REFCELL_REF) || match_def_path(cx, def_id, &paths::REFCELL_REFMUT)
290+
matches!(cx.tcx.get_diagnostic_name(def_id), Some(sym::RefCellRef | sym::RefCellRefMut))
291291
}

clippy_lints/src/box_default.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::macros::macro_backtrace;
33
use clippy_utils::ty::expr_sig;
4-
use clippy_utils::{get_parent_node, is_default_equivalent, match_path, path_def_id, paths};
4+
use clippy_utils::{get_parent_node, is_default_equivalent, path_def_id};
55
use rustc_errors::Applicability;
66
use rustc_hir::intravisit::{walk_ty, Visitor};
7-
use rustc_hir::{Block, Expr, ExprKind, Local, Node, QPath, TyKind};
7+
use rustc_hir::{def::Res, Block, Expr, ExprKind, Local, Node, QPath, TyKind};
88
use rustc_lint::{LateContext, LateLintPass, LintContext};
99
use rustc_middle::lint::in_external_macro;
1010
use rustc_middle::ty::print::with_forced_trimmed_paths;
@@ -55,7 +55,7 @@ impl LateLintPass<'_> for BoxDefault {
5555
expr.span,
5656
"`Box::new(_)` of default value",
5757
"try",
58-
if is_plain_default(arg_path) || given_type(cx, expr) {
58+
if is_plain_default(cx, arg_path) || given_type(cx, expr) {
5959
"Box::default()".into()
6060
} else if let Some(arg_ty) = cx.typeck_results().expr_ty(arg).make_suggestable(cx.tcx, true) {
6161
with_forced_trimmed_paths!(format!("Box::<{arg_ty}>::default()"))
@@ -68,11 +68,13 @@ impl LateLintPass<'_> for BoxDefault {
6868
}
6969
}
7070

71-
fn is_plain_default(arg_path: &Expr<'_>) -> bool {
71+
fn is_plain_default(cx: &LateContext<'_>, arg_path: &Expr<'_>) -> bool {
7272
// we need to match the actual path so we don't match e.g. "u8::default"
73-
if let ExprKind::Path(QPath::Resolved(None, path)) = &arg_path.kind {
73+
if let ExprKind::Path(QPath::Resolved(None, path)) = &arg_path.kind
74+
&& let Res::Def(_, def_id) = path.res
75+
{
7476
// avoid generic parameters
75-
match_path(path, &paths::DEFAULT_TRAIT_METHOD) && path.segments.iter().all(|seg| seg.args.is_none())
77+
cx.tcx.is_diagnostic_item(sym::default_fn, def_id) && path.segments.iter().all(|seg| seg.args.is_none())
7678
} else {
7779
false
7880
}

clippy_lints/src/casts/cast_ptr_alignment.rs

Lines changed: 10 additions & 9 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::is_c_void;
3-
use clippy_utils::{get_parent_expr, is_hir_ty_cfg_dependant, match_any_def_paths, paths};
3+
use clippy_utils::{get_parent_expr, is_hir_ty_cfg_dependant};
44
use rustc_hir::{Expr, ExprKind, GenericArg};
55
use rustc_lint::LateContext;
66
use rustc_middle::ty::layout::LayoutOf;
@@ -75,16 +75,17 @@ fn is_used_as_unaligned(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
7575
}
7676
},
7777
ExprKind::Call(func, [arg, ..]) if arg.hir_id == e.hir_id => {
78-
static PATHS: &[&[&str]] = &[
79-
paths::PTR_READ_UNALIGNED.as_slice(),
80-
paths::PTR_UNALIGNED_VOLATILE_LOAD.as_slice(),
81-
paths::PTR_UNALIGNED_VOLATILE_STORE.as_slice(),
82-
];
83-
8478
if let ExprKind::Path(path) = &func.kind
8579
&& let Some(def_id) = cx.qpath_res(path, func.hir_id).opt_def_id()
86-
&& (match_any_def_paths(cx, def_id, PATHS).is_some()
87-
|| cx.tcx.is_diagnostic_item(sym::ptr_write_unaligned, def_id))
80+
&& matches!(
81+
cx.tcx.get_diagnostic_name(def_id),
82+
Some(
83+
sym::ptr_write_unaligned
84+
| sym::ptr_read_unaligned
85+
| sym::intrinsics_unaligned_volatile_load
86+
| sym::intrinsics_unaligned_volatile_store
87+
)
88+
)
8889
{
8990
true
9091
} else {

clippy_lints/src/casts/cast_slice_from_raw_parts.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::msrvs::{self, Msrv};
33
use clippy_utils::source::snippet_with_context;
4-
use clippy_utils::{match_def_path, paths};
54
use if_chain::if_chain;
65
use rustc_errors::Applicability;
76
use rustc_hir::def_id::DefId;
87
use rustc_hir::{Expr, ExprKind};
98
use rustc_lint::LateContext;
109
use rustc_middle::ty::{self, Ty};
10+
use rustc_span::sym;
1111

1212
use super::CAST_SLICE_FROM_RAW_PARTS;
1313

@@ -17,12 +17,10 @@ enum RawPartsKind {
1717
}
1818

1919
fn raw_parts_kind(cx: &LateContext<'_>, did: DefId) -> Option<RawPartsKind> {
20-
if match_def_path(cx, did, &paths::SLICE_FROM_RAW_PARTS) {
21-
Some(RawPartsKind::Immutable)
22-
} else if match_def_path(cx, did, &paths::SLICE_FROM_RAW_PARTS_MUT) {
23-
Some(RawPartsKind::Mutable)
24-
} else {
25-
None
20+
match cx.tcx.get_diagnostic_name(did)? {
21+
sym::slice_from_raw_parts => Some(RawPartsKind::Immutable),
22+
sym::slice_from_raw_parts_mut => Some(RawPartsKind::Mutable),
23+
_ => None,
2624
}
2725
}
2826

clippy_lints/src/create_dir.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::{Expr, ExprKind};
76
use rustc_lint::{LateContext, LateLintPass};
87
use rustc_session::{declare_lint_pass, declare_tool_lint};
8+
use rustc_span::sym;
99

1010
declare_clippy_lint! {
1111
/// ### What it does
@@ -37,7 +37,7 @@ impl LateLintPass<'_> for CreateDir {
3737
if let ExprKind::Call(func, [arg, ..]) = expr.kind;
3838
if let ExprKind::Path(ref path) = func.kind;
3939
if let Some(def_id) = cx.qpath_res(path, func.hir_id).opt_def_id();
40-
if match_def_path(cx, def_id, &paths::STD_FS_CREATE_DIR);
40+
if cx.tcx.is_diagnostic_item(sym::fs_create_dir, def_id);
4141
then {
4242
span_lint_and_sugg(
4343
cx,

clippy_lints/src/default.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
use clippy_utils::diagnostics::{span_lint_and_note, span_lint_and_sugg};
22
use clippy_utils::source::snippet_with_context;
33
use clippy_utils::ty::{has_drop, is_copy};
4-
use clippy_utils::{
5-
any_parent_is_automatically_derived, contains_name, get_parent_expr, is_from_proc_macro, match_def_path, paths,
6-
};
4+
use clippy_utils::{any_parent_is_automatically_derived, contains_name, get_parent_expr, is_from_proc_macro};
75
use if_chain::if_chain;
86
use rustc_data_structures::fx::FxHashSet;
97
use rustc_errors::Applicability;
@@ -14,7 +12,7 @@ use rustc_middle::ty;
1412
use rustc_middle::ty::print::with_forced_trimmed_paths;
1513
use rustc_session::{declare_tool_lint, impl_lint_pass};
1614
use rustc_span::symbol::{Ident, Symbol};
17-
use rustc_span::Span;
15+
use rustc_span::{sym, Span};
1816

1917
declare_clippy_lint! {
2018
/// ### What it does
@@ -91,7 +89,7 @@ impl<'tcx> LateLintPass<'tcx> for Default {
9189
if !any_parent_is_automatically_derived(cx.tcx, expr.hir_id);
9290
if let ExprKind::Path(ref qpath) = path.kind;
9391
if let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id();
94-
if match_def_path(cx, def_id, &paths::DEFAULT_TRAIT_METHOD);
92+
if cx.tcx.is_diagnostic_item(sym::default_fn, def_id);
9593
if !is_update_syntax_base(cx, expr);
9694
// Detect and ignore <Foo as Default>::default() because these calls do explicitly name the type.
9795
if let QPath::Resolved(None, _path) = qpath;
@@ -268,7 +266,7 @@ fn is_expr_default<'tcx>(expr: &'tcx Expr<'tcx>, cx: &LateContext<'tcx>) -> bool
268266
if let Res::Def(_, def_id) = cx.qpath_res(qpath, fn_expr.hir_id);
269267
then {
270268
// right hand side of assignment is `Default::default`
271-
match_def_path(cx, def_id, &paths::DEFAULT_TRAIT_METHOD)
269+
cx.tcx.is_diagnostic_item(sym::default_fn, def_id)
272270
} else {
273271
false
274272
}

clippy_lints/src/default_constructed_unit_structs.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::{is_ty_alias, match_def_path, paths};
2+
use clippy_utils::is_ty_alias;
33
use hir::def::Res;
44
use hir::ExprKind;
55
use rustc_errors::Applicability;
66
use rustc_hir as hir;
77
use rustc_lint::{LateContext, LateLintPass};
88
use rustc_middle::ty;
99
use rustc_session::{declare_lint_pass, declare_tool_lint};
10+
use rustc_span::sym;
1011

1112
declare_clippy_lint! {
1213
/// ### What it does
@@ -63,7 +64,7 @@ impl LateLintPass<'_> for DefaultConstructedUnitStructs {
6364
// `<Foo as Bar>::Assoc` cannot be used as a constructor
6465
if !is_alias(*base);
6566
if let Res::Def(_, def_id) = cx.qpath_res(qpath, fn_expr.hir_id);
66-
if match_def_path(cx, def_id, &paths::DEFAULT_TRAIT_METHOD);
67+
if cx.tcx.is_diagnostic_item(sym::default_fn, def_id);
6768
// make sure we have a struct with no fields (unit struct)
6869
if let ty::Adt(def, ..) = cx.typeck_results().expr_ty(expr).kind();
6970
if def.is_struct();

clippy_lints/src/default_instead_of_iter_empty.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_sugg;
22
use clippy_utils::source::snippet_with_context;
3-
use clippy_utils::{last_path_segment, match_def_path, paths};
3+
use clippy_utils::last_path_segment;
44
use rustc_errors::Applicability;
55
use rustc_hir::{def, Expr, ExprKind, GenericArg, QPath, TyKind};
66
use rustc_lint::{LateContext, LateLintPass};
77
use rustc_session::{declare_lint_pass, declare_tool_lint};
8-
use rustc_span::SyntaxContext;
8+
use rustc_span::{sym, SyntaxContext};
99

1010
declare_clippy_lint! {
1111
/// ### What it does
@@ -37,7 +37,7 @@ impl<'tcx> LateLintPass<'tcx> for DefaultIterEmpty {
3737
&& let TyKind::Path(ty_path) = &ty.kind
3838
&& let QPath::Resolved(None, path) = ty_path
3939
&& let def::Res::Def(_, def_id) = &path.res
40-
&& match_def_path(cx, *def_id, &paths::ITER_EMPTY)
40+
&& cx.tcx.is_diagnostic_item(sym::IterEmpty, *def_id)
4141
&& let ctxt = expr.span.ctxt()
4242
&& ty.span.ctxt() == ctxt
4343
{

clippy_lints/src/exit.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use clippy_utils::diagnostics::span_lint;
2-
use clippy_utils::{is_entrypoint_fn, match_def_path, paths};
2+
use clippy_utils::{is_entrypoint_fn};
33
use if_chain::if_chain;
44
use rustc_hir::{Expr, ExprKind, Item, ItemKind, Node};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_session::{declare_lint_pass, declare_tool_lint};
7+
use rustc_span::sym;
78

89
declare_clippy_lint! {
910
/// ### What it does
@@ -45,7 +46,7 @@ impl<'tcx> LateLintPass<'tcx> for Exit {
4546
if let ExprKind::Call(path_expr, _args) = e.kind;
4647
if let ExprKind::Path(ref path) = path_expr.kind;
4748
if let Some(def_id) = cx.qpath_res(path, path_expr.hir_id).opt_def_id();
48-
if match_def_path(cx, def_id, &paths::EXIT);
49+
if cx.tcx.is_diagnostic_item(sym::process_exit, def_id);
4950
let parent = cx.tcx.hir().get_parent_item(e.hir_id).def_id;
5051
if let Some(Node::Item(Item{kind: ItemKind::Fn(..), ..})) = cx.tcx.hir().find_by_def_id(parent);
5152
// If the next item up is a function we check if it is an entry point

clippy_lints/src/explicit_write.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::macros::{find_format_args, format_args_inputs_span};
33
use clippy_utils::source::snippet_with_applicability;
4-
use clippy_utils::{is_expn_of, match_function_call, paths};
4+
use clippy_utils::{is_expn_of, path_def_id};
55
use if_chain::if_chain;
66
use rustc_errors::Applicability;
77
use rustc_hir::def::Res;
@@ -47,18 +47,19 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitWrite {
4747
if let ExprKind::MethodCall(unwrap_fun, write_call, [], _) = expr.kind
4848
&& unwrap_fun.ident.name == sym::unwrap
4949
// match call to write_fmt
50-
&& let ExprKind::MethodCall(write_fun, write_recv, [write_arg], _) = look_in_block(cx, &write_call.kind)
50+
&& let ExprKind::MethodCall(write_fun, write_recv, [write_arg], _) = *look_in_block(cx, &write_call.kind)
51+
&& let ExprKind::Call(write_recv_path, _) = write_recv.kind
5152
&& write_fun.ident.name == sym!(write_fmt)
52-
// match calls to std::io::stdout() / std::io::stderr ()
53-
&& let Some(dest_name) = if match_function_call(cx, write_recv, &paths::STDOUT).is_some() {
54-
Some("stdout")
55-
} else if match_function_call(cx, write_recv, &paths::STDERR).is_some() {
56-
Some("stderr")
57-
} else {
58-
None
59-
}
60-
&& let Some(format_args) = find_format_args(cx, write_arg, ExpnId::root())
53+
&& let Some(def_id) = path_def_id(cx, write_recv_path)
6154
{
55+
// match calls to std::io::stdout() / std::io::stderr ()
56+
let (dest_name, prefix) = match cx.tcx.get_diagnostic_name(def_id) {
57+
Some(sym::io_stdout) => ("stdout", ""),
58+
Some(sym::io_stderr) => ("stderr", "e"),
59+
_ => return,
60+
};
61+
let Some(format_args) = find_format_args(cx, write_arg, ExpnId::root()) else { return; };
62+
6263
// ordering is important here, since `writeln!` uses `write!` internally
6364
let calling_macro = if is_expn_of(write_call.span, "writeln").is_some() {
6465
Some("writeln")
@@ -67,11 +68,6 @@ impl<'tcx> LateLintPass<'tcx> for ExplicitWrite {
6768
} else {
6869
None
6970
};
70-
let prefix = if dest_name == "stderr" {
71-
"e"
72-
} else {
73-
""
74-
};
7571

7672
// We need to remove the last trailing newline from the string because the
7773
// underlying `fmt::write` function doesn't know whether `println!` or `print!` was

clippy_lints/src/from_raw_with_void_ptr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
22
use clippy_utils::ty::is_c_void;
3-
use clippy_utils::{match_def_path, path_def_id, paths};
3+
use clippy_utils::path_def_id;
44
use rustc_hir::def_id::DefId;
55
use rustc_hir::{Expr, ExprKind, QPath};
66
use rustc_lint::{LateContext, LateLintPass};
@@ -68,7 +68,7 @@ fn def_id_matches_type(cx: &LateContext<'_>, def_id: DefId) -> Option<&'static s
6868
}
6969
}
7070

71-
if match_def_path(cx, def_id, &paths::WEAK_RC) || match_def_path(cx, def_id, &paths::WEAK_ARC) {
71+
if matches!(cx.tcx.get_diagnostic_name(def_id), Some(sym::RcWeak | sym::ArcWeak)) {
7272
Some("Weak")
7373
} else {
7474
None

clippy_lints/src/instant_subtraction.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,7 @@ fn is_instant_now_call(cx: &LateContext<'_>, expr_block: &'_ Expr<'_>) -> bool {
130130

131131
fn is_an_instant(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
132132
let expr_ty = cx.typeck_results().expr_ty(expr);
133-
134-
match expr_ty.kind() {
135-
rustc_middle::ty::Adt(def, _) => clippy_utils::match_def_path(cx, def.did(), &clippy_utils::paths::INSTANT),
136-
_ => false,
137-
}
133+
ty::is_type_diagnostic_item(cx, expr_ty, sym::Instant)
138134
}
139135

140136
fn is_a_duration(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {

clippy_lints/src/lines_filter_map_ok.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
2-
use clippy_utils::ty::match_type;
2+
use clippy_utils::ty::is_type_diagnostic_item;
33
use clippy_utils::{is_diag_item_method, is_trait_method, match_def_path, path_to_local_id, paths};
44
use rustc_errors::Applicability;
55
use rustc_hir::{Body, Closure, Expr, ExprKind};
@@ -62,7 +62,7 @@ impl LateLintPass<'_> for LinesFilterMapOk {
6262
if let ExprKind::MethodCall(fm_method, fm_receiver, [fm_arg], fm_span) = expr.kind &&
6363
is_trait_method(cx, expr, sym::Iterator) &&
6464
(fm_method.ident.as_str() == "filter_map" || fm_method.ident.as_str() == "flat_map") &&
65-
match_type(cx, cx.typeck_results().expr_ty_adjusted(fm_receiver), &paths::STD_IO_LINES)
65+
is_type_diagnostic_item(cx, cx.typeck_results().expr_ty_adjusted(fm_receiver), sym::IoLines)
6666
{
6767
let lint = match &fm_arg.kind {
6868
// Detect `Result::ok`

clippy_lints/src/manual_retain.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ fn check_to_owned(
135135
if msrv.meets(msrvs::STRING_RETAIN)
136136
&& let hir::ExprKind::MethodCall(_, filter_expr, [], _) = &target_expr.kind
137137
&& let Some(to_owned_def_id) = cx.typeck_results().type_dependent_def_id(target_expr.hir_id)
138-
&& match_def_path(cx, to_owned_def_id, &paths::TO_OWNED_METHOD)
138+
&& cx.tcx.is_diagnostic_item(sym::to_owned_method, to_owned_def_id)
139139
&& let hir::ExprKind::MethodCall(_, chars_expr, [_], _) = &filter_expr.kind
140140
&& let Some(filter_def_id) = cx.typeck_results().type_dependent_def_id(filter_expr.hir_id)
141141
&& match_def_path(cx, filter_def_id, &paths::CORE_ITER_FILTER)

clippy_lints/src/methods/bytecount.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::source::snippet_with_applicability;
3-
use clippy_utils::ty::match_type;
3+
use clippy_utils::ty::is_type_diagnostic_item;
44
use clippy_utils::visitors::is_local_used;
5-
use clippy_utils::{path_to_local_id, paths, peel_blocks, peel_ref_operators, strip_pat_refs};
5+
use clippy_utils::{path_to_local_id, peel_blocks, peel_ref_operators, strip_pat_refs};
66
use if_chain::if_chain;
77
use rustc_errors::Applicability;
88
use rustc_hir::{BinOpKind, Closure, Expr, ExprKind, PatKind};
@@ -25,9 +25,9 @@ pub(super) fn check<'tcx>(
2525
if let PatKind::Binding(_, arg_id, _, _) = strip_pat_refs(param.pat).kind;
2626
if let ExprKind::Binary(ref op, l, r) = body.value.kind;
2727
if op.node == BinOpKind::Eq;
28-
if match_type(cx,
28+
if is_type_diagnostic_item(cx,
2929
cx.typeck_results().expr_ty(filter_recv).peel_refs(),
30-
&paths::SLICE_ITER);
30+
sym::SliceIter);
3131
let operand_is_arg = |expr| {
3232
let expr = peel_ref_operators(cx, peel_blocks(expr));
3333
path_to_local_id(expr, arg_id)

0 commit comments

Comments
 (0)