Skip to content

Commit 623faac

Browse files
committed
Cleanup: Use rustc's same_types instead of our same_tys
1 parent ea7066a commit 623faac

File tree

7 files changed

+29
-47
lines changed

7 files changed

+29
-47
lines changed

clippy_lints/src/copies.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use crate::utils::{get_parent_expr, higher, if_sequence, same_tys, snippet, span_lint_and_note, span_lint_and_then};
1+
use crate::utils::{get_parent_expr, higher, if_sequence, snippet, span_lint_and_note, span_lint_and_then};
22
use crate::utils::{SpanlessEq, SpanlessHash};
33
use rustc_data_structures::fx::FxHashMap;
44
use rustc_hir::{Arm, Block, Expr, ExprKind, MatchSource, Pat, PatKind};
55
use rustc_lint::{LateContext, LateLintPass};
6-
use rustc_middle::ty::Ty;
6+
use rustc_middle::ty::{Ty, TyS};
77
use rustc_session::{declare_lint_pass, declare_tool_lint};
88
use rustc_span::symbol::Symbol;
99
use std::collections::hash_map::Entry;
@@ -242,15 +242,11 @@ fn lint_same_fns_in_if_cond(cx: &LateContext<'_, '_>, conds: &[&Expr<'_>]) {
242242

243243
/// Implementation of `MATCH_SAME_ARMS`.
244244
fn lint_match_arms<'tcx>(cx: &LateContext<'_, 'tcx>, expr: &Expr<'_>) {
245-
fn same_bindings<'tcx>(
246-
cx: &LateContext<'_, 'tcx>,
247-
lhs: &FxHashMap<Symbol, Ty<'tcx>>,
248-
rhs: &FxHashMap<Symbol, Ty<'tcx>>,
249-
) -> bool {
245+
fn same_bindings<'tcx>(lhs: &FxHashMap<Symbol, Ty<'tcx>>, rhs: &FxHashMap<Symbol, Ty<'tcx>>) -> bool {
250246
lhs.len() == rhs.len()
251247
&& lhs
252248
.iter()
253-
.all(|(name, l_ty)| rhs.get(name).map_or(false, |r_ty| same_tys(cx, l_ty, r_ty)))
249+
.all(|(name, l_ty)| rhs.get(name).map_or(false, |r_ty| TyS::same_type(l_ty, r_ty)))
254250
}
255251

256252
if let ExprKind::Match(_, ref arms, MatchSource::Normal) = expr.kind {
@@ -269,7 +265,7 @@ fn lint_match_arms<'tcx>(cx: &LateContext<'_, 'tcx>, expr: &Expr<'_>) {
269265
(min_index..=max_index).all(|index| arms[index].guard.is_none()) &&
270266
SpanlessEq::new(cx).eq_expr(&lhs.body, &rhs.body) &&
271267
// all patterns should have the same bindings
272-
same_bindings(cx, &bindings(cx, &lhs.pat), &bindings(cx, &rhs.pat))
268+
same_bindings(&bindings(cx, &lhs.pat), &bindings(cx, &rhs.pat))
273269
};
274270

275271
let indexed_arms: Vec<(usize, &Arm<'_>)> = arms.iter().enumerate().collect();

clippy_lints/src/loops.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::utils::{
88
multispan_sugg, snippet, snippet_opt, snippet_with_applicability, span_lint, span_lint_and_help,
99
span_lint_and_sugg, span_lint_and_then, SpanlessEq,
1010
};
11-
use crate::utils::{is_type_diagnostic_item, qpath_res, same_tys, sugg};
11+
use crate::utils::{is_type_diagnostic_item, qpath_res, sugg};
1212
use if_chain::if_chain;
1313
use rustc_ast::ast;
1414
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
@@ -24,7 +24,7 @@ use rustc_lint::{LateContext, LateLintPass, LintContext};
2424
use rustc_middle::hir::map::Map;
2525
use rustc_middle::lint::in_external_macro;
2626
use rustc_middle::middle::region;
27-
use rustc_middle::ty::{self, Ty};
27+
use rustc_middle::ty::{self, Ty, TyS};
2828
use rustc_session::{declare_lint_pass, declare_tool_lint};
2929
use rustc_span::source_map::Span;
3030
use rustc_span::symbol::Symbol;
@@ -1256,7 +1256,7 @@ fn check_for_loop_arg(cx: &LateContext<'_, '_>, pat: &Pat<'_>, arg: &Expr<'_>, e
12561256
} else if method_name == "into_iter" && match_trait_method(cx, arg, &paths::INTO_ITERATOR) {
12571257
let receiver_ty = cx.tables.expr_ty(&args[0]);
12581258
let receiver_ty_adjusted = cx.tables.expr_ty_adjusted(&args[0]);
1259-
if same_tys(cx, receiver_ty, receiver_ty_adjusted) {
1259+
if TyS::same_type(receiver_ty, receiver_ty_adjusted) {
12601260
let mut applicability = Applicability::MachineApplicable;
12611261
let object = snippet_with_applicability(cx, args[0].span, "_", &mut applicability);
12621262
span_lint_and_sugg(
@@ -1277,7 +1277,7 @@ fn check_for_loop_arg(cx: &LateContext<'_, '_>, pat: &Pat<'_>, arg: &Expr<'_>, e
12771277
mutbl: Mutability::Not,
12781278
},
12791279
);
1280-
if same_tys(cx, receiver_ty_adjusted, ref_receiver_ty) {
1280+
if TyS::same_type(receiver_ty_adjusted, ref_receiver_ty) {
12811281
lint_iter_method(cx, args, arg, method_name)
12821282
}
12831283
}

clippy_lints/src/methods/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_lint::{LateContext, LateLintPass, Lint, LintContext};
1818
use rustc_middle::hir::map::Map;
1919
use rustc_middle::lint::in_external_macro;
2020
use rustc_middle::ty::subst::GenericArgKind;
21-
use rustc_middle::ty::{self, Ty};
21+
use rustc_middle::ty::{self, Ty, TyS};
2222
use rustc_session::{declare_lint_pass, declare_tool_lint};
2323
use rustc_span::source_map::Span;
2424
use rustc_span::symbol::{sym, SymbolStr};
@@ -29,9 +29,9 @@ use crate::utils::{
2929
get_arg_name, get_parent_expr, get_trait_def_id, has_iter_method, higher, implements_trait, in_macro, is_copy,
3030
is_ctor_or_promotable_const_function, is_expn_of, is_type_diagnostic_item, iter_input_pats, last_path_segment,
3131
match_def_path, match_qpath, match_trait_method, match_type, match_var, method_calls, method_chain_args, paths,
32-
remove_blocks, return_ty, same_tys, single_segment_path, snippet, snippet_with_applicability,
33-
snippet_with_macro_callsite, span_lint, span_lint_and_help, span_lint_and_note, span_lint_and_sugg,
34-
span_lint_and_then, sugg, walk_ptrs_ty, walk_ptrs_ty_depth, SpanlessEq,
32+
remove_blocks, return_ty, single_segment_path, snippet, snippet_with_applicability, snippet_with_macro_callsite,
33+
span_lint, span_lint_and_help, span_lint_and_note, span_lint_and_sugg, span_lint_and_then, sugg, walk_ptrs_ty,
34+
walk_ptrs_ty_depth, SpanlessEq,
3535
};
3636

3737
declare_clippy_lint! {
@@ -1548,7 +1548,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
15481548

15491549
let contains_self_ty = |ty: Ty<'tcx>| {
15501550
ty.walk().any(|inner| match inner.unpack() {
1551-
GenericArgKind::Type(inner_ty) => same_tys(cx, self_ty, inner_ty),
1551+
GenericArgKind::Type(inner_ty) => TyS::same_type(self_ty, inner_ty),
15521552

15531553
GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => false,
15541554
})
@@ -1575,7 +1575,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
15751575
}
15761576
}
15771577

1578-
if name == "new" && !same_tys(cx, ret_ty, self_ty) {
1578+
if name == "new" && !TyS::same_type(ret_ty, self_ty) {
15791579
span_lint(
15801580
cx,
15811581
NEW_RET_NO_SELF,

clippy_lints/src/new_without_default.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use crate::utils::paths;
22
use crate::utils::sugg::DiagnosticBuilderExt;
3-
use crate::utils::{get_trait_def_id, return_ty, same_tys, span_lint_hir_and_then};
3+
use crate::utils::{get_trait_def_id, return_ty, span_lint_hir_and_then};
44
use if_chain::if_chain;
55
use rustc_errors::Applicability;
66
use rustc_hir as hir;
77
use rustc_hir::HirIdSet;
88
use rustc_lint::{LateContext, LateLintPass, LintContext};
99
use rustc_middle::lint::in_external_macro;
10-
use rustc_middle::ty::Ty;
10+
use rustc_middle::ty::{Ty, TyS};
1111
use rustc_session::{declare_tool_lint, impl_lint_pass};
1212

1313
declare_clippy_lint! {
@@ -93,7 +93,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NewWithoutDefault {
9393
let self_def_id = cx.tcx.hir().local_def_id(cx.tcx.hir().get_parent_item(id));
9494
let self_ty = cx.tcx.type_of(self_def_id);
9595
if_chain! {
96-
if same_tys(cx, self_ty, return_ty(cx, id));
96+
if TyS::same_type(self_ty, return_ty(cx, id));
9797
if let Some(default_trait_id) = get_trait_def_id(cx, &paths::DEFAULT_TRAIT);
9898
then {
9999
if self.impling_types.is_none() {

clippy_lints/src/types.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_hir::{
1717
use rustc_lint::{LateContext, LateLintPass, LintContext};
1818
use rustc_middle::hir::map::Map;
1919
use rustc_middle::lint::in_external_macro;
20-
use rustc_middle::ty::{self, InferTy, Ty, TyCtxt, TypeckTables};
20+
use rustc_middle::ty::{self, InferTy, Ty, TyCtxt, TyS, TypeckTables};
2121
use rustc_session::{declare_lint_pass, declare_tool_lint, impl_lint_pass};
2222
use rustc_span::hygiene::{ExpnKind, MacroKind};
2323
use rustc_span::source_map::Span;
@@ -31,7 +31,7 @@ use crate::utils::paths;
3131
use crate::utils::{
3232
clip, comparisons, differing_macro_contexts, higher, in_constant, indent_of, int_bits, is_type_diagnostic_item,
3333
last_path_segment, match_def_path, match_path, method_chain_args, multispan_sugg, numeric_literal::NumericLiteral,
34-
qpath_res, same_tys, sext, snippet, snippet_block_with_applicability, snippet_opt, snippet_with_applicability,
34+
qpath_res, sext, snippet, snippet_block_with_applicability, snippet_opt, snippet_with_applicability,
3535
snippet_with_macro_callsite, span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then, unsext,
3636
};
3737

@@ -2556,7 +2556,7 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for ImplicitHasherConstructorVisitor<'a, 'b, 't
25562556
if let ExprKind::Path(QPath::TypeRelative(ref ty, ref method)) = fun.kind;
25572557
if let TyKind::Path(QPath::Resolved(None, ty_path)) = ty.kind;
25582558
then {
2559-
if !same_tys(self.cx, self.target.ty(), self.body.expr_ty(e)) {
2559+
if !TyS::same_type(self.target.ty(), self.body.expr_ty(e)) {
25602560
return;
25612561
}
25622562

clippy_lints/src/useless_conversion.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use crate::utils::{
2-
is_type_diagnostic_item, match_def_path, match_trait_method, paths, same_tys, snippet, snippet_with_macro_callsite,
2+
is_type_diagnostic_item, match_def_path, match_trait_method, paths, snippet, snippet_with_macro_callsite,
33
span_lint_and_help, span_lint_and_sugg,
44
};
55
use if_chain::if_chain;
66
use rustc_errors::Applicability;
77
use rustc_hir::{Expr, ExprKind, HirId, MatchSource};
88
use rustc_lint::{LateContext, LateLintPass};
9-
use rustc_middle::ty;
9+
use rustc_middle::ty::{self, TyS};
1010
use rustc_session::{declare_tool_lint, impl_lint_pass};
1111

1212
declare_clippy_lint! {
@@ -65,7 +65,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UselessConversion {
6565
if match_trait_method(cx, e, &paths::INTO) && &*name.ident.as_str() == "into" {
6666
let a = cx.tables.expr_ty(e);
6767
let b = cx.tables.expr_ty(&args[0]);
68-
if same_tys(cx, a, b) {
68+
if TyS::same_type(a, b) {
6969
let sugg = snippet_with_macro_callsite(cx, args[0].span, "<expr>").to_string();
7070
span_lint_and_sugg(
7171
cx,
@@ -81,7 +81,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UselessConversion {
8181
if match_trait_method(cx, e, &paths::INTO_ITERATOR) && &*name.ident.as_str() == "into_iter" {
8282
let a = cx.tables.expr_ty(e);
8383
let b = cx.tables.expr_ty(&args[0]);
84-
if same_tys(cx, a, b) {
84+
if TyS::same_type(a, b) {
8585
let sugg = snippet(cx, args[0].span, "<expr>").into_owned();
8686
span_lint_and_sugg(
8787
cx,
@@ -101,7 +101,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UselessConversion {
101101
if is_type_diagnostic_item(cx, a, sym!(result_type));
102102
if let ty::Adt(_, substs) = a.kind;
103103
if let Some(a_type) = substs.types().next();
104-
if same_tys(cx, a_type, b);
104+
if TyS::same_type(a_type, b);
105105

106106
then {
107107
span_lint_and_help(
@@ -131,7 +131,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UselessConversion {
131131
if is_type_diagnostic_item(cx, a, sym!(result_type));
132132
if let ty::Adt(_, substs) = a.kind;
133133
if let Some(a_type) = substs.types().next();
134-
if same_tys(cx, a_type, b);
134+
if TyS::same_type(a_type, b);
135135

136136
then {
137137
let hint = format!("consider removing `{}()`", snippet(cx, path.span, "TryFrom::try_from"));
@@ -148,7 +148,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UselessConversion {
148148

149149
if_chain! {
150150
if match_def_path(cx, def_id, &paths::FROM_FROM);
151-
if same_tys(cx, a, b);
151+
if TyS::same_type(a, b);
152152

153153
then {
154154
let sugg = snippet(cx, args[0].span.source_callsite(), "<expr>").into_owned();

clippy_lints/src/utils/mod.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use rustc_hir::{
4040
use rustc_infer::infer::TyCtxtInferExt;
4141
use rustc_lint::{LateContext, Level, Lint, LintContext};
4242
use rustc_middle::hir::map::Map;
43-
use rustc_middle::ty::{self, layout::IntegerExt, subst::GenericArg, Binder, Ty, TyCtxt, TypeFoldable};
43+
use rustc_middle::ty::{self, layout::IntegerExt, subst::GenericArg, Ty, TyCtxt, TypeFoldable};
4444
use rustc_span::hygiene::{ExpnKind, MacroKind};
4545
use rustc_span::source_map::original_sp;
4646
use rustc_span::symbol::{self, kw, Symbol};
@@ -879,20 +879,6 @@ pub fn return_ty<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, fn_item: hir::HirId) -> T
879879
cx.tcx.erase_late_bound_regions(&ret_ty)
880880
}
881881

882-
/// Checks if two types are the same.
883-
///
884-
/// This discards any lifetime annotations, too.
885-
//
886-
// FIXME: this works correctly for lifetimes bounds (`for <'a> Foo<'a>` ==
887-
// `for <'b> Foo<'b>`, but not for type parameters).
888-
pub fn same_tys<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, a: Ty<'tcx>, b: Ty<'tcx>) -> bool {
889-
let a = cx.tcx.erase_late_bound_regions(&Binder::bind(a));
890-
let b = cx.tcx.erase_late_bound_regions(&Binder::bind(b));
891-
cx.tcx
892-
.infer_ctxt()
893-
.enter(|infcx| infcx.can_eq(cx.param_env, a, b).is_ok())
894-
}
895-
896882
/// Returns `true` if the given type is an `unsafe` function.
897883
pub fn type_is_unsafe_function<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, ty: Ty<'tcx>) -> bool {
898884
match ty.kind {

0 commit comments

Comments
 (0)