Skip to content

Commit 56d591e

Browse files
committed
Cleanup: Use rustc's same_types for our same_tys
This delegates our `same_tys` to [ty::TyS::same_type][same_type] to remove some duplication. Our `same_tys` was introduced 4 years ago in #730. Before, `same_tys` was building an inference context to be able to call `can_eq` to compare the types. The `rustc-dev-guide` has some more details about `can_eq` in [Type Inference -> Trying equality][try_eq]. Now, using the rustc function, we are essentially comparing the `DefId`s of the given types, which also makes more sense, IMO. I also confirmed that the FIXME is resolved via a bit of `dbg!`, however no UI tests were affected. [same_type]: https://github.com/rust-lang/rust/blob/659951c4a0d7450e43f61c61c0e87d0ceae17087/src/librustc_middle/ty/util.rs#L777 [try_eq]: https://rustc-dev-guide.rust-lang.org/type-inference.html#trying-equality
1 parent 6ffe725 commit 56d591e

File tree

7 files changed

+14
-24
lines changed

7 files changed

+14
-24
lines changed

clippy_lints/src/copies.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,14 +243,13 @@ fn lint_same_fns_in_if_cond(cx: &LateContext<'_, '_>, conds: &[&Expr<'_>]) {
243243
/// Implementation of `MATCH_SAME_ARMS`.
244244
fn lint_match_arms<'tcx>(cx: &LateContext<'_, 'tcx>, expr: &Expr<'_>) {
245245
fn same_bindings<'tcx>(
246-
cx: &LateContext<'_, 'tcx>,
247246
lhs: &FxHashMap<Symbol, Ty<'tcx>>,
248247
rhs: &FxHashMap<Symbol, Ty<'tcx>>,
249248
) -> bool {
250249
lhs.len() == rhs.len()
251250
&& lhs
252251
.iter()
253-
.all(|(name, l_ty)| rhs.get(name).map_or(false, |r_ty| same_tys(cx, l_ty, r_ty)))
252+
.all(|(name, l_ty)| rhs.get(name).map_or(false, |r_ty| same_tys(l_ty, r_ty)))
254253
}
255254

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

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

clippy_lints/src/identity_conversion.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityConversion {
5555
if match_trait_method(cx, e, &paths::INTO) && &*name.ident.as_str() == "into" {
5656
let a = cx.tables.expr_ty(e);
5757
let b = cx.tables.expr_ty(&args[0]);
58-
if same_tys(cx, a, b) {
58+
if same_tys(a, b) {
5959
let sugg = snippet_with_macro_callsite(cx, args[0].span, "<expr>").to_string();
6060

6161
span_lint_and_sugg(
@@ -72,7 +72,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityConversion {
7272
if match_trait_method(cx, e, &paths::INTO_ITERATOR) && &*name.ident.as_str() == "into_iter" {
7373
let a = cx.tables.expr_ty(e);
7474
let b = cx.tables.expr_ty(&args[0]);
75-
if same_tys(cx, a, b) {
75+
if same_tys(a, b) {
7676
let sugg = snippet(cx, args[0].span, "<expr>").into_owned();
7777
span_lint_and_sugg(
7878
cx,
@@ -93,7 +93,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityConversion {
9393
if match_def_path(cx, def_id, &paths::FROM_FROM) {
9494
let a = cx.tables.expr_ty(e);
9595
let b = cx.tables.expr_ty(&args[0]);
96-
if same_tys(cx, a, b) {
96+
if same_tys(a, b) {
9797
let sugg = snippet(cx, args[0].span.source_callsite(), "<expr>").into_owned();
9898
let sugg_msg =
9999
format!("consider removing `{}()`", snippet(cx, path.span, "From::from"));

clippy_lints/src/loops.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,7 +1348,7 @@ fn check_for_loop_arg(cx: &LateContext<'_, '_>, pat: &Pat<'_>, arg: &Expr<'_>, e
13481348
} else if method_name == "into_iter" && match_trait_method(cx, arg, &paths::INTO_ITERATOR) {
13491349
let receiver_ty = cx.tables.expr_ty(&args[0]);
13501350
let receiver_ty_adjusted = cx.tables.expr_ty_adjusted(&args[0]);
1351-
if same_tys(cx, receiver_ty, receiver_ty_adjusted) {
1351+
if same_tys(receiver_ty, receiver_ty_adjusted) {
13521352
let mut applicability = Applicability::MachineApplicable;
13531353
let object = snippet_with_applicability(cx, args[0].span, "_", &mut applicability);
13541354
span_lint_and_sugg(
@@ -1369,7 +1369,7 @@ fn check_for_loop_arg(cx: &LateContext<'_, '_>, pat: &Pat<'_>, arg: &Expr<'_>, e
13691369
mutbl: Mutability::Not,
13701370
},
13711371
);
1372-
if same_tys(cx, receiver_ty_adjusted, ref_receiver_ty) {
1372+
if same_tys(receiver_ty_adjusted, ref_receiver_ty) {
13731373
lint_iter_method(cx, args, arg, method_name)
13741374
}
13751375
}

clippy_lints/src/methods/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,7 +1524,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
15241524

15251525
let contains_self_ty = |ty: Ty<'tcx>| {
15261526
ty.walk().any(|inner| match inner.unpack() {
1527-
GenericArgKind::Type(inner_ty) => same_tys(cx, self_ty, inner_ty),
1527+
GenericArgKind::Type(inner_ty) => same_tys(self_ty, inner_ty),
15281528

15291529
GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => false,
15301530
})
@@ -1554,7 +1554,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
15541554
}
15551555
}
15561556

1557-
if name == "new" && !same_tys(cx, ret_ty, self_ty) {
1557+
if name == "new" && !same_tys(ret_ty, self_ty) {
15581558
span_lint(
15591559
cx,
15601560
NEW_RET_NO_SELF,

clippy_lints/src/new_without_default.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NewWithoutDefault {
129129
let self_did = cx.tcx.hir().local_def_id(cx.tcx.hir().get_parent_item(id));
130130
let self_ty = cx.tcx.type_of(self_did);
131131
if_chain! {
132-
if same_tys(cx, self_ty, return_ty(cx, id));
132+
if same_tys(self_ty, return_ty(cx, id));
133133
if let Some(default_trait_id) = get_trait_def_id(cx, &paths::DEFAULT_TRAIT);
134134
then {
135135
if self.impling_types.is_none() {

clippy_lints/src/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2462,7 +2462,7 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for ImplicitHasherConstructorVisitor<'a, 'b, 't
24622462
if let ExprKind::Path(QPath::TypeRelative(ref ty, ref method)) = fun.kind;
24632463
if let TyKind::Path(QPath::Resolved(None, ref ty_path)) = ty.kind;
24642464
then {
2465-
if !same_tys(self.cx, self.target.ty(), self.body.expr_ty(e)) {
2465+
if !same_tys(self.target.ty(), self.body.expr_ty(e)) {
24662466
return;
24672467
}
24682468

clippy_lints/src/utils/mod.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use rustc_infer::infer::TyCtxtInferExt;
4141
use rustc_lint::{LateContext, Level, Lint, LintContext};
4242
use rustc_middle::hir::map::Map;
4343
use rustc_middle::traits;
44-
use rustc_middle::ty::{self, layout::IntegerExt, subst::GenericArg, Binder, Ty, TyCtxt, TypeFoldable};
44+
use rustc_middle::ty::{self, layout::IntegerExt, subst::GenericArg, Ty, TyCtxt, TypeFoldable};
4545
use rustc_span::hygiene::{ExpnKind, MacroKind};
4646
use rustc_span::source_map::original_sp;
4747
use rustc_span::symbol::{self, kw, Symbol};
@@ -890,17 +890,8 @@ pub fn return_ty<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, fn_item: hir::HirId) -> T
890890
}
891891

892892
/// Checks if two types are the same.
893-
///
894-
/// This discards any lifetime annotations, too.
895-
//
896-
// FIXME: this works correctly for lifetimes bounds (`for <'a> Foo<'a>` ==
897-
// `for <'b> Foo<'b>`, but not for type parameters).
898-
pub fn same_tys<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, a: Ty<'tcx>, b: Ty<'tcx>) -> bool {
899-
let a = cx.tcx.erase_late_bound_regions(&Binder::bind(a));
900-
let b = cx.tcx.erase_late_bound_regions(&Binder::bind(b));
901-
cx.tcx
902-
.infer_ctxt()
903-
.enter(|infcx| infcx.can_eq(cx.param_env, a, b).is_ok())
893+
pub fn same_tys<'tcx>(a: Ty<'tcx>, b: Ty<'tcx>) -> bool {
894+
ty::TyS::same_type(a, b)
904895
}
905896

906897
/// Returns `true` if the given type is an `unsafe` function.

0 commit comments

Comments
 (0)