Skip to content

Commit 81b3e70

Browse files
committed
Auto merge of rust-lang#5449 - phansch:diagnostic-items, r=matthiaskrgr
Make use of more diagnostic items This makes use of some (not all) already existing diagnostic items. Specifically: * 79982a2: `core::mem::uninitialized`, `core::mem::zeroed`, `alloc::sync::Arc`, `alloc::sync::Rc` * 83874d0: `Option` and `Result` cc rust-lang#5393 changelog: none
2 parents 3c77188 + a524be6 commit 81b3e70

16 files changed

+64
-68
lines changed

clippy_lints/src/booleans.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::utils::{
2-
get_trait_def_id, implements_trait, in_macro, match_type, paths, snippet_opt, span_lint_and_sugg,
2+
get_trait_def_id, implements_trait, in_macro, is_type_diagnostic_item, paths, snippet_opt, span_lint_and_sugg,
33
span_lint_and_then, SpanlessEq,
44
};
55
use if_chain::if_chain;
@@ -249,7 +249,9 @@ fn simplify_not(cx: &LateContext<'_, '_>, expr: &Expr<'_>) -> Option<String> {
249249
},
250250
ExprKind::MethodCall(path, _, args) if args.len() == 1 => {
251251
let type_of_receiver = cx.tables.expr_ty(&args[0]);
252-
if !match_type(cx, type_of_receiver, &paths::OPTION) && !match_type(cx, type_of_receiver, &paths::RESULT) {
252+
if !is_type_diagnostic_item(cx, type_of_receiver, sym!(option_type))
253+
&& !is_type_diagnostic_item(cx, type_of_receiver, sym!(result_type))
254+
{
253255
return None;
254256
}
255257
METHODS_WITH_NEGATION

clippy_lints/src/cognitive_complexity.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_session::{declare_tool_lint, impl_lint_pass};
99
use rustc_span::source_map::Span;
1010
use rustc_span::BytePos;
1111

12-
use crate::utils::{match_type, paths, snippet_opt, span_lint_and_help, LimitStack};
12+
use crate::utils::{is_type_diagnostic_item, snippet_opt, span_lint_and_help, LimitStack};
1313

1414
declare_clippy_lint! {
1515
/// **What it does:** Checks for methods with high cognitive complexity.
@@ -61,7 +61,7 @@ impl CognitiveComplexity {
6161
helper.visit_expr(expr);
6262
let CCHelper { cc, returns } = helper;
6363
let ret_ty = cx.tables.node_type(expr.hir_id);
64-
let ret_adjust = if match_type(cx, ret_ty, &paths::RESULT) {
64+
let ret_adjust = if is_type_diagnostic_item(cx, ret_ty, sym!(result_type)) {
6565
returns
6666
} else {
6767
#[allow(clippy::integer_division)]

clippy_lints/src/doc.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::utils::{implements_trait, is_entrypoint_fn, match_type, paths, return_ty, span_lint};
1+
use crate::utils::{implements_trait, is_entrypoint_fn, is_type_diagnostic_item, return_ty, span_lint};
22
use if_chain::if_chain;
33
use itertools::Itertools;
44
use rustc_ast::ast::{AttrKind, Attribute};
@@ -217,7 +217,7 @@ fn lint_for_missing_headers<'a, 'tcx>(
217217
);
218218
}
219219
if !headers.errors {
220-
if match_type(cx, return_ty(cx, hir_id), &paths::RESULT) {
220+
if is_type_diagnostic_item(cx, return_ty(cx, hir_id), sym!(result_type)) {
221221
span_lint(
222222
cx,
223223
MISSING_ERRORS_DOC,
@@ -235,7 +235,7 @@ fn lint_for_missing_headers<'a, 'tcx>(
235235
if let ty::Opaque(_, subs) = ret_ty.kind;
236236
if let Some(gen) = subs.types().next();
237237
if let ty::Generator(_, subs, _) = gen.kind;
238-
if match_type(cx, subs.as_generator().return_ty(), &paths::RESULT);
238+
if is_type_diagnostic_item(cx, subs.as_generator().return_ty(), sym!(result_type));
239239
then {
240240
span_lint(
241241
cx,

clippy_lints/src/fallible_impl_from.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
use crate::utils::paths::{BEGIN_PANIC, BEGIN_PANIC_FMT, FROM_TRAIT, OPTION, RESULT};
2-
use crate::utils::{is_expn_of, match_def_path, method_chain_args, span_lint_and_then, walk_ptrs_ty};
1+
use crate::utils::paths::{BEGIN_PANIC, BEGIN_PANIC_FMT, FROM_TRAIT};
2+
use crate::utils::{
3+
is_expn_of, is_type_diagnostic_item, match_def_path, method_chain_args, span_lint_and_then, walk_ptrs_ty,
4+
};
35
use if_chain::if_chain;
46
use rustc_hir as hir;
57
use rustc_lint::{LateContext, LateLintPass};
68
use rustc_middle::hir::map::Map;
7-
use rustc_middle::ty::{self, Ty};
9+
use rustc_middle::ty;
810
use rustc_session::{declare_lint_pass, declare_tool_lint};
911
use rustc_span::Span;
1012

@@ -76,7 +78,9 @@ fn lint_impl_body<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, impl_span: Span, impl_it
7678
// check for `unwrap`
7779
if let Some(arglists) = method_chain_args(expr, &["unwrap"]) {
7880
let reciever_ty = walk_ptrs_ty(self.tables.expr_ty(&arglists[0][0]));
79-
if match_type(self.lcx, reciever_ty, &OPTION) || match_type(self.lcx, reciever_ty, &RESULT) {
81+
if is_type_diagnostic_item(self.lcx, reciever_ty, sym!(option_type))
82+
|| is_type_diagnostic_item(self.lcx, reciever_ty, sym!(result_type))
83+
{
8084
self.result.push(expr.span);
8185
}
8286
}
@@ -124,10 +128,3 @@ fn lint_impl_body<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, impl_span: Span, impl_it
124128
}
125129
}
126130
}
127-
128-
fn match_type(cx: &LateContext<'_, '_>, ty: Ty<'_>, path: &[&str]) -> bool {
129-
match ty.kind {
130-
ty::Adt(adt, _) => match_def_path(cx, adt.did, path),
131-
_ => false,
132-
}
133-
}

clippy_lints/src/if_let_some_result.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::utils::{match_type, method_chain_args, paths, snippet_with_applicability, span_lint_and_sugg};
1+
use crate::utils::{is_type_diagnostic_item, method_chain_args, snippet_with_applicability, span_lint_and_sugg};
22
use if_chain::if_chain;
33
use rustc_errors::Applicability;
44
use rustc_hir::{Expr, ExprKind, MatchSource, PatKind, QPath};
@@ -45,8 +45,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for OkIfLet {
4545
if let ExprKind::MethodCall(_, ok_span, ref result_types) = op.kind; //check is expr.ok() has type Result<T,E>.ok()
4646
if let PatKind::TupleStruct(QPath::Resolved(_, ref x), ref y, _) = body[0].pat.kind; //get operation
4747
if method_chain_args(op, &["ok"]).is_some(); //test to see if using ok() methoduse std::marker::Sized;
48-
let is_result_type = match_type(cx, cx.tables.expr_ty(&result_types[0]), &paths::RESULT);
49-
if rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_path(x, false)) == "Some" && is_result_type;
48+
if is_type_diagnostic_item(cx, cx.tables.expr_ty(&result_types[0]), sym!(result_type));
49+
if rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_path(x, false)) == "Some";
5050

5151
then {
5252
let mut applicability = Applicability::MachineApplicable;

clippy_lints/src/loops.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1392,7 +1392,7 @@ fn check_for_loop_arg(cx: &LateContext<'_, '_>, pat: &Pat<'_>, arg: &Expr<'_>, e
13921392
/// Checks for `for` loops over `Option`s and `Result`s.
13931393
fn check_arg_type(cx: &LateContext<'_, '_>, pat: &Pat<'_>, arg: &Expr<'_>) {
13941394
let ty = cx.tables.expr_ty(arg);
1395-
if match_type(cx, ty, &paths::OPTION) {
1395+
if is_type_diagnostic_item(cx, ty, sym!(option_type)) {
13961396
span_lint_and_help(
13971397
cx,
13981398
FOR_LOOP_OVER_OPTION,
@@ -1408,7 +1408,7 @@ fn check_arg_type(cx: &LateContext<'_, '_>, pat: &Pat<'_>, arg: &Expr<'_>) {
14081408
snippet(cx, arg.span, "_")
14091409
),
14101410
);
1411-
} else if match_type(cx, ty, &paths::RESULT) {
1411+
} else if is_type_diagnostic_item(cx, ty, sym!(result_type)) {
14121412
span_lint_and_help(
14131413
cx,
14141414
FOR_LOOP_OVER_RESULT,

clippy_lints/src/map_clone.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::utils::paths;
22
use crate::utils::{
3-
is_copy, match_trait_method, match_type, remove_blocks, snippet_with_applicability, span_lint_and_sugg,
3+
is_copy, is_type_diagnostic_item, match_trait_method, remove_blocks, snippet_with_applicability, span_lint_and_sugg,
44
};
55
use if_chain::if_chain;
66
use rustc_ast::ast::Ident;
@@ -52,7 +52,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MapClone {
5252
if args.len() == 2;
5353
if method.ident.as_str() == "map";
5454
let ty = cx.tables.expr_ty(&args[0]);
55-
if match_type(cx, ty, &paths::OPTION) || match_trait_method(cx, e, &paths::ITERATOR);
55+
if is_type_diagnostic_item(cx, ty, sym!(option_type)) || match_trait_method(cx, e, &paths::ITERATOR);
5656
if let hir::ExprKind::Closure(_, _, body_id, _, _) = args[1].kind;
5757
let closure_body = cx.tcx.hir().body(body_id);
5858
let closure_expr = remove_blocks(&closure_body.value);

clippy_lints/src/map_unit_fn.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use crate::utils::paths;
2-
use crate::utils::{iter_input_pats, match_type, method_chain_args, snippet, span_lint_and_then};
1+
use crate::utils::{is_type_diagnostic_item, iter_input_pats, method_chain_args, snippet, span_lint_and_then};
32
use if_chain::if_chain;
43
use rustc_errors::Applicability;
54
use rustc_hir as hir;
@@ -206,9 +205,9 @@ fn suggestion_msg(function_type: &str, map_type: &str) -> String {
206205
fn lint_map_unit_fn(cx: &LateContext<'_, '_>, stmt: &hir::Stmt<'_>, expr: &hir::Expr<'_>, map_args: &[hir::Expr<'_>]) {
207206
let var_arg = &map_args[0];
208207

209-
let (map_type, variant, lint) = if match_type(cx, cx.tables.expr_ty(var_arg), &paths::OPTION) {
208+
let (map_type, variant, lint) = if is_type_diagnostic_item(cx, cx.tables.expr_ty(var_arg), sym!(option_type)) {
210209
("Option", "Some", OPTION_MAP_UNIT_FN)
211-
} else if match_type(cx, cx.tables.expr_ty(var_arg), &paths::RESULT) {
210+
} else if is_type_diagnostic_item(cx, cx.tables.expr_ty(var_arg), sym!(result_type)) {
212211
("Result", "Ok", RESULT_MAP_UNIT_FN)
213212
} else {
214213
return;

clippy_lints/src/matches.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ use crate::utils::paths;
33
use crate::utils::sugg::Sugg;
44
use crate::utils::usage::is_unused;
55
use crate::utils::{
6-
expr_block, get_arg_name, get_parent_expr, in_macro, indent_of, is_allowed, is_expn_of, is_refutable, is_wild,
7-
match_qpath, match_type, match_var, multispan_sugg, remove_blocks, snippet, snippet_block,
8-
snippet_with_applicability, span_lint_and_help, span_lint_and_note, span_lint_and_sugg, span_lint_and_then,
9-
walk_ptrs_ty,
6+
expr_block, get_arg_name, get_parent_expr, in_macro, indent_of, is_allowed, is_expn_of, is_refutable,
7+
is_type_diagnostic_item, is_wild, match_qpath, match_type, match_var, multispan_sugg, remove_blocks, snippet,
8+
snippet_block, snippet_with_applicability, span_lint_and_help, span_lint_and_note, span_lint_and_sugg,
9+
span_lint_and_then, walk_ptrs_ty,
1010
};
1111
use if_chain::if_chain;
1212
use rustc_ast::ast::LitKind;
@@ -642,7 +642,7 @@ fn check_overlapping_arms<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, ex: &'tcx Expr<'
642642

643643
fn check_wild_err_arm(cx: &LateContext<'_, '_>, ex: &Expr<'_>, arms: &[Arm<'_>]) {
644644
let ex_ty = walk_ptrs_ty(cx.tables.expr_ty(ex));
645-
if match_type(cx, ex_ty, &paths::RESULT) {
645+
if is_type_diagnostic_item(cx, ex_ty, sym!(result_type)) {
646646
for arm in arms {
647647
if let PatKind::TupleStruct(ref path, ref inner, _) = arm.pat.kind {
648648
let path_str = rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_qpath(path, false));

clippy_lints/src/mem_replace.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use rustc_lint::{LateContext, LateLintPass};
99
use rustc_middle::lint::in_external_macro;
1010
use rustc_session::{declare_lint_pass, declare_tool_lint};
1111
use rustc_span::source_map::Span;
12+
use rustc_span::symbol::sym;
1213

1314
declare_clippy_lint! {
1415
/// **What it does:** Checks for `mem::replace()` on an `Option` with
@@ -141,15 +142,15 @@ fn check_replace_with_uninit(cx: &LateContext<'_, '_>, src: &Expr<'_>, expr_span
141142
if let ExprKind::Path(ref repl_func_qpath) = repl_func.kind;
142143
if let Some(repl_def_id) = cx.tables.qpath_res(repl_func_qpath, repl_func.hir_id).opt_def_id();
143144
then {
144-
if match_def_path(cx, repl_def_id, &paths::MEM_UNINITIALIZED) {
145+
if cx.tcx.is_diagnostic_item(sym::mem_uninitialized, repl_def_id) {
145146
span_lint_and_help(
146147
cx,
147148
MEM_REPLACE_WITH_UNINIT,
148149
expr_span,
149150
"replacing with `mem::uninitialized()`",
150151
"consider using the `take_mut` crate instead",
151152
);
152-
} else if match_def_path(cx, repl_def_id, &paths::MEM_ZEROED) &&
153+
} else if cx.tcx.is_diagnostic_item(sym::mem_zeroed, repl_def_id) &&
153154
!cx.tables.expr_ty(src).is_primitive() {
154155
span_lint_and_help(
155156
cx,

clippy_lints/src/methods/mod.rs

+16-19
Original file line numberDiff line numberDiff line change
@@ -1858,9 +1858,9 @@ fn lint_expect_fun_call(
18581858
}
18591859

18601860
let receiver_type = cx.tables.expr_ty_adjusted(&args[0]);
1861-
let closure_args = if match_type(cx, receiver_type, &paths::OPTION) {
1861+
let closure_args = if is_type_diagnostic_item(cx, receiver_type, sym!(option_type)) {
18621862
"||"
1863-
} else if match_type(cx, receiver_type, &paths::RESULT) {
1863+
} else if is_type_diagnostic_item(cx, receiver_type, sym!(result_type)) {
18641864
"|_|"
18651865
} else {
18661866
return;
@@ -2020,9 +2020,9 @@ fn lint_clone_on_ref_ptr(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, arg: &h
20202020
let obj_ty = walk_ptrs_ty(cx.tables.expr_ty(arg));
20212021

20222022
if let ty::Adt(_, subst) = obj_ty.kind {
2023-
let caller_type = if match_type(cx, obj_ty, &paths::RC) {
2023+
let caller_type = if is_type_diagnostic_item(cx, obj_ty, sym::Rc) {
20242024
"Rc"
2025-
} else if match_type(cx, obj_ty, &paths::ARC) {
2025+
} else if is_type_diagnostic_item(cx, obj_ty, sym::Arc) {
20262026
"Arc"
20272027
} else if match_type(cx, obj_ty, &paths::WEAK_RC) || match_type(cx, obj_ty, &paths::WEAK_ARC) {
20282028
"Weak"
@@ -2089,7 +2089,7 @@ fn lint_cstring_as_ptr(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, source: &
20892089
if_chain! {
20902090
let source_type = cx.tables.expr_ty(source);
20912091
if let ty::Adt(def, substs) = source_type.kind;
2092-
if match_def_path(cx, def.did, &paths::RESULT);
2092+
if cx.tcx.is_diagnostic_item(sym!(result_type), def.did);
20932093
if match_type(cx, substs.type_at(0), &paths::CSTRING);
20942094
then {
20952095
span_lint_and_then(
@@ -2417,9 +2417,9 @@ fn derefs_to_slice<'a, 'tcx>(
24172417
fn lint_unwrap(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, unwrap_args: &[hir::Expr<'_>]) {
24182418
let obj_ty = walk_ptrs_ty(cx.tables.expr_ty(&unwrap_args[0]));
24192419

2420-
let mess = if match_type(cx, obj_ty, &paths::OPTION) {
2420+
let mess = if is_type_diagnostic_item(cx, obj_ty, sym!(option_type)) {
24212421
Some((OPTION_UNWRAP_USED, "an Option", "None"))
2422-
} else if match_type(cx, obj_ty, &paths::RESULT) {
2422+
} else if is_type_diagnostic_item(cx, obj_ty, sym!(result_type)) {
24232423
Some((RESULT_UNWRAP_USED, "a Result", "Err"))
24242424
} else {
24252425
None
@@ -2444,9 +2444,9 @@ fn lint_unwrap(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, unwrap_args: &[hi
24442444
fn lint_expect(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, expect_args: &[hir::Expr<'_>]) {
24452445
let obj_ty = walk_ptrs_ty(cx.tables.expr_ty(&expect_args[0]));
24462446

2447-
let mess = if match_type(cx, obj_ty, &paths::OPTION) {
2447+
let mess = if is_type_diagnostic_item(cx, obj_ty, sym!(option_type)) {
24482448
Some((OPTION_EXPECT_USED, "an Option", "None"))
2449-
} else if match_type(cx, obj_ty, &paths::RESULT) {
2449+
} else if is_type_diagnostic_item(cx, obj_ty, sym!(result_type)) {
24502450
Some((RESULT_EXPECT_USED, "a Result", "Err"))
24512451
} else {
24522452
None
@@ -2467,7 +2467,7 @@ fn lint_expect(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, expect_args: &[hi
24672467
fn lint_ok_expect(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, ok_args: &[hir::Expr<'_>]) {
24682468
if_chain! {
24692469
// lint if the caller of `ok()` is a `Result`
2470-
if match_type(cx, cx.tables.expr_ty(&ok_args[0]), &paths::RESULT);
2470+
if is_type_diagnostic_item(cx, cx.tables.expr_ty(&ok_args[0]), sym!(result_type));
24712471
let result_type = cx.tables.expr_ty(&ok_args[0]);
24722472
if let Some(error_type) = get_error_type(cx, result_type);
24732473
if has_debug_impl(error_type, cx);
@@ -2513,8 +2513,8 @@ fn lint_map_unwrap_or_else<'a, 'tcx>(
25132513
unwrap_args: &'tcx [hir::Expr<'_>],
25142514
) {
25152515
// lint if the caller of `map()` is an `Option`
2516-
let is_option = match_type(cx, cx.tables.expr_ty(&map_args[0]), &paths::OPTION);
2517-
let is_result = match_type(cx, cx.tables.expr_ty(&map_args[0]), &paths::RESULT);
2516+
let is_option = is_type_diagnostic_item(cx, cx.tables.expr_ty(&map_args[0]), sym!(option_type));
2517+
let is_result = is_type_diagnostic_item(cx, cx.tables.expr_ty(&map_args[0]), sym!(result_type));
25182518

25192519
if is_option || is_result {
25202520
// Don't make a suggestion that may fail to compile due to mutably borrowing
@@ -2581,8 +2581,8 @@ fn lint_map_or_none<'a, 'tcx>(
25812581
expr: &'tcx hir::Expr<'_>,
25822582
map_or_args: &'tcx [hir::Expr<'_>],
25832583
) {
2584-
let is_option = match_type(cx, cx.tables.expr_ty(&map_or_args[0]), &paths::OPTION);
2585-
let is_result = match_type(cx, cx.tables.expr_ty(&map_or_args[0]), &paths::RESULT);
2584+
let is_option = is_type_diagnostic_item(cx, cx.tables.expr_ty(&map_or_args[0]), sym!(option_type));
2585+
let is_result = is_type_diagnostic_item(cx, cx.tables.expr_ty(&map_or_args[0]), sym!(result_type));
25862586

25872587
// There are two variants of this `map_or` lint:
25882588
// (1) using `map_or` as an adapter from `Result<T,E>` to `Option<T>`
@@ -3231,10 +3231,7 @@ fn is_maybe_uninit_ty_valid(cx: &LateContext<'_, '_>, ty: Ty<'_>) -> bool {
32313231
match ty.kind {
32323232
ty::Array(ref component, _) => is_maybe_uninit_ty_valid(cx, component),
32333233
ty::Tuple(ref types) => types.types().all(|ty| is_maybe_uninit_ty_valid(cx, ty)),
3234-
ty::Adt(ref adt, _) => {
3235-
// needs to be a MaybeUninit
3236-
match_def_path(cx, adt.did, &paths::MEM_MAYBEUNINIT)
3237-
},
3234+
ty::Adt(ref adt, _) => match_def_path(cx, adt.did, &paths::MEM_MAYBEUNINIT),
32383235
_ => false,
32393236
}
32403237
}
@@ -3348,7 +3345,7 @@ fn lint_option_as_ref_deref<'a, 'tcx>(
33483345
/// Given a `Result<T, E>` type, return its error type (`E`).
33493346
fn get_error_type<'a>(cx: &LateContext<'_, '_>, ty: Ty<'a>) -> Option<Ty<'a>> {
33503347
match ty.kind {
3351-
ty::Adt(_, substs) if match_type(cx, ty, &paths::RESULT) => substs.types().nth(1),
3348+
ty::Adt(_, substs) if is_type_diagnostic_item(cx, ty, sym!(result_type)) => substs.types().nth(1),
33523349
_ => None,
33533350
}
33543351
}

clippy_lints/src/methods/option_map_unwrap_or.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use crate::utils::{differing_macro_contexts, paths, snippet_with_applicability, span_lint_and_then};
2-
use crate::utils::{is_copy, match_type};
1+
use crate::utils::{differing_macro_contexts, snippet_with_applicability, span_lint_and_then};
2+
use crate::utils::{is_copy, is_type_diagnostic_item};
33
use rustc_data_structures::fx::FxHashSet;
44
use rustc_errors::Applicability;
55
use rustc_hir::intravisit::{walk_path, NestedVisitorMap, Visitor};
@@ -20,7 +20,7 @@ pub(super) fn lint<'a, 'tcx>(
2020
map_span: Span,
2121
) {
2222
// lint if the caller of `map()` is an `Option`
23-
if match_type(cx, cx.tables.expr_ty(&map_args[0]), &paths::OPTION) {
23+
if is_type_diagnostic_item(cx, cx.tables.expr_ty(&map_args[0]), sym!(option_type)) {
2424
if !is_copy(cx, cx.tables.expr_ty(&unwrap_args[1])) {
2525
// Do not lint if the `map` argument uses identifiers in the `map`
2626
// argument that are also used in the `unwrap_or` argument

clippy_lints/src/question_mark.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ use rustc_hir::{def, BindingAnnotation, Block, Expr, ExprKind, MatchSource, PatK
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_session::{declare_lint_pass, declare_tool_lint};
77

8-
use crate::utils::paths::{OPTION, OPTION_NONE};
98
use crate::utils::sugg::Sugg;
109
use crate::utils::{
11-
higher, match_def_path, match_qpath, match_type, snippet_with_applicability, span_lint_and_sugg, SpanlessEq,
10+
higher, is_type_diagnostic_item, match_def_path, match_qpath, paths, snippet_with_applicability,
11+
span_lint_and_sugg, SpanlessEq,
1212
};
1313

1414
declare_clippy_lint! {
@@ -141,7 +141,7 @@ impl QuestionMark {
141141
fn is_option(cx: &LateContext<'_, '_>, expression: &Expr<'_>) -> bool {
142142
let expr_ty = cx.tables.expr_ty(expression);
143143

144-
match_type(cx, expr_ty, &OPTION)
144+
is_type_diagnostic_item(cx, expr_ty, sym!(option_type))
145145
}
146146

147147
fn expression_returns_none(cx: &LateContext<'_, '_>, expression: &Expr<'_>) -> bool {
@@ -158,7 +158,7 @@ impl QuestionMark {
158158
if let Res::Def(DefKind::Ctor(def::CtorOf::Variant, def::CtorKind::Const), def_id) =
159159
cx.tables.qpath_res(qp, expression.hir_id)
160160
{
161-
return match_def_path(cx, def_id, &OPTION_NONE);
161+
return match_def_path(cx, def_id, &paths::OPTION_NONE);
162162
}
163163

164164
false

clippy_lints/src/types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ impl Types {
420420
return; // don't recurse into the type
421421
}
422422
}
423-
} else if match_def_path(cx, def_id, &paths::OPTION) {
423+
} else if cx.tcx.is_diagnostic_item(sym!(option_type), def_id) {
424424
if match_type_parameter(cx, qpath, &paths::OPTION).is_some() {
425425
span_lint(
426426
cx,

0 commit comments

Comments
 (0)