Skip to content

Commit

Permalink
Remove all usages of match_path and match_qpath except the `autho…
Browse files Browse the repository at this point in the history
…r` lint.
  • Loading branch information
Jarcho committed Apr 7, 2021
1 parent db6ea84 commit b632c92
Show file tree
Hide file tree
Showing 37 changed files with 344 additions and 387 deletions.
8 changes: 5 additions & 3 deletions clippy_lints/src/if_then_some_else_none.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::source::snippet_with_macro_callsite;
use clippy_utils::{match_qpath, meets_msrv, parent_node_is_if_expr};
use clippy_utils::{match_def_path, meets_msrv, parent_node_is_if_expr};
use if_chain::if_chain;
use rustc_hir::{Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass, LintContext};
Expand Down Expand Up @@ -77,12 +77,14 @@ impl LateLintPass<'_> for IfThenSomeElseNone {
if let Some(then_expr) = then_block.expr;
if let ExprKind::Call(then_call, [then_arg]) = then_expr.kind;
if let ExprKind::Path(ref then_call_qpath) = then_call.kind;
if match_qpath(then_call_qpath, &clippy_utils::paths::OPTION_SOME);
if let Some(then_call_did) = cx.qpath_res(then_call_qpath, then_expr.hir_id).opt_def_id();
if match_def_path(cx, then_call_did, &clippy_utils::paths::OPTION_SOME);
if let ExprKind::Block(els_block, _) = els.kind;
if els_block.stmts.is_empty();
if let Some(els_expr) = els_block.expr;
if let ExprKind::Path(ref els_call_qpath) = els_expr.kind;
if match_qpath(els_call_qpath, &clippy_utils::paths::OPTION_NONE);
if let Some(els_call_did) = cx.qpath_res(els_call_qpath, els_expr.hir_id).opt_def_id();
if match_def_path(cx, els_call_did, &clippy_utils::paths::OPTION_NONE);
then {
let cond_snip = snippet_with_macro_callsite(cx, cond.span, "[condition]");
let cond_snip = if matches!(cond.kind, ExprKind::Unary(_, _) | ExprKind::Binary(_, _, _)) {
Expand Down
7 changes: 4 additions & 3 deletions clippy_lints/src/implicit_hasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use clippy_utils::diagnostics::{multispan_sugg, span_lint_and_then};
use clippy_utils::paths;
use clippy_utils::source::{snippet, snippet_opt};
use clippy_utils::ty::is_type_diagnostic_item;
use clippy_utils::{differing_macro_contexts, match_path};
use clippy_utils::{differing_macro_contexts, match_def_path};

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

if match_path(ty_path, &paths::HASHMAP) {
if match_def_path(self.cx, ty_did, &paths::HASHMAP) {
if method.ident.name == sym::new {
self.suggestions
.insert(e.span, "HashMap::default()".to_string());
Expand All @@ -351,7 +352,7 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for ImplicitHasherConstructorVisitor<'a, 'b, 't
),
);
}
} else if match_path(ty_path, &paths::HASHSET) {
} else if match_def_path(self.cx, ty_did, &paths::HASHSET) {
if method.ident.name == sym::new {
self.suggestions
.insert(e.span, "HashSet::default()".to_string());
Expand Down
34 changes: 24 additions & 10 deletions clippy_lints/src/implicit_saturating_sub.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::{in_macro, match_qpath, SpanlessEq};
use clippy_utils::{in_macro, match_any_expr_path_res, match_any_qpath_res, paths, SpanlessEq};
use if_chain::if_chain;
use rustc_ast::ast::LitKind;
use rustc_errors::Applicability;
Expand Down Expand Up @@ -87,7 +87,6 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitSaturatingSub {

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

match cond_num_val.kind {
ExprKind::Lit(ref cond_lit) => {
Expand All @@ -100,16 +99,31 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitSaturatingSub {
}
},
ExprKind::Path(ref cond_num_path) => {
if INT_TYPES.iter().any(|int_type| match_qpath(cond_num_path, &[int_type, "MIN"])) {
if match_any_qpath_res(cx, cond_num_path, cond_num_val.hir_id, &[
&paths::I8_MIN,
&paths::I16_MIN,
&paths::I32_MIN,
&paths::I64_MIN,
&paths::ISIZE_MIN,
&paths::I8_MOD_MIN,
&paths::I16_MOD_MIN,
&paths::I32_MOD_MIN,
&paths::I64_MOD_MIN,
&paths::ISIZE_MOD_MIN,
]).is_some() {
print_lint_and_sugg(cx, &var_name, expr);
};
}
},
ExprKind::Call(func, _) => {
if let ExprKind::Path(ref cond_num_path) = func.kind {
if INT_TYPES.iter().any(|int_type| match_qpath(cond_num_path, &[int_type, "min_value"])) {
print_lint_and_sugg(cx, &var_name, expr);
}
};
ExprKind::Call(func, []) => {
if match_any_expr_path_res(cx, func, &[
&paths::I8_MIN_VALUE,
&paths::I16_MIN_VALUE,
&paths::I32_MIN_VALUE,
&paths::I64_MIN_VALUE,
&paths::ISIZE_MIN_VALUE,
]).is_some() {
print_lint_and_sugg(cx, &var_name, expr);
}
},
_ => (),
}
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/infinite_iter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clippy_utils::diagnostics::span_lint;
use clippy_utils::ty::{implements_trait, match_type};
use clippy_utils::{get_trait_def_id, higher, match_qpath, paths};
use clippy_utils::{get_trait_def_id, higher, match_qpath_res, paths};
use rustc_hir::{BorrowKind, Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
Expand Down Expand Up @@ -163,7 +163,7 @@ fn is_infinite(cx: &LateContext<'_>, expr: &Expr<'_>) -> Finiteness {
ExprKind::Box(e) | ExprKind::AddrOf(BorrowKind::Ref, _, e) => is_infinite(cx, e),
ExprKind::Call(path, _) => {
if let ExprKind::Path(ref qpath) = path.kind {
match_qpath(qpath, &paths::REPEAT).into()
match_qpath_res(cx, qpath, path.hir_id, &paths::ITER_REPEAT).into()
} else {
Finite
}
Expand Down
16 changes: 7 additions & 9 deletions clippy_lints/src/manual_ok_or.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::{indent_of, reindent_multiline, snippet_opt};
use clippy_utils::ty::is_type_diagnostic_item;
use clippy_utils::{match_qpath, path_to_local_id, paths};
use clippy_utils::{match_expr_path_res, path_to_local_id, paths};
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind, PatKind};
Expand Down Expand Up @@ -53,8 +53,8 @@ impl LateLintPass<'_> for ManualOkOr {
if is_type_diagnostic_item(cx, ty, sym::option_type);
let or_expr = &args[1];
if is_ok_wrapping(cx, &args[2]);
if let ExprKind::Call(Expr { kind: ExprKind::Path(err_path), .. }, &[ref err_arg]) = or_expr.kind;
if match_qpath(err_path, &paths::RESULT_ERR);
if let ExprKind::Call(err_expr, &[ref err_arg]) = or_expr.kind;
if match_expr_path_res(cx, err_expr, &paths::RESULT_ERR);
if let Some(method_receiver_snippet) = snippet_opt(cx, method_receiver.span);
if let Some(err_arg_snippet) = snippet_opt(cx, err_arg.span);
if let Some(indent) = indent_of(cx, scrutinee.span);
Expand All @@ -80,17 +80,15 @@ impl LateLintPass<'_> for ManualOkOr {
}

fn is_ok_wrapping(cx: &LateContext<'_>, map_expr: &Expr<'_>) -> bool {
if let ExprKind::Path(ref qpath) = map_expr.kind {
if match_qpath(qpath, &paths::RESULT_OK) {
return true;
}
if match_expr_path_res(cx, map_expr, &paths::RESULT_OK) {
return true;
}
if_chain! {
if let ExprKind::Closure(_, _, body_id, ..) = map_expr.kind;
let body = cx.tcx.hir().body(body_id);
if let PatKind::Binding(_, param_id, ..) = body.params[0].pat.kind;
if let ExprKind::Call(Expr { kind: ExprKind::Path(ok_path), .. }, &[ref ok_arg]) = body.value.kind;
if match_qpath(ok_path, &paths::RESULT_OK);
if let ExprKind::Call(ok_expr, &[ref ok_arg]) = body.value.kind;
if match_expr_path_res(cx, ok_expr, &paths::RESULT_OK);
then { path_to_local_id(ok_arg, param_id) } else { false }
}
}
14 changes: 7 additions & 7 deletions clippy_lints/src/manual_unwrap_or.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::{indent_of, reindent_multiline, snippet_opt};
use clippy_utils::ty::is_type_diagnostic_item;
use clippy_utils::usage::contains_return_break_continue_macro;
use clippy_utils::{in_constant, match_qpath, path_to_local_id, paths, sugg};
use clippy_utils::{in_constant, match_qpath_res, path_to_local_id, paths, sugg};
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::{Arm, Expr, ExprKind, Pat, PatKind};
Expand Down Expand Up @@ -68,23 +68,23 @@ impl Case {
}

fn lint_manual_unwrap_or<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
fn applicable_or_arm<'a>(arms: &'a [Arm<'a>]) -> Option<&'a Arm<'a>> {
fn applicable_or_arm<'a>(cx: &LateContext<'_>, arms: &'a [Arm<'a>]) -> Option<&'a Arm<'a>> {
if_chain! {
if arms.len() == 2;
if arms.iter().all(|arm| arm.guard.is_none());
if let Some((idx, or_arm)) = arms.iter().enumerate().find(|(_, arm)|
match arm.pat.kind {
PatKind::Path(ref some_qpath) =>
match_qpath(some_qpath, &paths::OPTION_NONE),
match_qpath_res(cx, some_qpath, arm.pat.hir_id, &paths::OPTION_NONE),
PatKind::TupleStruct(ref err_qpath, &[Pat { kind: PatKind::Wild, .. }], _) =>
match_qpath(err_qpath, &paths::RESULT_ERR),
match_qpath_res(cx, err_qpath, arm.pat.hir_id, &paths::RESULT_ERR),
_ => false,
}
);
let unwrap_arm = &arms[1 - idx];
if let PatKind::TupleStruct(ref unwrap_qpath, &[unwrap_pat], _) = unwrap_arm.pat.kind;
if match_qpath(unwrap_qpath, &paths::OPTION_SOME)
|| match_qpath(unwrap_qpath, &paths::RESULT_OK);
if match_qpath_res(cx, unwrap_qpath, unwrap_arm.hir_id, &paths::OPTION_SOME)
|| match_qpath_res(cx, unwrap_qpath, unwrap_arm.hir_id, &paths::RESULT_OK);
if let PatKind::Binding(_, binding_hir_id, ..) = unwrap_pat.kind;
if path_to_local_id(unwrap_arm.body, binding_hir_id);
if !contains_return_break_continue_macro(or_arm.body);
Expand All @@ -106,7 +106,7 @@ fn lint_manual_unwrap_or<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
} else {
None
};
if let Some(or_arm) = applicable_or_arm(match_arms);
if let Some(or_arm) = applicable_or_arm(cx, match_arms);
if let Some(or_body_snippet) = snippet_opt(cx, or_arm.body.span);
if let Some(indent) = indent_of(cx, expr.span);
if constant_simple(cx, cx.typeck_results(), or_arm.body).is_some();
Expand Down
7 changes: 5 additions & 2 deletions clippy_lints/src/map_identity.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::ty::is_type_diagnostic_item;
use clippy_utils::{is_adjusted, is_trait_method, match_path, match_var, paths, remove_blocks};
use clippy_utils::{is_adjusted, is_trait_method, match_def_path, match_var, paths, remove_blocks};
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::{Body, Expr, ExprKind, Pat, PatKind, QPath, StmtKind};
Expand Down Expand Up @@ -80,7 +80,10 @@ fn get_map_argument<'a>(cx: &LateContext<'_>, expr: &'a Expr<'a>) -> Option<&'a
fn is_expr_identity_function(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
match expr.kind {
ExprKind::Closure(_, _, body_id, _, _) => is_body_identity_function(cx, cx.tcx.hir().body(body_id)),
ExprKind::Path(QPath::Resolved(_, path)) => match_path(path, &paths::STD_CONVERT_IDENTITY),
ExprKind::Path(QPath::Resolved(_, path)) => path
.res
.opt_def_id()
.map_or(false, |id| match_def_path(cx, id, &paths::CONVERT_IDENTITY)),
_ => false,
}
}
Expand Down
Loading

0 comments on commit b632c92

Please sign in to comment.