Skip to content

Commit 9118cd6

Browse files
Simplify lifetime-differences-only detection
Now instead of reinventing the wheel with differ_only_in_lifetimes(), we use TyCtxt's erase_regions()
1 parent 8134863 commit 9118cd6

File tree

1 file changed

+2
-45
lines changed

1 file changed

+2
-45
lines changed

clippy_lints/src/transmute.rs

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use rustc::lint::*;
2-
use rustc::ty::{self, Ty, walk::TypeWalker};
2+
use rustc::ty::{self, Ty};
33
use rustc::hir::*;
44
use std::borrow::Cow;
5-
use std::mem;
65
use syntax::ast;
76
use utils::{last_path_segment, match_def_path, paths, snippet, span_lint, span_lint_and_then};
87
use utils::{opt_def_id, sugg};
@@ -364,7 +363,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Transmute {
364363
}
365364
)
366365
} else {
367-
if !differ_only_in_lifetime_params(from_ty, to_ty) {
366+
if cx.tcx.erase_regions(&from_ty) != cx.tcx.erase_regions(&to_ty) {
368367
span_lint_and_then(
369368
cx,
370369
TRANSMUTE_PTR_TO_PTR,
@@ -448,48 +447,6 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Transmute {
448447
}
449448
}
450449

451-
/// Returns true if `type1` and `type2` are the same type except for their lifetime parameters
452-
fn differ_only_in_lifetime_params(type1: Ty, type2: Ty) -> bool {
453-
use rustc::ty::TypeVariants::*;
454-
if TypeWalker::new(type1).count() != TypeWalker::new(type2).count() {
455-
return false;
456-
}
457-
TypeWalker::new(type1)
458-
.zip(TypeWalker::new(type2))
459-
.all(|(t1, t2)| {
460-
match (&t1.sty, &t2.sty) {
461-
// types with generic parameters which can contain lifetimes
462-
(TyAdt(_, sub1), TyAdt(_, sub2))
463-
| (TyFnDef(_, sub1), TyFnDef(_, sub2))
464-
| (TyAnon(_, sub1), TyAnon(_, sub2))
465-
=> {
466-
// Iterate over generic parameters, which are either Lifetimes or Types.
467-
// Here we only need to check that they are the same type of thing, because
468-
// if they are both Lifetimes then we don't care about their equality, and if
469-
// they are both Types, we will check their equality later in the type walk.
470-
sub1.iter().count() == sub2.iter().count()
471-
&& sub1.iter().zip(sub2.iter()).all(|(k1, k2)| {
472-
mem::discriminant(&k1.unpack()) == mem::discriminant(&k2.unpack())
473-
})
474-
}
475-
// types without subtypes: check that the types are equal
476-
(TyBool, TyBool)
477-
| (TyChar, TyChar)
478-
| (TyInt(_), TyInt(_))
479-
| (TyUint(_), TyUint(_))
480-
| (TyFloat(_), TyFloat(_))
481-
| (TyForeign(_), TyForeign(_))
482-
| (TyStr, TyStr)
483-
| (TyNever, TyNever)
484-
| (TyInfer(_), TyInfer(_))
485-
=> t1.sty == t2.sty,
486-
// types with subtypes: return true for now if they are the same sort of type.
487-
// we will check their subtypes later
488-
(sty1, sty2) => mem::discriminant(sty1) == mem::discriminant(sty2)
489-
}
490-
})
491-
}
492-
493450
/// Get the snippet of `Bar` in `…::transmute<Foo, &Bar>`. If that snippet is
494451
/// not available , use
495452
/// the type's `ToString` implementation. In weird cases it could lead to types

0 commit comments

Comments
 (0)