Skip to content

Commit 5d13b15

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 87a6f3f commit 5d13b15

File tree

7 files changed

+24
-38
lines changed

7 files changed

+24
-38
lines changed

clippy_lints/src/copies.rs

+4-5
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;
@@ -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| TyS::same_type(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

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use crate::utils::{
2-
match_def_path, match_trait_method, paths, same_tys, snippet, snippet_with_macro_callsite, span_lint_and_sugg,
2+
match_def_path, match_trait_method, paths, snippet, snippet_with_macro_callsite, span_lint_and_sugg,
33
};
44
use rustc_errors::Applicability;
55
use rustc_hir::{Expr, ExprKind, HirId, MatchSource};
66
use rustc_lint::{LateContext, LateLintPass};
7+
use rustc_middle::ty::TyS;
78
use rustc_session::{declare_tool_lint, impl_lint_pass};
89

910
declare_clippy_lint! {
@@ -55,7 +56,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityConversion {
5556
if match_trait_method(cx, e, &paths::INTO) && &*name.ident.as_str() == "into" {
5657
let a = cx.tables.expr_ty(e);
5758
let b = cx.tables.expr_ty(&args[0]);
58-
if same_tys(cx, a, b) {
59+
if TyS::same_type(a, b) {
5960
let sugg = snippet_with_macro_callsite(cx, args[0].span, "<expr>").to_string();
6061

6162
span_lint_and_sugg(
@@ -72,7 +73,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityConversion {
7273
if match_trait_method(cx, e, &paths::INTO_ITERATOR) && &*name.ident.as_str() == "into_iter" {
7374
let a = cx.tables.expr_ty(e);
7475
let b = cx.tables.expr_ty(&args[0]);
75-
if same_tys(cx, a, b) {
76+
if TyS::same_type(a, b) {
7677
let sugg = snippet(cx, args[0].span, "<expr>").into_owned();
7778
span_lint_and_sugg(
7879
cx,
@@ -93,7 +94,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityConversion {
9394
if match_def_path(cx, def_id, &paths::FROM_FROM) {
9495
let a = cx.tables.expr_ty(e);
9596
let b = cx.tables.expr_ty(&args[0]);
96-
if same_tys(cx, a, b) {
97+
if TyS::same_type(a, b) {
9798
let sugg = snippet(cx, args[0].span.source_callsite(), "<expr>").into_owned();
9899
let sugg_msg =
99100
format!("consider removing `{}()`", snippet(cx, path.span, "From::from"));

clippy_lints/src/loops.rs

+4-4
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, sext, sugg};
11+
use crate::utils::{is_type_diagnostic_item, qpath_res, sext, sugg};
1212
use if_chain::if_chain;
1313
use itertools::Itertools;
1414
use rustc_ast::ast;
@@ -25,7 +25,7 @@ use rustc_lint::{LateContext, LateLintPass, LintContext};
2525
use rustc_middle::hir::map::Map;
2626
use rustc_middle::lint::in_external_macro;
2727
use rustc_middle::middle::region;
28-
use rustc_middle::ty::{self, Ty};
28+
use rustc_middle::ty::{self, Ty, TyS};
2929
use rustc_session::{declare_lint_pass, declare_tool_lint};
3030
use rustc_span::source_map::Span;
3131
use rustc_span::BytePos;
@@ -1359,7 +1359,7 @@ fn check_for_loop_arg(cx: &LateContext<'_, '_>, pat: &Pat<'_>, arg: &Expr<'_>, e
13591359
} else if method_name == "into_iter" && match_trait_method(cx, arg, &paths::INTO_ITERATOR) {
13601360
let receiver_ty = cx.tables.expr_ty(&args[0]);
13611361
let receiver_ty_adjusted = cx.tables.expr_ty_adjusted(&args[0]);
1362-
if same_tys(cx, receiver_ty, receiver_ty_adjusted) {
1362+
if TyS::same_type(receiver_ty, receiver_ty_adjusted) {
13631363
let mut applicability = Applicability::MachineApplicable;
13641364
let object = snippet_with_applicability(cx, args[0].span, "_", &mut applicability);
13651365
span_lint_and_sugg(
@@ -1380,7 +1380,7 @@ fn check_for_loop_arg(cx: &LateContext<'_, '_>, pat: &Pat<'_>, arg: &Expr<'_>, e
13801380
mutbl: Mutability::Not,
13811381
},
13821382
);
1383-
if same_tys(cx, receiver_ty_adjusted, ref_receiver_ty) {
1383+
if TyS::same_type(receiver_ty_adjusted, ref_receiver_ty) {
13841384
lint_iter_method(cx, args, arg, method_name)
13851385
}
13861386
}

clippy_lints/src/methods/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc_lint::{LateContext, LateLintPass, Lint, LintContext};
1616
use rustc_middle::hir::map::Map;
1717
use rustc_middle::lint::in_external_macro;
1818
use rustc_middle::ty::subst::GenericArgKind;
19-
use rustc_middle::ty::{self, Predicate, Ty};
19+
use rustc_middle::ty::{self, Predicate, Ty, TyS};
2020
use rustc_session::{declare_lint_pass, declare_tool_lint};
2121
use rustc_span::source_map::Span;
2222
use rustc_span::symbol::{sym, SymbolStr};
@@ -27,7 +27,7 @@ use crate::utils::{
2727
get_arg_name, get_parent_expr, get_trait_def_id, has_iter_method, implements_trait, in_macro, is_copy,
2828
is_ctor_or_promotable_const_function, is_expn_of, is_type_diagnostic_item, iter_input_pats, last_path_segment,
2929
match_def_path, match_qpath, match_trait_method, match_type, match_var, method_calls, method_chain_args, paths,
30-
remove_blocks, return_ty, same_tys, single_segment_path, snippet, snippet_with_applicability,
30+
remove_blocks, return_ty, single_segment_path, snippet, snippet_with_applicability,
3131
snippet_with_macro_callsite, span_lint, span_lint_and_help, span_lint_and_note, span_lint_and_sugg,
3232
span_lint_and_then, sugg, walk_ptrs_ty, walk_ptrs_ty_depth, SpanlessEq,
3333
};
@@ -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) => TyS::same_type(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" && !TyS::same_type(ret_ty, self_ty) {
15581558
span_lint(
15591559
cx,
15601560
NEW_RET_NO_SELF,

clippy_lints/src/new_without_default.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use crate::utils::paths;
22
use crate::utils::sugg::DiagnosticBuilderExt;
3-
use crate::utils::{get_trait_def_id, implements_trait, return_ty, same_tys, span_lint_hir_and_then};
3+
use crate::utils::{get_trait_def_id, implements_trait, 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::def_id::DefId;
88
use rustc_hir::HirIdSet;
99
use rustc_lint::{LateContext, LateLintPass, LintContext};
1010
use rustc_middle::lint::in_external_macro;
11-
use rustc_middle::ty::{self, Ty};
11+
use rustc_middle::ty::{self, Ty, TyS};
1212
use rustc_session::{declare_tool_lint, impl_lint_pass};
1313
use rustc_span::source_map::Span;
1414

@@ -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 TyS::same_type(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

+3-3
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, TypeckTables, TyS};
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, 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_opt, snippet_with_applicability, snippet_with_macro_callsite,
34+
qpath_res, sext, snippet, snippet_opt, snippet_with_applicability, snippet_with_macro_callsite,
3535
span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then, unsext,
3636
};
3737

@@ -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, ty_path)) = ty.kind;
24642464
then {
2465-
if !same_tys(self.cx, self.target.ty(), self.body.expr_ty(e)) {
2465+
if !TyS::same_type(self.target.ty(), self.body.expr_ty(e)) {
24662466
return;
24672467
}
24682468

clippy_lints/src/utils/mod.rs

+1-15
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};
@@ -889,20 +889,6 @@ pub fn return_ty<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, fn_item: hir::HirId) -> T
889889
cx.tcx.erase_late_bound_regions(&ret_ty)
890890
}
891891

892-
/// 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())
904-
}
905-
906892
/// Returns `true` if the given type is an `unsafe` function.
907893
pub fn type_is_unsafe_function<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, ty: Ty<'tcx>) -> bool {
908894
match ty.kind {

0 commit comments

Comments
 (0)