Skip to content

Commit

Permalink
Remove match_type
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarcho committed Sep 8, 2021
1 parent d3c027e commit dd8070d
Show file tree
Hide file tree
Showing 24 changed files with 65 additions and 156 deletions.
5 changes: 2 additions & 3 deletions clippy_lints/src/bytecount.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::ty::match_type;
use clippy_utils::visitors::is_local_used;
use clippy_utils::{path_to_local_id, paths, peel_ref_operators, remove_blocks, strip_pat_refs};
use clippy_utils::{is_item, path_to_local_id, paths, peel_ref_operators, remove_blocks, strip_pat_refs};
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::{BinOpKind, Expr, ExprKind, PatKind};
Expand Down Expand Up @@ -50,7 +49,7 @@ impl<'tcx> LateLintPass<'tcx> for ByteCount {
if let PatKind::Binding(_, arg_id, _, _) = strip_pat_refs(param.pat).kind;
if let ExprKind::Binary(ref op, l, r) = body.value.kind;
if op.node == BinOpKind::Eq;
if match_type(cx,
if is_item(cx,
cx.typeck_results().expr_ty(filter_recv).peel_refs(),
&paths::SLICE_ITER);
let operand_is_arg = |expr| {
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/duration_subsec.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clippy_utils::is_item;
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::ty::match_type;
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::{BinOpKind, Expr, ExprKind};
Expand Down Expand Up @@ -45,7 +45,7 @@ impl<'tcx> LateLintPass<'tcx> for DurationSubsec {
if_chain! {
if let ExprKind::Binary(Spanned { node: BinOpKind::Div, .. }, left, right) = expr.kind;
if let ExprKind::MethodCall(method_path, _ , args, _) = left.kind;
if match_type(cx, cx.typeck_results().expr_ty(&args[0]).peel_refs(), &paths::DURATION);
if is_item(cx, cx.typeck_results().expr_ty(&args[0]).peel_refs(), &paths::DURATION);
if let Some((Constant::Int(divisor), _)) = constant(cx, cx.typeck_results(), right);
then {
let suggested_fn = match (method_path.ident.as_str().as_ref(), divisor) {
Expand Down
9 changes: 3 additions & 6 deletions clippy_lints/src/let_underscore.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::ty::{is_must_use_ty, match_type};
use clippy_utils::{is_must_use_func_call, paths};
use clippy_utils::ty::is_must_use_ty;
use clippy_utils::{is_any_item, is_must_use_func_call, paths};
use if_chain::if_chain;
use rustc_hir::{Local, PatKind};
use rustc_lint::{LateContext, LateLintPass};
Expand Down Expand Up @@ -120,10 +120,7 @@ impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
then {
let init_ty = cx.typeck_results().expr_ty(init);
let contains_sync_guard = init_ty.walk(cx.tcx).any(|inner| match inner.unpack() {
GenericArgKind::Type(inner_ty) => {
SYNC_GUARD_PATHS.iter().any(|path| match_type(cx, inner_ty, path))
},

GenericArgKind::Type(inner_ty) => is_any_item(cx, inner_ty, &SYNC_GUARD_PATHS).is_some(),
GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => false,
});
if contains_sync_guard {
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/map_clone.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::{is_trait_method, remove_blocks, is_item};
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::ty::{is_copy, };
use clippy_utils::ty::is_copy;
use clippy_utils::{is_item, is_trait_method, remove_blocks};
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir as hir;
Expand Down
9 changes: 4 additions & 5 deletions clippy_lints/src/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use clippy_utils::diagnostics::{
use clippy_utils::higher;
use clippy_utils::source::{expr_block, indent_of, snippet, snippet_block, snippet_opt, snippet_with_applicability};
use clippy_utils::sugg::Sugg;
use clippy_utils::ty::{implements_trait, match_type, peel_mid_ty_refs};
use clippy_utils::ty::{implements_trait, peel_mid_ty_refs};
use clippy_utils::visitors::is_local_used;
use clippy_utils::{
get_parent_expr, in_macro, is_any_item, is_expn_of, is_item, is_lang_ctor, is_lint_allowed, is_refutable,
Expand Down Expand Up @@ -857,7 +857,7 @@ fn check_single_match_opt_like(
};

for &(ty_path, pat_path) in candidates {
if path == *pat_path && match_type(cx, ty, ty_path) {
if path == *pat_path && is_item(cx, ty, ty_path) {
report_single_match_single_pattern(cx, ex, arms, expr, els);
}
}
Expand Down Expand Up @@ -1802,7 +1802,7 @@ mod redundant_pattern_match {
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::higher;
use clippy_utils::source::{snippet, snippet_with_applicability};
use clippy_utils::ty::{implements_trait, match_type};
use clippy_utils::ty::implements_trait;
use clippy_utils::{is_any_item, is_item, is_lang_ctor, is_trait_method, paths};
use if_chain::if_chain;
use rustc_ast::ast::LitKind;
Expand Down Expand Up @@ -1881,8 +1881,7 @@ mod redundant_pattern_match {
],
)
.is_some()
|| match_type(cx, ty, &paths::WEAK_RC)
|| match_type(cx, ty, &paths::WEAK_ARC)
|| is_any_item(cx, ty, &[paths::WEAK_RC.as_slice(), paths::WEAK_ARC.as_slice()]).is_some()
{
// Check all of the generic arguments.
if let ty::Adt(_, subs) = ty.kind() {
Expand Down
3 changes: 1 addition & 2 deletions clippy_lints/src/methods/clone_on_ref_ptr.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::snippet_with_macro_callsite;
use clippy_utils::ty::match_type;
use clippy_utils::{is_any_item, paths};
use rustc_errors::Applicability;
use rustc_hir as hir;
Expand All @@ -21,7 +20,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, method_name: Sym
let caller_type = match is_any_item(cx, adt.did, &[sym::Rc, sym::Arc]) {
Some(0) => "Rc",
Some(1) => "Arc",
_ if match_type(cx, obj_ty, &paths::WEAK_RC) || match_type(cx, obj_ty, &paths::WEAK_ARC) => "Weak",
_ if is_any_item(cx, obj_ty, &[paths::WEAK_RC.as_slice(), paths::WEAK_ARC.as_slice()]).is_some() => "Weak",
_ => return,
};

Expand Down
5 changes: 2 additions & 3 deletions clippy_lints/src/methods/filetype_is_file.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::ty::match_type;
use clippy_utils::{get_parent_expr, paths};
use clippy_utils::{get_parent_expr, is_item, paths};
use if_chain::if_chain;
use rustc_hir as hir;
use rustc_lint::LateContext;
Expand All @@ -11,7 +10,7 @@ use super::FILETYPE_IS_FILE;
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>) {
let ty = cx.typeck_results().expr_ty(recv);

if !match_type(cx, ty, &paths::FILE_TYPE) {
if !is_item(cx, ty, &paths::FILE_TYPE) {
return;
}

Expand Down
3 changes: 1 addition & 2 deletions clippy_lints/src/methods/manual_str_repeat.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::{snippet_with_applicability, snippet_with_context};
use clippy_utils::sugg::Sugg;
use clippy_utils::ty::{match_type};
use clippy_utils::{is_item, paths};
use if_chain::if_chain;
use rustc_ast::LitKind;
Expand Down Expand Up @@ -39,7 +38,7 @@ fn parse_repeat_arg(cx: &LateContext<'_>, e: &Expr<'_>) -> Option<RepeatKind> {
(ty.is_str()
|| is_item(cx, ty, sym::string_type)
|| (is_item(cx, ty, LangItem::OwnedBox) && get_ty_param(ty).map_or(false, TyS::is_str))
|| (match_type(cx, ty, &paths::COW) && get_ty_param(ty).map_or(false, TyS::is_str)))
|| (is_item(cx, ty, &paths::COW) && get_ty_param(ty).map_or(false, TyS::is_str)))
.then(|| RepeatKind::String)
}
}
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/methods/or_fun_call.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::eager_or_lazy::is_lazyness_candidate;
use clippy_utils::source::{snippet, snippet_with_applicability, snippet_with_macro_callsite};
use clippy_utils::ty::{implements_trait, match_type};
use clippy_utils::ty::implements_trait;
use clippy_utils::{contains_return, last_path_segment, paths};
use clippy_utils::{is_item, is_trait_item};
use if_chain::if_chain;
Expand Down Expand Up @@ -119,7 +119,7 @@ pub(super) fn check<'tcx>(
let self_ty = cx.typeck_results().expr_ty(self_expr);

if let Some(&(_, fn_has_arguments, poss, suffix)) =
KNOW_TYPES.iter().find(|&&i| match_type(cx, self_ty, i.0));
KNOW_TYPES.iter().find(|&&i| is_item(cx, self_ty, i.0));

if poss.contains(&name);

Expand Down
12 changes: 7 additions & 5 deletions clippy_lints/src/non_octal_unix_permissions.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::{snippet_opt, snippet_with_applicability};
use clippy_utils::ty::match_type;
use clippy_utils::{is_item, paths};
use clippy_utils::{is_any_item, is_item, paths};
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind};
Expand Down Expand Up @@ -47,9 +46,12 @@ impl LateLintPass<'_> for NonOctalUnixPermissions {

if_chain! {
if (path.ident.name == sym!(mode)
&& (match_type(cx, obj_ty, &paths::OPEN_OPTIONS)
|| match_type(cx, obj_ty, &paths::DIR_BUILDER)))
|| (path.ident.name == sym!(set_mode) && match_type(cx, obj_ty, &paths::PERMISSIONS));
&& is_any_item(
cx,
obj_ty,
&[paths::OPEN_OPTIONS.as_slice(), paths::DIR_BUILDER.as_slice()]
).is_some())
|| (path.ident.name == sym!(set_mode) && is_item(cx, obj_ty, &paths::PERMISSIONS));
if let ExprKind::Lit(_) = param.kind;

then {
Expand Down
15 changes: 7 additions & 8 deletions clippy_lints/src/open_options.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use clippy_utils::diagnostics::span_lint;
use clippy_utils::paths;
use clippy_utils::ty::match_type;
use clippy_utils::{is_item, paths};
use rustc_ast::ast::LitKind;
use rustc_hir::{Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass};
Expand Down Expand Up @@ -33,7 +32,7 @@ impl<'tcx> LateLintPass<'tcx> for OpenOptions {
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
if let ExprKind::MethodCall(path, _, [self_arg, ..], _) = &e.kind {
let obj_ty = cx.typeck_results().expr_ty(self_arg).peel_refs();
if path.ident.name == sym!(open) && match_type(cx, obj_ty, &paths::OPEN_OPTIONS) {
if path.ident.name == sym!(open) && is_item(cx, obj_ty, &paths::OPEN_OPTIONS) {
let mut options = Vec::new();
get_open_options(cx, self_arg, &mut options);
check_open_options(cx, &options, e.span);
Expand All @@ -59,12 +58,12 @@ enum OpenOption {
}

fn get_open_options(cx: &LateContext<'_>, argument: &Expr<'_>, options: &mut Vec<(OpenOption, Argument)>) {
if let ExprKind::MethodCall(path, _, arguments, _) = argument.kind {
let obj_ty = cx.typeck_results().expr_ty(&arguments[0]).peel_refs();
if let ExprKind::MethodCall(path, _, [self_arg, arg], _) = argument.kind {
let obj_ty = cx.typeck_results().expr_ty(self_arg).peel_refs();

// Only proceed if this is a call on some object of type std::fs::OpenOptions
if match_type(cx, obj_ty, &paths::OPEN_OPTIONS) && arguments.len() >= 2 {
let argument_option = match arguments[1].kind {
if is_item(cx, obj_ty, &paths::OPEN_OPTIONS) {
let argument_option = match arg.kind {
ExprKind::Lit(ref span) => {
if let Spanned {
node: LitKind::Bool(lit),
Expand Down Expand Up @@ -100,7 +99,7 @@ fn get_open_options(cx: &LateContext<'_>, argument: &Expr<'_>, options: &mut Vec
_ => (),
}

get_open_options(cx, &arguments[0], options);
get_open_options(cx, self_arg, options);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg, span_lint_and_then};
use clippy_utils::ptr::get_spans;
use clippy_utils::source::snippet_opt;
use clippy_utils::ty::{match_type, walk_ptrs_hir_ty};
use clippy_utils::ty::walk_ptrs_hir_ty;
use clippy_utils::{is_any_item, is_item, is_lint_allowed, paths};
use if_chain::if_chain;
use rustc_errors::Applicability;
Expand Down Expand Up @@ -337,7 +337,7 @@ fn check_fn(cx: &LateContext<'_>, decl: &FnDecl<'_>, fn_id: HirId, opt_body_id:
},
);
}
} else if match_type(cx, ty, &paths::COW) {
} else if is_item(cx, ty, &paths::COW) {
if_chain! {
if let TyKind::Rptr(_, MutTy { ty, ..} ) = arg.kind;
if let TyKind::Path(QPath::Resolved(None, pp)) = ty.kind;
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/question_mark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use clippy_utils::higher;
use clippy_utils::is_lang_ctor;
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::sugg::Sugg;
use clippy_utils::{eq_expr_value,is_item, path_to_local_id};
use clippy_utils::{eq_expr_value, is_item, path_to_local_id};
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::LangItem::{OptionNone, OptionSome};
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/repeat_once.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clippy_utils::consts::{constant_context, Constant};
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::{in_macro, is_item};
use clippy_utils::source::snippet;
use clippy_utils::{in_macro, is_item};
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind};
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/slow_vector_initialization.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::sugg::Sugg;
use clippy_utils::{is_item,get_enclosing_block, path_to_local, path_to_local_id, paths, SpanlessEq};
use clippy_utils::{get_enclosing_block, is_item, path_to_local, path_to_local_id, paths, SpanlessEq};
use if_chain::if_chain;
use rustc_ast::ast::LitKind;
use rustc_errors::Applicability;
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/strings.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clippy_utils::diagnostics::{span_lint, span_lint_and_help, span_lint_and_sugg};
use clippy_utils::source::{snippet, snippet_with_applicability};
use clippy_utils::SpanlessEq;
use clippy_utils::{is_item, get_parent_expr, is_lint_allowed, match_function_call, method_calls, paths};
use clippy_utils::{get_parent_expr, is_item, is_lint_allowed, match_function_call, method_calls, paths};
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::{BinOpKind, BorrowKind, Expr, ExprKind, LangItem, QPath};
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/unnecessary_sort_by.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::sugg::Sugg;
use clippy_utils::is_item;
use clippy_utils::sugg::Sugg;
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind, Mutability, Param, Pat, PatKind, Path, PathSegment, QPath};
Expand Down
6 changes: 2 additions & 4 deletions clippy_lints/src/unwrap_in_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,15 @@ impl<'a, 'tcx> Visitor<'tcx> for FindExpectUnwrap<'a, 'tcx> {
// check for `expect`
if let Some(arglists) = method_chain_args(expr, &["expect"]) {
let reciever_ty = self.typeck_results.expr_ty(&arglists[0][0]).peel_refs();
if is_any_item(self.lcx, reciever_ty, &[sym::result_type, sym::option_type]).is_some()
{
if is_any_item(self.lcx, reciever_ty, &[sym::result_type, sym::option_type]).is_some() {
self.result.push(expr.span);
}
}

// check for `unwrap`
if let Some(arglists) = method_chain_args(expr, &["unwrap"]) {
let reciever_ty = self.typeck_results.expr_ty(&arglists[0][0]).peel_refs();
if is_any_item(self.lcx, reciever_ty, &[sym::result_type, sym::option_type]).is_some()
{
if is_any_item(self.lcx, reciever_ty, &[sym::result_type, sym::option_type]).is_some() {
self.result.push(expr.span);
}
}
Expand Down
18 changes: 7 additions & 11 deletions clippy_lints/src/utils/internal_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use clippy_utils::consts::{constant_simple, Constant};
use clippy_utils::diagnostics::{span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then};
use clippy_utils::higher;
use clippy_utils::source::snippet;
use clippy_utils::ty::match_type;
use clippy_utils::{
is_any_item, is_else_clause, is_expn_of, is_item, is_lint_allowed, method_calls, path_to_res, paths, SpanlessEq,
};
Expand Down Expand Up @@ -505,8 +504,7 @@ impl<'tcx> LateLintPass<'tcx> for CompilerLintFunctions {
let fn_name = path.ident;
if let Some(sugg) = self.map.get(&*fn_name.as_str());
let ty = cx.typeck_results().expr_ty(self_arg).peel_refs();
if match_type(cx, ty, &paths::EARLY_CONTEXT)
|| match_type(cx, ty, &paths::LATE_CONTEXT);
if is_any_item(cx, ty, &[paths::EARLY_CONTEXT.as_slice(), paths::LATE_CONTEXT.as_slice()]).is_some();
then {
span_lint_and_help(
cx,
Expand Down Expand Up @@ -538,7 +536,7 @@ impl<'tcx> LateLintPass<'tcx> for OuterExpnDataPass {
if args.len() == 1;
let self_arg = &args[0];
let self_ty = cx.typeck_results().expr_ty(self_arg).peel_refs();
if match_type(cx, self_ty, &paths::SYNTAX_CONTEXT);
if is_item(cx, self_ty, &paths::SYNTAX_CONTEXT);
then {
span_lint_and_sugg(
cx,
Expand Down Expand Up @@ -917,7 +915,7 @@ impl<'tcx> LateLintPass<'tcx> for InterningDefinedSymbol {
if_chain! {
if let Res::Def(DefKind::Const, item_def_id) = item.res;
let ty = cx.tcx.type_of(item_def_id);
if match_type(cx, ty, &paths::SYMBOL);
if is_item(cx, ty, &paths::SYMBOL);
if let Ok(ConstValue::Scalar(value)) = cx.tcx.const_eval_poly(item_def_id);
if let Ok(value) = value.to_u32();
then {
Expand Down Expand Up @@ -1016,12 +1014,10 @@ impl InterningDefinedSymbol {
if let Some(did) = cx.typeck_results().type_dependent_def_id(call.hir_id);
let ty = cx.typeck_results().expr_ty(item);
// ...on either an Ident or a Symbol
if let Some(is_ident) = if match_type(cx, ty, &paths::SYMBOL) {
Some(false)
} else if match_type(cx, ty, &paths::IDENT) {
Some(true)
} else {
None
if let Some(is_ident) = match is_any_item(cx, ty, &[paths::SYMBOL.as_slice(), paths::IDENT.as_slice()]) {
Some(0) => Some(false),
Some(1) => Some(true),
_ => None,
};
// ...which converts it to a string
let paths = if is_ident { IDENT_STR_PATHS } else { SYMBOL_STR_PATHS };
Expand Down
Loading

0 comments on commit dd8070d

Please sign in to comment.