Skip to content

Commit db40bd4

Browse files
committed
Remove all usages of match_path and match_qpath except the author lint.
1 parent db6ea84 commit db40bd4

37 files changed

+345
-387
lines changed

clippy_lints/src/if_then_some_else_none.rs

Lines changed: 5 additions & 3 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::source::snippet_with_macro_callsite;
3-
use clippy_utils::{match_qpath, meets_msrv, parent_node_is_if_expr};
3+
use clippy_utils::{match_def_path, meets_msrv, parent_node_is_if_expr};
44
use if_chain::if_chain;
55
use rustc_hir::{Expr, ExprKind};
66
use rustc_lint::{LateContext, LateLintPass, LintContext};
@@ -77,12 +77,14 @@ impl LateLintPass<'_> for IfThenSomeElseNone {
7777
if let Some(then_expr) = then_block.expr;
7878
if let ExprKind::Call(then_call, [then_arg]) = then_expr.kind;
7979
if let ExprKind::Path(ref then_call_qpath) = then_call.kind;
80-
if match_qpath(then_call_qpath, &clippy_utils::paths::OPTION_SOME);
80+
if let Some(then_call_did) = cx.qpath_res(then_call_qpath, then_expr.hir_id).opt_def_id();
81+
if match_def_path(cx, then_call_did, &clippy_utils::paths::OPTION_SOME);
8182
if let ExprKind::Block(els_block, _) = els.kind;
8283
if els_block.stmts.is_empty();
8384
if let Some(els_expr) = els_block.expr;
8485
if let ExprKind::Path(ref els_call_qpath) = els_expr.kind;
85-
if match_qpath(els_call_qpath, &clippy_utils::paths::OPTION_NONE);
86+
if let Some(els_call_did) = cx.qpath_res(els_call_qpath, els_expr.hir_id).opt_def_id();
87+
if match_def_path(cx, els_call_did, &clippy_utils::paths::OPTION_NONE);
8688
then {
8789
let cond_snip = snippet_with_macro_callsite(cx, cond.span, "[condition]");
8890
let cond_snip = if matches!(cond.kind, ExprKind::Unary(_, _) | ExprKind::Binary(_, _, _)) {

clippy_lints/src/implicit_hasher.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use clippy_utils::diagnostics::{multispan_sugg, span_lint_and_then};
2222
use clippy_utils::paths;
2323
use clippy_utils::source::{snippet, snippet_opt};
2424
use clippy_utils::ty::is_type_diagnostic_item;
25-
use clippy_utils::{differing_macro_contexts, match_path};
25+
use clippy_utils::{differing_macro_contexts, match_def_path};
2626

2727
declare_clippy_lint! {
2828
/// **What it does:** Checks for public `impl` or `fn` missing generalization
@@ -333,12 +333,13 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for ImplicitHasherConstructorVisitor<'a, 'b, 't
333333
if let ExprKind::Call(fun, args) = e.kind;
334334
if let ExprKind::Path(QPath::TypeRelative(ty, method)) = fun.kind;
335335
if let TyKind::Path(QPath::Resolved(None, ty_path)) = ty.kind;
336+
if let Some(ty_did) = ty_path.res.opt_def_id();
336337
then {
337338
if !TyS::same_type(self.target.ty(), self.maybe_typeck_results.unwrap().expr_ty(e)) {
338339
return;
339340
}
340341

341-
if match_path(ty_path, &paths::HASHMAP) {
342+
if match_def_path(self.cx, ty_did, &paths::HASHMAP) {
342343
if method.ident.name == sym::new {
343344
self.suggestions
344345
.insert(e.span, "HashMap::default()".to_string());
@@ -351,7 +352,7 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for ImplicitHasherConstructorVisitor<'a, 'b, 't
351352
),
352353
);
353354
}
354-
} else if match_path(ty_path, &paths::HASHSET) {
355+
} else if match_def_path(self.cx, ty_did, &paths::HASHSET) {
355356
if method.ident.name == sym::new {
356357
self.suggestions
357358
.insert(e.span, "HashSet::default()".to_string());

clippy_lints/src/implicit_saturating_sub.rs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::{in_macro, match_qpath, SpanlessEq};
2+
use clippy_utils::{in_macro, match_any_expr_path_res, match_any_qpath_res, paths, SpanlessEq};
33
use if_chain::if_chain;
44
use rustc_ast::ast::LitKind;
55
use rustc_errors::Applicability;
@@ -87,7 +87,6 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitSaturatingSub {
8787

8888
// Get the variable name
8989
let var_name = ares_path.segments[0].ident.name.as_str();
90-
const INT_TYPES: [&str; 5] = ["i8", "i16", "i32", "i64", "i128"];
9190

9291
match cond_num_val.kind {
9392
ExprKind::Lit(ref cond_lit) => {
@@ -100,16 +99,31 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitSaturatingSub {
10099
}
101100
},
102101
ExprKind::Path(ref cond_num_path) => {
103-
if INT_TYPES.iter().any(|int_type| match_qpath(cond_num_path, &[int_type, "MIN"])) {
102+
if match_any_qpath_res(cx, cond_num_path, cond_num_val.hir_id, &[
103+
&paths::I8_MIN,
104+
&paths::I16_MIN,
105+
&paths::I32_MIN,
106+
&paths::I64_MIN,
107+
&paths::ISIZE_MIN,
108+
&paths::I8_MOD_MIN,
109+
&paths::I16_MOD_MIN,
110+
&paths::I32_MOD_MIN,
111+
&paths::I64_MOD_MIN,
112+
&paths::ISIZE_MOD_MIN,
113+
]).is_some() {
104114
print_lint_and_sugg(cx, &var_name, expr);
105-
};
115+
}
106116
},
107-
ExprKind::Call(func, _) => {
108-
if let ExprKind::Path(ref cond_num_path) = func.kind {
109-
if INT_TYPES.iter().any(|int_type| match_qpath(cond_num_path, &[int_type, "min_value"])) {
110-
print_lint_and_sugg(cx, &var_name, expr);
111-
}
112-
};
117+
ExprKind::Call(func, []) => {
118+
if match_any_expr_path_res(cx, func, &[
119+
&paths::I8_MIN_VALUE,
120+
&paths::I16_MIN_VALUE,
121+
&paths::I32_MIN_VALUE,
122+
&paths::I64_MIN_VALUE,
123+
&paths::ISIZE_MIN_VALUE,
124+
]).is_some() {
125+
print_lint_and_sugg(cx, &var_name, expr);
126+
}
113127
},
114128
_ => (),
115129
}

clippy_lints/src/infinite_iter.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;
22
use clippy_utils::ty::{implements_trait, match_type};
3-
use clippy_utils::{get_trait_def_id, higher, match_qpath, paths};
3+
use clippy_utils::{get_trait_def_id, higher, match_qpath_res, paths};
44
use rustc_hir::{BorrowKind, Expr, ExprKind};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -163,7 +163,7 @@ fn is_infinite(cx: &LateContext<'_>, expr: &Expr<'_>) -> Finiteness {
163163
ExprKind::Box(e) | ExprKind::AddrOf(BorrowKind::Ref, _, e) => is_infinite(cx, e),
164164
ExprKind::Call(path, _) => {
165165
if let ExprKind::Path(ref qpath) = path.kind {
166-
match_qpath(qpath, &paths::REPEAT).into()
166+
match_qpath_res(cx, qpath, path.hir_id, &paths::ITER_REPEAT).into()
167167
} else {
168168
Finite
169169
}

clippy_lints/src/manual_ok_or.rs

Lines changed: 7 additions & 9 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::source::{indent_of, reindent_multiline, snippet_opt};
33
use clippy_utils::ty::is_type_diagnostic_item;
4-
use clippy_utils::{match_qpath, path_to_local_id, paths};
4+
use clippy_utils::{match_expr_path_res, path_to_local_id, paths};
55
use if_chain::if_chain;
66
use rustc_errors::Applicability;
77
use rustc_hir::{Expr, ExprKind, PatKind};
@@ -53,8 +53,8 @@ impl LateLintPass<'_> for ManualOkOr {
5353
if is_type_diagnostic_item(cx, ty, sym::option_type);
5454
let or_expr = &args[1];
5555
if is_ok_wrapping(cx, &args[2]);
56-
if let ExprKind::Call(Expr { kind: ExprKind::Path(err_path), .. }, &[ref err_arg]) = or_expr.kind;
57-
if match_qpath(err_path, &paths::RESULT_ERR);
56+
if let ExprKind::Call(err_expr, &[ref err_arg]) = or_expr.kind;
57+
if match_expr_path_res(cx, err_expr, &paths::RESULT_ERR);
5858
if let Some(method_receiver_snippet) = snippet_opt(cx, method_receiver.span);
5959
if let Some(err_arg_snippet) = snippet_opt(cx, err_arg.span);
6060
if let Some(indent) = indent_of(cx, scrutinee.span);
@@ -80,17 +80,15 @@ impl LateLintPass<'_> for ManualOkOr {
8080
}
8181

8282
fn is_ok_wrapping(cx: &LateContext<'_>, map_expr: &Expr<'_>) -> bool {
83-
if let ExprKind::Path(ref qpath) = map_expr.kind {
84-
if match_qpath(qpath, &paths::RESULT_OK) {
85-
return true;
86-
}
83+
if match_expr_path_res(cx, map_expr, &paths::RESULT_OK) {
84+
return true;
8785
}
8886
if_chain! {
8987
if let ExprKind::Closure(_, _, body_id, ..) = map_expr.kind;
9088
let body = cx.tcx.hir().body(body_id);
9189
if let PatKind::Binding(_, param_id, ..) = body.params[0].pat.kind;
92-
if let ExprKind::Call(Expr { kind: ExprKind::Path(ok_path), .. }, &[ref ok_arg]) = body.value.kind;
93-
if match_qpath(ok_path, &paths::RESULT_OK);
90+
if let ExprKind::Call(ok_expr, &[ref ok_arg]) = body.value.kind;
91+
if match_expr_path_res(cx, ok_expr, &paths::RESULT_OK);
9492
then { path_to_local_id(ok_arg, param_id) } else { false }
9593
}
9694
}

clippy_lints/src/manual_unwrap_or.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
33
use clippy_utils::source::{indent_of, reindent_multiline, snippet_opt};
44
use clippy_utils::ty::is_type_diagnostic_item;
55
use clippy_utils::usage::contains_return_break_continue_macro;
6-
use clippy_utils::{in_constant, match_qpath, path_to_local_id, paths, sugg};
6+
use clippy_utils::{in_constant, match_qpath_res, path_to_local_id, paths, sugg};
77
use if_chain::if_chain;
88
use rustc_errors::Applicability;
99
use rustc_hir::{Arm, Expr, ExprKind, Pat, PatKind};
@@ -68,23 +68,23 @@ impl Case {
6868
}
6969

7070
fn lint_manual_unwrap_or<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
71-
fn applicable_or_arm<'a>(arms: &'a [Arm<'a>]) -> Option<&'a Arm<'a>> {
71+
fn applicable_or_arm<'a>(cx: &LateContext<'_>, arms: &'a [Arm<'a>]) -> Option<&'a Arm<'a>> {
7272
if_chain! {
7373
if arms.len() == 2;
7474
if arms.iter().all(|arm| arm.guard.is_none());
7575
if let Some((idx, or_arm)) = arms.iter().enumerate().find(|(_, arm)|
7676
match arm.pat.kind {
7777
PatKind::Path(ref some_qpath) =>
78-
match_qpath(some_qpath, &paths::OPTION_NONE),
78+
match_qpath_res(cx, some_qpath, arm.pat.hir_id, &paths::OPTION_NONE),
7979
PatKind::TupleStruct(ref err_qpath, &[Pat { kind: PatKind::Wild, .. }], _) =>
80-
match_qpath(err_qpath, &paths::RESULT_ERR),
80+
match_qpath_res(cx, err_qpath, arm.pat.hir_id, &paths::RESULT_ERR),
8181
_ => false,
8282
}
8383
);
8484
let unwrap_arm = &arms[1 - idx];
8585
if let PatKind::TupleStruct(ref unwrap_qpath, &[unwrap_pat], _) = unwrap_arm.pat.kind;
86-
if match_qpath(unwrap_qpath, &paths::OPTION_SOME)
87-
|| match_qpath(unwrap_qpath, &paths::RESULT_OK);
86+
if match_qpath_res(cx, unwrap_qpath, unwrap_arm.hir_id, &paths::OPTION_SOME)
87+
|| match_qpath_res(cx, unwrap_qpath, unwrap_arm.hir_id, &paths::RESULT_OK);
8888
if let PatKind::Binding(_, binding_hir_id, ..) = unwrap_pat.kind;
8989
if path_to_local_id(unwrap_arm.body, binding_hir_id);
9090
if !contains_return_break_continue_macro(or_arm.body);
@@ -106,7 +106,7 @@ fn lint_manual_unwrap_or<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
106106
} else {
107107
None
108108
};
109-
if let Some(or_arm) = applicable_or_arm(match_arms);
109+
if let Some(or_arm) = applicable_or_arm(cx, match_arms);
110110
if let Some(or_body_snippet) = snippet_opt(cx, or_arm.body.span);
111111
if let Some(indent) = indent_of(cx, expr.span);
112112
if constant_simple(cx, cx.typeck_results(), or_arm.body).is_some();

clippy_lints/src/map_identity.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::ty::is_type_diagnostic_item;
3-
use clippy_utils::{is_adjusted, is_trait_method, match_path, match_var, paths, remove_blocks};
3+
use clippy_utils::{is_adjusted, is_trait_method, match_def_path, match_var, paths, remove_blocks};
44
use if_chain::if_chain;
55
use rustc_errors::Applicability;
66
use rustc_hir::{Body, Expr, ExprKind, Pat, PatKind, QPath, StmtKind};
@@ -80,7 +80,10 @@ fn get_map_argument<'a>(cx: &LateContext<'_>, expr: &'a Expr<'a>) -> Option<&'a
8080
fn is_expr_identity_function(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
8181
match expr.kind {
8282
ExprKind::Closure(_, _, body_id, _, _) => is_body_identity_function(cx, cx.tcx.hir().body(body_id)),
83-
ExprKind::Path(QPath::Resolved(_, path)) => match_path(path, &paths::STD_CONVERT_IDENTITY),
83+
ExprKind::Path(QPath::Resolved(_, path)) => path
84+
.res
85+
.opt_def_id()
86+
.map_or(false, |id| match_def_path(cx, id, &paths::CONVERT_IDENTITY)),
8487
_ => false,
8588
}
8689
}

0 commit comments

Comments
 (0)