1 use super::utils::is_layout_incompatible;
2 use super::UNSOUND_COLLECTION_TRANSMUTE;
3 use clippy_utils::diagnostics::span_lint;
4 use clippy_utils::match_any_diagnostic_items;
6 use rustc_lint::LateContext;
7 use rustc_middle::ty::{self, Ty};
8 use rustc_span::symbol::{sym, Symbol};
10 // used to check for UNSOUND_COLLECTION_TRANSMUTE
11 static COLLECTIONS: &[Symbol] = &[
21 /// Checks for `unsound_collection_transmute` lint.
22 /// Returns `true` if it's triggered, otherwise returns `false`.
23 pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, from_ty: Ty<'tcx>, to_ty: Ty<'tcx>) -> bool {
24 match (&from_ty.kind(), &to_ty.kind()) {
25 (ty::Adt(from_adt, from_substs), ty::Adt(to_adt, to_substs)) => {
26 if from_adt.did != to_adt.did || match_any_diagnostic_items(cx, to_adt.did, COLLECTIONS).is_none() {
31 .zip(to_substs.types())
32 .any(|(from_ty, to_ty)| is_layout_incompatible(cx, from_ty, to_ty))
36 UNSOUND_COLLECTION_TRANSMUTE,
39 "transmute from `{}` to `{}` with mismatched layout is unsound",