Skip to content

Commit 81b3e70

Browse files
committed
Auto merge of #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 #5393 changelog: none
2 parents 3c77188 + a524be6 commit 81b3e70

16 files changed

+64
-68
lines changed

clippy_lints/src/booleans.rs

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

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

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

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

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

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

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

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

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

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

0 commit comments

Comments
 (0)