Skip to content

Commit 0560075

Browse files
committed
New internal lint: repeated_is_diagnostic_item
1 parent 6e33489 commit 0560075

27 files changed

+1475
-84
lines changed

clippy_lints/src/booleans.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ fn simplify_not(cx: &LateContext<'_>, curr_msrv: Msrv, expr: &Expr<'_>) -> Optio
433433
},
434434
ExprKind::MethodCall(path, receiver, args, _) => {
435435
let type_of_receiver = cx.typeck_results().expr_ty(receiver);
436-
if !type_of_receiver.is_diag_item(cx, sym::Option) && !type_of_receiver.is_diag_item(cx, sym::Result) {
436+
if !matches!(type_of_receiver.opt_diag_name(cx), Some(sym::Option | sym::Result)) {
437437
return None;
438438
}
439439
METHODS_WITH_NEGATION

clippy_lints/src/fallible_impl_from.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ fn lint_impl_body(cx: &LateContext<'_>, item_def_id: hir::OwnerId, impl_span: Sp
8282
// check for `unwrap`
8383
if let Some(arglists) = method_chain_args(expr, &[sym::unwrap]) {
8484
let receiver_ty = self.typeck_results.expr_ty(arglists[0].0).peel_refs();
85-
if receiver_ty.is_diag_item(self.lcx, sym::Option) || receiver_ty.is_diag_item(self.lcx, sym::Result) {
85+
if matches!(receiver_ty.opt_diag_name(self.lcx), Some(sym::Option | sym::Result)) {
8686
self.result.push(expr.span);
8787
}
8888
}

clippy_lints/src/implicit_hasher.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,14 +227,15 @@ impl<'tcx> ImplicitHasherType<'tcx> {
227227

228228
let ty = lower_ty(cx.tcx, hir_ty);
229229

230-
if ty.is_diag_item(cx, sym::HashMap) && params_len == 2 {
230+
let name = ty.opt_diag_name(cx);
231+
if name == Some(sym::HashMap) && params_len == 2 {
231232
Some(ImplicitHasherType::HashMap(
232233
hir_ty.span,
233234
ty,
234235
snippet(cx, params[0].span, "K"),
235236
snippet(cx, params[1].span, "V"),
236237
))
237-
} else if ty.is_diag_item(cx, sym::HashSet) && params_len == 1 {
238+
} else if name == Some(sym::HashSet) && params_len == 1 {
238239
Some(ImplicitHasherType::HashSet(
239240
hir_ty.span,
240241
ty,

clippy_lints/src/loops/for_kv_map.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>, arg: &'tcx
3434
_ => arg,
3535
};
3636

37-
if ty.is_diag_item(cx, sym::HashMap) || ty.is_diag_item(cx, sym::BTreeMap) {
37+
if matches!(ty.opt_diag_name(cx), Some(sym::HashMap | sym::BTreeMap)) {
3838
span_lint_and_then(
3939
cx,
4040
FOR_KV_MAP,

clippy_lints/src/manual_let_else.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ fn pat_allowed_for_else(cx: &LateContext<'_>, pat: &'_ Pat<'_>, check_types: boo
374374
}
375375
let ty = typeck_results.pat_ty(pat);
376376
// Option and Result are allowed, everything else isn't.
377-
if !(ty.is_diag_item(cx, sym::Option) || ty.is_diag_item(cx, sym::Result)) {
377+
if !matches!(ty.opt_diag_name(cx), Some(sym::Option | sym::Result)) {
378378
has_disallowed = true;
379379
}
380380
});

clippy_lints/src/matches/match_wild_enum.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub(crate) fn check(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>]) {
1616
let ty = cx.typeck_results().expr_ty(ex).peel_refs();
1717
let adt_def = match ty.kind() {
1818
ty::Adt(adt_def, _)
19-
if adt_def.is_enum() && !(ty.is_diag_item(cx, sym::Option) || ty.is_diag_item(cx, sym::Result)) =>
19+
if adt_def.is_enum() && !matches!(ty.opt_diag_name(cx), Some(sym::Option | sym::Result)) =>
2020
{
2121
adt_def
2222
},

clippy_lints/src/methods/expect_fun_call.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,10 @@ pub(super) fn check<'tcx>(
2626
let arg_root = get_arg_root(cx, arg);
2727
if contains_call(cx, arg_root) && !contains_return(arg_root) {
2828
let receiver_type = cx.typeck_results().expr_ty_adjusted(receiver);
29-
let closure_args = if receiver_type.is_diag_item(cx, sym::Option) {
30-
"||"
31-
} else if receiver_type.is_diag_item(cx, sym::Result) {
32-
"|_|"
33-
} else {
34-
return;
29+
let closure_args = match receiver_type.opt_diag_name(cx) {
30+
Some(sym::Option) => "||",
31+
Some(sym::Result) => "|_|",
32+
_ => return,
3533
};
3634

3735
let span_replace_word = method_span.with_hi(expr.span.hi());

clippy_lints/src/methods/iter_count.rs

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,17 @@ use super::ITER_COUNT;
1111

1212
pub(crate) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, recv: &'tcx Expr<'tcx>, iter_method: Symbol) {
1313
let ty = cx.typeck_results().expr_ty(recv);
14-
let caller_type = if derefs_to_slice(cx, recv, ty).is_some() {
15-
"slice"
16-
} else if ty.is_diag_item(cx, sym::Vec) {
17-
"Vec"
18-
} else if ty.is_diag_item(cx, sym::VecDeque) {
19-
"VecDeque"
20-
} else if ty.is_diag_item(cx, sym::HashSet) {
21-
"HashSet"
22-
} else if ty.is_diag_item(cx, sym::HashMap) {
23-
"HashMap"
24-
} else if ty.is_diag_item(cx, sym::BTreeMap) {
25-
"BTreeMap"
26-
} else if ty.is_diag_item(cx, sym::BTreeSet) {
27-
"BTreeSet"
28-
} else if ty.is_diag_item(cx, sym::LinkedList) {
29-
"LinkedList"
30-
} else if ty.is_diag_item(cx, sym::BinaryHeap) {
31-
"BinaryHeap"
32-
} else {
33-
return;
14+
let caller_type = match ty.opt_diag_name(cx) {
15+
_ if derefs_to_slice(cx, recv, ty).is_some() => "slice",
16+
Some(sym::Vec) => "Vec",
17+
Some(sym::VecDeque) => "VecDeque",
18+
Some(sym::HashSet) => "HashSet",
19+
Some(sym::HashMap) => "HashMap",
20+
Some(sym::BTreeMap) => "BTreeMap",
21+
Some(sym::BTreeSet) => "BTreeSet",
22+
Some(sym::LinkedList) => "LinkedList",
23+
Some(sym::BinaryHeap) => "BinaryHeap",
24+
_ => return,
3425
};
3526
let mut applicability = Applicability::MachineApplicable;
3627
span_lint_and_sugg(

clippy_lints/src/methods/iter_kv_map.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub(super) fn check<'tcx>(
3838
_ => return,
3939
}
4040
&& let ty = cx.typeck_results().expr_ty_adjusted(recv).peel_refs()
41-
&& (ty.is_diag_item(cx, sym::HashMap) || ty.is_diag_item(cx, sym::BTreeMap))
41+
&& matches!(ty.opt_diag_name(cx), Some(sym::HashMap | sym::BTreeMap))
4242
{
4343
let mut applicability = rustc_errors::Applicability::MachineApplicable;
4444
let recv_snippet = snippet_with_applicability(cx, recv.span, "map", &mut applicability);

clippy_lints/src/methods/join_absolute_paths.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use super::JOIN_ABSOLUTE_PATHS;
1313

1414
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, recv: &'tcx Expr<'tcx>, join_arg: &'tcx Expr<'tcx>, expr_span: Span) {
1515
let ty = cx.typeck_results().expr_ty(recv).peel_refs();
16-
if (ty.is_diag_item(cx, sym::Path) || ty.is_diag_item(cx, sym::PathBuf))
16+
if matches!(ty.opt_diag_name(cx), Some(sym::Path | sym::PathBuf))
1717
&& let ExprKind::Lit(spanned) = expr_or_init(cx, join_arg).kind
1818
&& let LitKind::Str(symbol, _) = spanned.node
1919
&& let sym_str = symbol.as_str()

0 commit comments

Comments
 (0)