Factor out eq_ty_kind

This commit is contained in:
Cameron Steffen 2021-03-23 10:40:27 -05:00
parent 029777f029
commit 9132dbdf31
2 changed files with 10 additions and 20 deletions

View File

@ -62,13 +62,11 @@
use bind_instead_of_map::BindInsteadOfMap; use bind_instead_of_map::BindInsteadOfMap;
use clippy_utils::diagnostics::{span_lint, span_lint_and_help}; use clippy_utils::diagnostics::{span_lint, span_lint_and_help};
use clippy_utils::ty::{contains_ty, implements_trait, is_copy, is_type_diagnostic_item}; use clippy_utils::ty::{contains_ty, implements_trait, is_copy, is_type_diagnostic_item};
use clippy_utils::{ use clippy_utils::{contains_return, get_trait_def_id, in_macro, iter_input_pats, method_calls, paths, return_ty};
contains_return, get_trait_def_id, in_macro, iter_input_pats, match_qpath, method_calls, paths, return_ty,
SpanlessEq,
};
use if_chain::if_chain; use if_chain::if_chain;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::{TraitItem, TraitItemKind}; use rustc_hir::def::Res;
use rustc_hir::{PrimTy, QPath, TraitItem, TraitItemKind};
use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::lint::in_external_macro; use rustc_middle::lint::in_external_macro;
use rustc_middle::ty::{self, TraitRef, Ty, TyS}; use rustc_middle::ty::{self, TraitRef, Ty, TyS};
@ -1872,7 +1870,7 @@ fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx hir::Impl
for method_config in &TRAIT_METHODS { for method_config in &TRAIT_METHODS {
if name == method_config.method_name && if name == method_config.method_name &&
sig.decl.inputs.len() == method_config.param_count && sig.decl.inputs.len() == method_config.param_count &&
method_config.output_type.matches(cx, &sig.decl.output) && method_config.output_type.matches(&sig.decl.output) &&
method_config.self_kind.matches(cx, self_ty, first_arg_ty) && method_config.self_kind.matches(cx, self_ty, first_arg_ty) &&
fn_header_equals(method_config.fn_header, sig.header) && fn_header_equals(method_config.fn_header, sig.header) &&
method_config.lifetime_param_cond(&impl_item) method_config.lifetime_param_cond(&impl_item)
@ -2199,8 +2197,8 @@ enum OutType {
} }
impl OutType { impl OutType {
fn matches(self, cx: &LateContext<'_>, ty: &hir::FnRetTy<'_>) -> bool { fn matches(self, ty: &hir::FnRetTy<'_>) -> bool {
let is_unit = |ty: &hir::Ty<'_>| SpanlessEq::new(cx).eq_ty_kind(&ty.kind, &hir::TyKind::Tup(&[])); let is_unit = |ty: &hir::Ty<'_>| matches!(ty.kind, hir::TyKind::Tup(&[]));
match (self, ty) { match (self, ty) {
(Self::Unit, &hir::FnRetTy::DefaultReturn(_)) => true, (Self::Unit, &hir::FnRetTy::DefaultReturn(_)) => true,
(Self::Unit, &hir::FnRetTy::Return(ref ty)) if is_unit(ty) => true, (Self::Unit, &hir::FnRetTy::Return(ref ty)) if is_unit(ty) => true,
@ -2213,8 +2211,8 @@ fn matches(self, cx: &LateContext<'_>, ty: &hir::FnRetTy<'_>) -> bool {
} }
fn is_bool(ty: &hir::Ty<'_>) -> bool { fn is_bool(ty: &hir::Ty<'_>) -> bool {
if let hir::TyKind::Path(ref p) = ty.kind { if let hir::TyKind::Path(QPath::Resolved(_, path)) = ty.kind {
match_qpath(p, &["bool"]) matches!(path.res, Res::PrimTy(PrimTy::Bool))
} else { } else {
false false
} }

View File

@ -80,10 +80,6 @@ pub fn eq_path_segment(&mut self, left: &PathSegment<'_>, right: &PathSegment<'_
pub fn eq_path_segments(&mut self, left: &[PathSegment<'_>], right: &[PathSegment<'_>]) -> bool { pub fn eq_path_segments(&mut self, left: &[PathSegment<'_>], right: &[PathSegment<'_>]) -> bool {
self.inter_expr().eq_path_segments(left, right) self.inter_expr().eq_path_segments(left, right)
} }
pub fn eq_ty_kind(&mut self, left: &TyKind<'_>, right: &TyKind<'_>) -> bool {
self.inter_expr().eq_ty_kind(left, right)
}
} }
struct HirEqInterExpr<'a, 'b, 'tcx> { struct HirEqInterExpr<'a, 'b, 'tcx> {
@ -378,13 +374,9 @@ pub fn eq_path_segment(&mut self, left: &PathSegment<'_>, right: &PathSegment<'_
left.ident.name == right.ident.name && both(&left.args, &right.args, |l, r| self.eq_path_parameters(l, r)) left.ident.name == right.ident.name && both(&left.args, &right.args, |l, r| self.eq_path_parameters(l, r))
} }
fn eq_ty(&mut self, left: &Ty<'_>, right: &Ty<'_>) -> bool {
self.eq_ty_kind(&left.kind, &right.kind)
}
#[allow(clippy::similar_names)] #[allow(clippy::similar_names)]
fn eq_ty_kind(&mut self, left: &TyKind<'_>, right: &TyKind<'_>) -> bool { fn eq_ty(&mut self, left: &Ty<'_>, right: &Ty<'_>) -> bool {
match (left, right) { match (&left.kind, &right.kind) {
(&TyKind::Slice(ref l_vec), &TyKind::Slice(ref r_vec)) => self.eq_ty(l_vec, r_vec), (&TyKind::Slice(ref l_vec), &TyKind::Slice(ref r_vec)) => self.eq_ty(l_vec, r_vec),
(&TyKind::Array(ref lt, ref ll_id), &TyKind::Array(ref rt, ref rl_id)) => { (&TyKind::Array(ref lt, ref ll_id), &TyKind::Array(ref rt, ref rl_id)) => {
let cx = self.inner.cx; let cx = self.inner.cx;